📄 time.texi
字号:
This specifies the Julian day, with @var{n} between @code{1} and @code{365}.February 29 is never counted, even in leap years.@item @var{n}This specifies the Julian day, with @var{n} between @code{0} and @code{365}.February 29 is counted in leap years.@item M@var{m}.@var{w}.@var{d}This specifies day @var{d} of week @var{w} of month @var{m}. The day@var{d} must be between @code{0} (Sunday) and @code{6}. The week@var{w} must be between @code{1} and @code{5}; week @code{1} is thefirst week in which day @var{d} occurs, and week @code{5} specifies the@emph{last} @var{d} day in the month. The month @var{m} should bebetween @code{1} and @code{12}.@end tableThe @var{time} fields specify when, in the local time currently ineffect, the change to the other time occurs. If omitted, the default is@code{02:00:00}.For example, here is how one would specify the Eastern time zone in theUnited States, including the appropriate daylight saving time and its datesof applicability. The normal offset from GMT is 5 hours; since this iswest of the prime meridian, the sign is positive. Summer time begins onthe first Sunday in April at 2:00am, and ends on the last Sunday in Octoberat 2:00am.@smallexampleEST+5EDT,M4.1.0/M10.5.0@end smallexampleThe schedule of daylight savings time in any particular jurisdiction haschanged over the years. To be strictly correct, the conversion of datesand times in the past should be based on the schedule that was in effectthen. However, this format has no facilities to let you specify how theschedule has changed from year to year. The most you can do is specifyone particular schedule---usually the present day schedule---and this isused to convert any date, no matter when. For precise time zonespecifications, it is best to use the time zone information database(see below).The third format looks like this:@smallexample:@var{characters}@end smallexampleEach operating system interprets this format differently; in the GNU Clibrary, @var{characters} is the name of a file which describes the timezone.@pindex /etc/localtime@pindex localtimeIf the @code{TZ} environment variable does not have a value, theoperation chooses a time zone by default. In the GNU C library, thedefault time zone is like the specification @samp{TZ=:/etc/localtime}(or @samp{TZ=:/usr/local/etc/localtime}, depending on how GNU C librarywas configured; @pxref{Installation}). Other C libraries use their ownrule for choosing the default time zone, so there is little we can sayabout them.@cindex time zone database@pindex /share/lib/zoneinfo@pindex zoneinfoIf @var{characters} begins with a slash, it is an absolute file name;otherwise the library looks for the file@w{@file{/share/lib/zoneinfo/@var{characters}}}. The @file{zoneinfo}directory contains data files describing local time zones in manydifferent parts of the world. The names represent major cities, withsubdirectories for geographical areas; for example,@file{America/New_York}, @file{Europe/London}, @file{Asia/Hong_Kong}.These data files are installed by the system administrator, who alsosets @file{/etc/localtime} to point to the data file for the local timezone. The GNU C library comes with a large database of time zoneinformation for most regions of the world, which is maintained by acommunity of volunteers and put in the public domain.@node Time Zone Functions@subsection Functions and Variables for Time Zones@comment time.h@comment POSIX.1@deftypevar char * tzname [2]The array @code{tzname} contains two strings, which are the standardthree-letter names of the pair of time zones (standard and daylightsavings) that the user has selected. @code{tzname[0]} is the name ofthe standard time zone (for example, @code{"EST"}), and @code{tzname[1]}is the name for the time zone when daylight savings time is in use (forexample, @code{"EDT"}). These correspond to the @var{std} and @var{dst}strings (respectively) from the @code{TZ} environment variable.The @code{tzname} array is initialized from the @code{TZ} environmentvariable whenever @code{tzset}, @code{ctime}, @code{strftime},@code{mktime}, or @code{localtime} is called.@end deftypevar@comment time.h@comment POSIX.1@deftypefun void tzset (void)The @code{tzset} function initializes the @code{tzname} variable fromthe value of the @code{TZ} environment variable. It is not usuallynecessary for your program to call this function, because it is calledautomatically when you use the other time conversion functions thatdepend on the time zone.@end deftypefunThe following variables are defined for compatibility with System VUnix. These variables are set by calling @code{localtime}.@comment time.h@comment SVID@deftypevar {long int} timezoneThis contains the difference between GMT and local standard time, inseconds. For example, in the U.S. Eastern time zone, the value is@code{5*60*60}.@end deftypevar@comment time.h@comment SVID@deftypevar int daylightThis variable has a nonzero value if the standard U.S. daylight savingstime rules apply.@end deftypevar@node Time Functions Example@subsection Time Functions ExampleHere is an example program showing the use of some of the local time andcalendar time functions.@smallexample@include strftim.c.texi@end smallexampleIt produces output like this:@smallexampleWed Jul 31 13:02:36 1991Today is Wednesday, July 31.The time is 01:02 PM.@end smallexample@node Setting an Alarm@section Setting an AlarmThe @code{alarm} and @code{setitimer} functions provide a mechanism for aprocess to interrupt itself at some future time. They do this by setting atimer; when the timer expires, the process receives a signal.@cindex setting an alarm@cindex interval timer, setting@cindex alarms, setting@cindex timers, settingEach process has three independent interval timers available:@itemize @bullet@item A real-time timer that counts clock time. This timer sends a@code{SIGALRM} signal to the process when it expires.@cindex real-time timer@cindex timer, real-time@item A virtual timer that counts CPU time used by the process. This timersends a @code{SIGVTALRM} signal to the process when it expires.@cindex virtual timer@cindex timer, virtual@item A profiling timer that counts both CPU time used by the process, and CPUtime spent in system calls on behalf of the process. This timer sends a@code{SIGPROF} signal to the process when it expires.@cindex profiling timer@cindex timer, profilingThis timer is useful for profiling in interpreters. The interval timermechanism does not have the fine granularity necessary for profilingnative code.@c @xref{profil} !!!@end itemizeYou can only have one timer of each kind set at any given time. If youset a timer that has not yet expired, that timer is simply reset to thenew value.You should establish a handler for the appropriate alarm signal using@code{signal} or @code{sigaction} before issuing a call to @code{setitimer}or @code{alarm}. Otherwise, an unusual chain of events could cause thetimer to expire before your program establishes the handler, and in thatcase it would be terminated, since that is the default action for the alarmsignals. @xref{Signal Handling}.The @code{setitimer} function is the primary means for setting an alarm.This facility is declared in the header file @file{sys/time.h}. The@code{alarm} function, declared in @file{unistd.h}, provides a somewhatsimpler interface for setting the real-time timer.@pindex unistd.h@pindex sys/time.h@comment sys/time.h@comment BSD@deftp {Data Type} {struct itimerval}This structure is used to specify when a timer should expire. It containsthe following members:@table @code@item struct timeval it_intervalThis is the interval between successive timer interrupts. If zero, thealarm will only be sent once.@item struct timeval it_valueThis is the interval to the first timer interrupt. If zero, the alarm isdisabled.@end tableThe @code{struct timeval} data type is described in @ref{High-ResolutionCalendar}.@end deftp@comment sys/time.h@comment BSD@deftypefun int setitimer (int @var{which}, struct itimerval *@var{new}, struct itimerval *@var{old})The @code{setitimer} function sets the timer specified by @var{which} according to @var{new}. The @var{which} argument can have a value of@code{ITIMER_REAL}, @code{ITIMER_VIRTUAL}, or @code{ITIMER_PROF}.If @var{old} is not a null pointer, @code{setitimer} returns informationabout any previous unexpired timer of the same kind in the structure itpoints to.The return value is @code{0} on success and @code{-1} on failure. Thefollowing @code{errno} error conditions are defined for this function:@table @code@item EINVALThe timer interval was too large.@end table@end deftypefun@comment sys/time.h@comment BSD@deftypefun int getitimer (int @var{which}, struct itimerval *@var{old})The @code{getitimer} function stores information about the timer specifiedby @var{which} in the structure pointed at by @var{old}.The return value and error conditions are the same as for @code{setitimer}.@end deftypefun@comment sys/time.h@comment BSD@table @code@item ITIMER_REAL@findex ITIMER_REALThis constant can be used as the @var{which} argument to the@code{setitimer} and @code{getitimer} functions to specify the real-timetimer.@comment sys/time.h@comment BSD@item ITIMER_VIRTUAL@findex ITIMER_VIRTUALThis constant can be used as the @var{which} argument to the@code{setitimer} and @code{getitimer} functions to specify the virtualtimer.@comment sys/time.h@comment BSD@item ITIMER_PROF@findex ITIMER_PROFThis constant can be used as the @var{which} argument to the@code{setitimer} and @code{getitimer} functions to specify the profilingtimer.@end table@comment unistd.h@comment POSIX.1@deftypefun {unsigned int} alarm (unsigned int @var{seconds})The @code{alarm} function sets the real-time timer to expire in@var{seconds} seconds. If you want to cancel any existing alarm, youcan do this by calling @code{alarm} with a @var{seconds} argument ofzero.The return value indicates how many seconds remain before the previousalarm would have been sent. If there is no previous alarm, @code{alarm}returns zero.@end deftypefunThe @code{alarm} function could be defined in terms of @code{setitimer}like this:@smallexampleunsigned intalarm (unsigned int seconds)@{ struct itimerval old, new; new.it_interval.tv_usec = 0; new.it_interval.tv_sec = 0; new.it_value.tv_usec = 0; new.it_value.tv_sec = (long int) seconds; if (setitimer (ITIMER_REAL, &new, &old) < 0) return 0; else return old.it_value.tv_sec;@}@end smallexampleThere is an example showing the use of the @code{alarm} function in@ref{Handler Returns}.If you simply want your process to wait for a given number of seconds,you should use the @code{sleep} function. @xref{Sleeping}.You shouldn't count on the signal arriving precisely when the timerexpires. In a multiprocessing environment there is typically someamount of delay involved.@strong{Portability Note:} The @code{setitimer} and @code{getitimer}functions are derived from BSD Unix, while the @code{alarm} function isspecified by the POSIX.1 standard. @code{setitimer} is more powerful than@code{alarm}, but @code{alarm} is more widely used.@node Sleeping@section SleepingThe function @code{sleep} gives a simple way to make the program waitfor short periods of time. If your program doesn't use signals (exceptto terminate), then you can expect @code{sleep} to wait reliably forthe specified amount of time. Otherwise, @code{sleep} can return soonerif a signal arrives; if you want to wait for a given period regardlessof signals, use @code{select} (@pxref{Waiting for I/O}) and don'tspecify any descriptors to wait for.@c !!! select can get EINTR; using SA_RESTART makes sleep win too.@comment unistd.h@comment POSIX.1@deftypefun {unsigned int} sleep (unsigned int @var{seconds})The @code{sleep} function waits for @var{seconds} or until a signalis delivered, whichever happens first. If @code{sleep} function returns because the requested time haselapsed, it returns a value of zero. If it returns because of deliveryof a signal, its return value is the remaining time in the sleep period.The @code{sleep} function is declared in @file{unistd.h}.@end deftypefunResist the temptation to implement a sleep for a fixed amount of time byusing the return value of @code{sleep}, when nonzero, to call@code{sleep} again. This will work with a certain amount of accuracy aslong as signals arrive infrequently. But each signal can cause theeventual wakeup time to be off by an additional second or so. Suppose afew signals happen to arrive in rapid succession by bad luck---there isno limit on how much this could shorten or lengthen the wait.Instead, compute the time at which the program should stop waiting, andkeep trying to wait until that time. This won't be off by more than asecond. With just a little more work, you can use @code{select} andmake the waiting period quite accurate. (Of course, heavy system loadcan cause unavoidable additional delays---unless the machine is dedicated to one application, there is no way you can avoid this.)On some systems, @code{sleep} can do strange things if your program uses@code{SIGALRM} explicitly. Even if @code{SIGALRM} signals are beingignored or blocked when @code{sleep} is called, @code{sleep} mightreturn prematurely on delivery of a @code{SIGALRM} signal. If you haveestablished a handler for @code{SIGALRM} signals and a @code{SIGALRM}signal is delivered while the process is sleeping, the action takenmight be just to cause @code{sleep} to return instead of invoking yourhandler. And, if @code{sleep} is interrupted by delivery of a signalwhose handler requests an alarm or alters the handling of @code{SIGALRM},this handler and @code{sleep} will interfere.On the GNU system, it is safe to use @code{sleep} and @code{SIGALRM} inthe same program, because @code{sleep} does not work by means of@code{SIGALRM}.@node Resource Usage@section Resource Usage@pindex sys/resource.hThe function @code{getrusage} and the data type @code{struct rusage}are used for examining the usage figures of a process. They are declaredin @file{sys/resource.h}.@comment sys/resource.h@comment BSD@deftypefun int getrusage (int @var{processes}, struct rusage *@var{rusage})This function reports the usage totals for processes specified by@var{processes}, storing the information in @code{*@var{rusage}}.In most systems, @var{processes} has only two valid values:@table @code@comment sys/resource.h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -