| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | @node Date and Time, Resource Usage And Limitation, Arithmetic, Top | 
|  | 2 | @c %MENU% Functions for getting the date and time and formatting them nicely | 
|  | 3 | @chapter Date and Time | 
|  | 4 |  | 
|  | 5 | This chapter describes functions for manipulating dates and times, | 
|  | 6 | including functions for determining what time it is and conversion | 
|  | 7 | between different time representations. | 
|  | 8 |  | 
|  | 9 | @menu | 
|  | 10 | * Time Basics::                 Concepts and definitions. | 
|  | 11 | * Elapsed Time::                Data types to represent elapsed times | 
|  | 12 | * Processor And CPU Time::      Time a program has spent executing. | 
|  | 13 | * Calendar Time::               Manipulation of ``real'' dates and times. | 
|  | 14 | * Setting an Alarm::            Sending a signal after a specified time. | 
|  | 15 | * Sleeping::                    Waiting for a period of time. | 
|  | 16 | @end menu | 
|  | 17 |  | 
|  | 18 |  | 
|  | 19 | @node Time Basics | 
|  | 20 | @section Time Basics | 
|  | 21 | @cindex time | 
|  | 22 |  | 
|  | 23 | Discussing time in a technical manual can be difficult because the word | 
|  | 24 | ``time'' in English refers to lots of different things.  In this manual, | 
|  | 25 | we use a rigorous terminology to avoid confusion, and the only thing we | 
|  | 26 | use the simple word ``time'' for is to talk about the abstract concept. | 
|  | 27 |  | 
|  | 28 | A @dfn{calendar time} is a point in the time continuum, for example | 
|  | 29 | November 4, 1990 at 18:02.5 UTC.  Sometimes this is called ``absolute | 
|  | 30 | time''. | 
|  | 31 | @cindex calendar time | 
|  | 32 |  | 
|  | 33 | We don't speak of a ``date'', because that is inherent in a calendar | 
|  | 34 | time. | 
|  | 35 | @cindex date | 
|  | 36 |  | 
|  | 37 | An @dfn{interval} is a contiguous part of the time continuum between two | 
|  | 38 | calendar times, for example the hour between 9:00 and 10:00 on July 4, | 
|  | 39 | 1980. | 
|  | 40 | @cindex interval | 
|  | 41 |  | 
|  | 42 | An @dfn{elapsed time} is the length of an interval, for example, 35 | 
|  | 43 | minutes.  People sometimes sloppily use the word ``interval'' to refer | 
|  | 44 | to the elapsed time of some interval. | 
|  | 45 | @cindex elapsed time | 
|  | 46 | @cindex time, elapsed | 
|  | 47 |  | 
|  | 48 | An @dfn{amount of time} is a sum of elapsed times, which need not be of | 
|  | 49 | any specific intervals.  For example, the amount of time it takes to | 
|  | 50 | read a book might be 9 hours, independently of when and in how many | 
|  | 51 | sittings it is read. | 
|  | 52 |  | 
|  | 53 | A @dfn{period} is the elapsed time of an interval between two events, | 
|  | 54 | especially when they are part of a sequence of regularly repeating | 
|  | 55 | events. | 
|  | 56 | @cindex period of time | 
|  | 57 |  | 
|  | 58 | @dfn{CPU time} is like calendar time, except that it is based on the | 
|  | 59 | subset of the time continuum when a particular process is actively | 
|  | 60 | using a CPU.  CPU time is, therefore, relative to a process. | 
|  | 61 | @cindex CPU time | 
|  | 62 |  | 
|  | 63 | @dfn{Processor time} is an amount of time that a CPU is in use.  In | 
|  | 64 | fact, it's a basic system resource, since there's a limit to how much | 
|  | 65 | can exist in any given interval (that limit is the elapsed time of the | 
|  | 66 | interval times the number of CPUs in the processor).  People often call | 
|  | 67 | this CPU time, but we reserve the latter term in this manual for the | 
|  | 68 | definition above. | 
|  | 69 | @cindex processor time | 
|  | 70 |  | 
|  | 71 | @node Elapsed Time | 
|  | 72 | @section Elapsed Time | 
|  | 73 | @cindex elapsed time | 
|  | 74 |  | 
|  | 75 | One way to represent an elapsed time is with a simple arithmetic data | 
|  | 76 | type, as with the following function to compute the elapsed time between | 
|  | 77 | two calendar times.  This function is declared in @file{time.h}. | 
|  | 78 |  | 
|  | 79 | @comment time.h | 
|  | 80 | @comment ISO | 
|  | 81 | @deftypefun double difftime (time_t @var{time1}, time_t @var{time0}) | 
|  | 82 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | 
|  | 83 | The @code{difftime} function returns the number of seconds of elapsed | 
|  | 84 | time between calendar time @var{time1} and calendar time @var{time0}, as | 
|  | 85 | a value of type @code{double}.  The difference ignores leap seconds | 
|  | 86 | unless leap second support is enabled. | 
|  | 87 |  | 
|  | 88 | In @theglibc{}, you can simply subtract @code{time_t} values.  But on | 
|  | 89 | other systems, the @code{time_t} data type might use some other encoding | 
|  | 90 | where subtraction doesn't work directly. | 
|  | 91 | @end deftypefun | 
|  | 92 |  | 
|  | 93 | @Theglibc{} provides two data types specifically for representing | 
|  | 94 | an elapsed time.  They are used by various @glibcadj{} functions, and | 
|  | 95 | you can use them for your own purposes too.  They're exactly the same | 
|  | 96 | except that one has a resolution in microseconds, and the other, newer | 
|  | 97 | one, is in nanoseconds. | 
|  | 98 |  | 
|  | 99 | @comment sys/time.h | 
|  | 100 | @comment BSD | 
|  | 101 | @deftp {Data Type} {struct timeval} | 
|  | 102 | @cindex timeval | 
|  | 103 | The @code{struct timeval} structure represents an elapsed time.  It is | 
|  | 104 | declared in @file{sys/time.h} and has the following members: | 
|  | 105 |  | 
|  | 106 | @table @code | 
|  | 107 | @item time_t tv_sec | 
|  | 108 | This represents the number of whole seconds of elapsed time. | 
|  | 109 |  | 
|  | 110 | @item long int tv_usec | 
|  | 111 | This is the rest of the elapsed time (a fraction of a second), | 
|  | 112 | represented as the number of microseconds.  It is always less than one | 
|  | 113 | million. | 
|  | 114 |  | 
|  | 115 | @end table | 
|  | 116 | @end deftp | 
|  | 117 |  | 
|  | 118 | @comment sys/time.h | 
|  | 119 | @comment POSIX.1 | 
|  | 120 | @deftp {Data Type} {struct timespec} | 
|  | 121 | @cindex timespec | 
|  | 122 | The @code{struct timespec} structure represents an elapsed time.  It is | 
|  | 123 | declared in @file{time.h} and has the following members: | 
|  | 124 |  | 
|  | 125 | @table @code | 
|  | 126 | @item time_t tv_sec | 
|  | 127 | This represents the number of whole seconds of elapsed time. | 
|  | 128 |  | 
|  | 129 | @item long int tv_nsec | 
|  | 130 | This is the rest of the elapsed time (a fraction of a second), | 
|  | 131 | represented as the number of nanoseconds.  It is always less than one | 
|  | 132 | billion. | 
|  | 133 |  | 
|  | 134 | @end table | 
|  | 135 | @end deftp | 
|  | 136 |  | 
|  | 137 | It is often necessary to subtract two values of type @w{@code{struct | 
|  | 138 | timeval}} or @w{@code{struct timespec}}.  Here is the best way to do | 
|  | 139 | this.  It works even on some peculiar operating systems where the | 
|  | 140 | @code{tv_sec} member has an unsigned type. | 
|  | 141 |  | 
|  | 142 | @smallexample | 
|  | 143 | @include timeval_subtract.c.texi | 
|  | 144 | @end smallexample | 
|  | 145 |  | 
|  | 146 | Common functions that use @code{struct timeval} are @code{gettimeofday} | 
|  | 147 | and @code{settimeofday}. | 
|  | 148 |  | 
|  | 149 |  | 
|  | 150 | There are no @glibcadj{} functions specifically oriented toward | 
|  | 151 | dealing with elapsed times, but the calendar time, processor time, and | 
|  | 152 | alarm and sleeping functions have a lot to do with them. | 
|  | 153 |  | 
|  | 154 |  | 
|  | 155 | @node Processor And CPU Time | 
|  | 156 | @section Processor And CPU Time | 
|  | 157 |  | 
|  | 158 | If you're trying to optimize your program or measure its efficiency, | 
|  | 159 | it's very useful to know how much processor time it uses.  For that, | 
|  | 160 | calendar time and elapsed times are useless because a process may spend | 
|  | 161 | time waiting for I/O or for other processes to use the CPU.  However, | 
|  | 162 | you can get the information with the functions in this section. | 
|  | 163 |  | 
|  | 164 | CPU time (@pxref{Time Basics}) is represented by the data type | 
|  | 165 | @code{clock_t}, which is a number of @dfn{clock ticks}.  It gives the | 
|  | 166 | total amount of time a process has actively used a CPU since some | 
|  | 167 | arbitrary event.  On @gnusystems{}, that event is the creation of the | 
|  | 168 | process.  While arbitrary in general, the event is always the same event | 
|  | 169 | for any particular process, so you can always measure how much time on | 
|  | 170 | the CPU a particular computation takes by examining the process' CPU | 
|  | 171 | time before and after the computation. | 
|  | 172 | @cindex CPU time | 
|  | 173 | @cindex clock ticks | 
|  | 174 | @cindex ticks, clock | 
|  | 175 |  | 
|  | 176 | On @gnulinuxhurdsystems{}, @code{clock_t} is equivalent to @code{long int} and | 
|  | 177 | @code{CLOCKS_PER_SEC} is an integer value.  But in other systems, both | 
|  | 178 | @code{clock_t} and the macro @code{CLOCKS_PER_SEC} can be either integer | 
|  | 179 | or floating-point types.  Casting CPU time values to @code{double}, as | 
|  | 180 | in the example above, makes sure that operations such as arithmetic and | 
|  | 181 | printing work properly and consistently no matter what the underlying | 
|  | 182 | representation is. | 
|  | 183 |  | 
|  | 184 | Note that the clock can wrap around.  On a 32bit system with | 
|  | 185 | @code{CLOCKS_PER_SEC} set to one million this function will return the | 
|  | 186 | same value approximately every 72 minutes. | 
|  | 187 |  | 
|  | 188 | For additional functions to examine a process' use of processor time, | 
|  | 189 | and to control it, see @ref{Resource Usage And Limitation}. | 
|  | 190 |  | 
|  | 191 |  | 
|  | 192 | @menu | 
|  | 193 | * CPU Time::                    The @code{clock} function. | 
|  | 194 | * Processor Time::              The @code{times} function. | 
|  | 195 | @end menu | 
|  | 196 |  | 
|  | 197 | @node CPU Time | 
|  | 198 | @subsection CPU Time Inquiry | 
|  | 199 |  | 
|  | 200 | To get a process' CPU time, you can use the @code{clock} function.  This | 
|  | 201 | facility is declared in the header file @file{time.h}. | 
|  | 202 | @pindex time.h | 
|  | 203 |  | 
|  | 204 | In typical usage, you call the @code{clock} function at the beginning | 
|  | 205 | and end of the interval you want to time, subtract the values, and then | 
|  | 206 | divide by @code{CLOCKS_PER_SEC} (the number of clock ticks per second) | 
|  | 207 | to get processor time, like this: | 
|  | 208 |  | 
|  | 209 | @smallexample | 
|  | 210 | @group | 
|  | 211 | #include <time.h> | 
|  | 212 |  | 
|  | 213 | clock_t start, end; | 
|  | 214 | double cpu_time_used; | 
|  | 215 |  | 
|  | 216 | start = clock(); | 
|  | 217 | @dots{} /* @r{Do the work.} */ | 
|  | 218 | end = clock(); | 
|  | 219 | cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; | 
|  | 220 | @end group | 
|  | 221 | @end smallexample | 
|  | 222 |  | 
|  | 223 | Do not use a single CPU time as an amount of time; it doesn't work that | 
|  | 224 | way.  Either do a subtraction as shown above or query processor time | 
|  | 225 | directly.  @xref{Processor Time}. | 
|  | 226 |  | 
|  | 227 | Different computers and operating systems vary wildly in how they keep | 
|  | 228 | track of CPU time.  It's common for the internal processor clock | 
|  | 229 | to have a resolution somewhere between a hundredth and millionth of a | 
|  | 230 | second. | 
|  | 231 |  | 
|  | 232 | @comment time.h | 
|  | 233 | @comment ISO | 
|  | 234 | @deftypevr Macro int CLOCKS_PER_SEC | 
|  | 235 | The value of this macro is the number of clock ticks per second measured | 
|  | 236 | by the @code{clock} function.  POSIX requires that this value be one | 
|  | 237 | million independent of the actual resolution. | 
|  | 238 | @end deftypevr | 
|  | 239 |  | 
|  | 240 | @comment time.h | 
|  | 241 | @comment ISO | 
|  | 242 | @deftp {Data Type} clock_t | 
|  | 243 | This is the type of the value returned by the @code{clock} function. | 
|  | 244 | Values of type @code{clock_t} are numbers of clock ticks. | 
|  | 245 | @end deftp | 
|  | 246 |  | 
|  | 247 | @comment time.h | 
|  | 248 | @comment ISO | 
|  | 249 | @deftypefun clock_t clock (void) | 
|  | 250 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | 
|  | 251 | @c On Hurd, this calls task_info twice and adds user and system time | 
|  | 252 | @c from both basic and thread time info structs.  On generic posix, | 
|  | 253 | @c calls times and adds utime and stime.  On bsd, calls getrusage and | 
|  | 254 | @c safely converts stime and utime to clock.  On linux, calls | 
|  | 255 | @c clock_gettime. | 
|  | 256 | This function returns the calling process' current CPU time.  If the CPU | 
|  | 257 | time is not available or cannot be represented, @code{clock} returns the | 
|  | 258 | value @code{(clock_t)(-1)}. | 
|  | 259 | @end deftypefun | 
|  | 260 |  | 
|  | 261 |  | 
|  | 262 | @node Processor Time | 
|  | 263 | @subsection Processor Time Inquiry | 
|  | 264 |  | 
|  | 265 | The @code{times} function returns information about a process' | 
|  | 266 | consumption of processor time in a @w{@code{struct tms}} object, in | 
|  | 267 | addition to the process' CPU time.  @xref{Time Basics}.  You should | 
|  | 268 | include the header file @file{sys/times.h} to use this facility. | 
|  | 269 | @cindex processor time | 
|  | 270 | @cindex CPU time | 
|  | 271 | @pindex sys/times.h | 
|  | 272 |  | 
|  | 273 | @comment sys/times.h | 
|  | 274 | @comment POSIX.1 | 
|  | 275 | @deftp {Data Type} {struct tms} | 
|  | 276 | The @code{tms} structure is used to return information about process | 
|  | 277 | times.  It contains at least the following members: | 
|  | 278 |  | 
|  | 279 | @table @code | 
|  | 280 | @item clock_t tms_utime | 
|  | 281 | This is the total processor time the calling process has used in | 
|  | 282 | executing the instructions of its program. | 
|  | 283 |  | 
|  | 284 | @item clock_t tms_stime | 
|  | 285 | This is the processor time the system has used on behalf of the calling | 
|  | 286 | process. | 
|  | 287 |  | 
|  | 288 | @item clock_t tms_cutime | 
|  | 289 | This is the sum of the @code{tms_utime} values and the @code{tms_cutime} | 
|  | 290 | values of all terminated child processes of the calling process, whose | 
|  | 291 | status has been reported to the parent process by @code{wait} or | 
|  | 292 | @code{waitpid}; see @ref{Process Completion}.  In other words, it | 
|  | 293 | represents the total processor time used in executing the instructions | 
|  | 294 | of all the terminated child processes of the calling process, excluding | 
|  | 295 | child processes which have not yet been reported by @code{wait} or | 
|  | 296 | @code{waitpid}. | 
|  | 297 | @cindex child process | 
|  | 298 |  | 
|  | 299 | @item clock_t tms_cstime | 
|  | 300 | This is similar to @code{tms_cutime}, but represents the total processor | 
|  | 301 | time system has used on behalf of all the terminated child processes | 
|  | 302 | of the calling process. | 
|  | 303 | @end table | 
|  | 304 |  | 
|  | 305 | All of the times are given in numbers of clock ticks.  Unlike CPU time, | 
|  | 306 | these are the actual amounts of time; not relative to any event. | 
|  | 307 | @xref{Creating a Process}. | 
|  | 308 | @end deftp | 
|  | 309 |  | 
|  | 310 | @comment time.h | 
|  | 311 | @comment POSIX.1 | 
|  | 312 | @deftypevr Macro int CLK_TCK | 
|  | 313 | This is an obsolete name for the number of clock ticks per second.  Use | 
|  | 314 | @code{sysconf (_SC_CLK_TCK)} instead. | 
|  | 315 | @end deftypevr | 
|  | 316 |  | 
|  | 317 | @comment sys/times.h | 
|  | 318 | @comment POSIX.1 | 
|  | 319 | @deftypefun clock_t times (struct tms *@var{buffer}) | 
|  | 320 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | 
|  | 321 | @c On HURD, this calls task_info twice, for basic and thread times info, | 
|  | 322 | @c adding user and system times into tms, and then gettimeofday, to | 
|  | 323 | @c compute the real time.  On BSD, it calls getclktck, getrusage (twice) | 
|  | 324 | @c and time.  On Linux, it's a syscall with special handling to account | 
|  | 325 | @c for clock_t counts that look like error values. | 
|  | 326 | The @code{times} function stores the processor time information for | 
|  | 327 | the calling process in @var{buffer}. | 
|  | 328 |  | 
|  | 329 | The return value is the number of clock ticks since an arbitrary point | 
|  | 330 | in the past, e.g. since system start-up.  @code{times} returns | 
|  | 331 | @code{(clock_t)(-1)} to indicate failure. | 
|  | 332 | @end deftypefun | 
|  | 333 |  | 
|  | 334 | @strong{Portability Note:} The @code{clock} function described in | 
|  | 335 | @ref{CPU Time} is specified by the @w{ISO C} standard.  The | 
|  | 336 | @code{times} function is a feature of POSIX.1.  On @gnusystems{}, the | 
|  | 337 | CPU time is defined to be equivalent to the sum of the @code{tms_utime} | 
|  | 338 | and @code{tms_stime} fields returned by @code{times}. | 
|  | 339 |  | 
|  | 340 | @node Calendar Time | 
|  | 341 | @section Calendar Time | 
|  | 342 |  | 
|  | 343 | This section describes facilities for keeping track of calendar time. | 
|  | 344 | @xref{Time Basics}. | 
|  | 345 |  | 
|  | 346 | @Theglibc{} represents calendar time three ways: | 
|  | 347 |  | 
|  | 348 | @itemize @bullet | 
|  | 349 | @item | 
|  | 350 | @dfn{Simple time} (the @code{time_t} data type) is a compact | 
|  | 351 | representation, typically giving the number of seconds of elapsed time | 
|  | 352 | since some implementation-specific base time. | 
|  | 353 | @cindex simple time | 
|  | 354 |  | 
|  | 355 | @item | 
|  | 356 | There is also a "high-resolution time" representation.  Like simple | 
|  | 357 | time, this represents a calendar time as an elapsed time since a base | 
|  | 358 | time, but instead of measuring in whole seconds, it uses a @code{struct | 
|  | 359 | timeval} data type, which includes fractions of a second.  Use this time | 
|  | 360 | representation instead of simple time when you need greater precision. | 
|  | 361 | @cindex high-resolution time | 
|  | 362 |  | 
|  | 363 | @item | 
|  | 364 | @dfn{Local time} or @dfn{broken-down time} (the @code{struct tm} data | 
|  | 365 | type) represents a calendar time as a set of components specifying the | 
|  | 366 | year, month, and so on in the Gregorian calendar, for a specific time | 
|  | 367 | zone.  This calendar time representation is usually used only to | 
|  | 368 | communicate with people. | 
|  | 369 | @cindex local time | 
|  | 370 | @cindex broken-down time | 
|  | 371 | @cindex Gregorian calendar | 
|  | 372 | @cindex calendar, Gregorian | 
|  | 373 | @end itemize | 
|  | 374 |  | 
|  | 375 | @menu | 
|  | 376 | * Simple Calendar Time::        Facilities for manipulating calendar time. | 
|  | 377 | * High-Resolution Calendar::    A time representation with greater precision. | 
|  | 378 | * Broken-down Time::            Facilities for manipulating local time. | 
|  | 379 | * High Accuracy Clock::         Maintaining a high accuracy system clock. | 
|  | 380 | * Formatting Calendar Time::    Converting times to strings. | 
|  | 381 | * Parsing Date and Time::       Convert textual time and date information back | 
|  | 382 | into broken-down time values. | 
|  | 383 | * TZ Variable::                 How users specify the time zone. | 
|  | 384 | * Time Zone Functions::         Functions to examine or specify the time zone. | 
|  | 385 | * Time Functions Example::      An example program showing use of some of | 
|  | 386 | the time functions. | 
|  | 387 | @end menu | 
|  | 388 |  | 
|  | 389 | @node Simple Calendar Time | 
|  | 390 | @subsection Simple Calendar Time | 
|  | 391 |  | 
|  | 392 | This section describes the @code{time_t} data type for representing calendar | 
|  | 393 | time as simple time, and the functions which operate on simple time objects. | 
|  | 394 | These facilities are declared in the header file @file{time.h}. | 
|  | 395 | @pindex time.h | 
|  | 396 |  | 
|  | 397 | @cindex epoch | 
|  | 398 | @comment time.h | 
|  | 399 | @comment ISO | 
|  | 400 | @deftp {Data Type} time_t | 
|  | 401 | This is the data type used to represent simple time.  Sometimes, it also | 
|  | 402 | represents an elapsed time.  When interpreted as a calendar time value, | 
|  | 403 | it represents the number of seconds elapsed since 00:00:00 on January 1, | 
|  | 404 | 1970, Coordinated Universal Time.  (This calendar time is sometimes | 
|  | 405 | referred to as the @dfn{epoch}.)  POSIX requires that this count not | 
|  | 406 | include leap seconds, but on some systems this count includes leap seconds | 
|  | 407 | if you set @code{TZ} to certain values (@pxref{TZ Variable}). | 
|  | 408 |  | 
|  | 409 | Note that a simple time has no concept of local time zone.  Calendar | 
|  | 410 | Time @var{T} is the same instant in time regardless of where on the | 
|  | 411 | globe the computer is. | 
|  | 412 |  | 
|  | 413 | In @theglibc{}, @code{time_t} is equivalent to @code{long int}. | 
|  | 414 | In other systems, @code{time_t} might be either an integer or | 
|  | 415 | floating-point type. | 
|  | 416 | @end deftp | 
|  | 417 |  | 
|  | 418 | The function @code{difftime} tells you the elapsed time between two | 
|  | 419 | simple calendar times, which is not always as easy to compute as just | 
|  | 420 | subtracting.  @xref{Elapsed Time}. | 
|  | 421 |  | 
|  | 422 | @comment time.h | 
|  | 423 | @comment ISO | 
|  | 424 | @deftypefun time_t time (time_t *@var{result}) | 
|  | 425 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | 
|  | 426 | The @code{time} function returns the current calendar time as a value of | 
|  | 427 | type @code{time_t}.  If the argument @var{result} is not a null pointer, | 
|  | 428 | the calendar time value is also stored in @code{*@var{result}}.  If the | 
|  | 429 | current calendar time is not available, the value | 
|  | 430 | @w{@code{(time_t)(-1)}} is returned. | 
|  | 431 | @end deftypefun | 
|  | 432 |  | 
|  | 433 | @c The GNU C library implements stime() with a call to settimeofday() on | 
|  | 434 | @c Linux. | 
|  | 435 | @comment time.h | 
|  | 436 | @comment SVID, XPG | 
|  | 437 | @deftypefun int stime (const time_t *@var{newtime}) | 
|  | 438 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | 
|  | 439 | @c On unix, this is implemented in terms of settimeofday. | 
|  | 440 | @code{stime} sets the system clock, i.e., it tells the system that the | 
|  | 441 | current calendar time is @var{newtime}, where @code{newtime} is | 
|  | 442 | interpreted as described in the above definition of @code{time_t}. | 
|  | 443 |  | 
|  | 444 | @code{settimeofday} is a newer function which sets the system clock to | 
|  | 445 | better than one second precision.  @code{settimeofday} is generally a | 
|  | 446 | better choice than @code{stime}.  @xref{High-Resolution Calendar}. | 
|  | 447 |  | 
|  | 448 | Only the superuser can set the system clock. | 
|  | 449 |  | 
|  | 450 | If the function succeeds, the return value is zero.  Otherwise, it is | 
|  | 451 | @code{-1} and @code{errno} is set accordingly: | 
|  | 452 |  | 
|  | 453 | @table @code | 
|  | 454 | @item EPERM | 
|  | 455 | The process is not superuser. | 
|  | 456 | @end table | 
|  | 457 | @end deftypefun | 
|  | 458 |  | 
|  | 459 |  | 
|  | 460 |  | 
|  | 461 | @node High-Resolution Calendar | 
|  | 462 | @subsection High-Resolution Calendar | 
|  | 463 |  | 
|  | 464 | The @code{time_t} data type used to represent simple times has a | 
|  | 465 | resolution of only one second.  Some applications need more precision. | 
|  | 466 |  | 
|  | 467 | So, @theglibc{} also contains functions which are capable of | 
|  | 468 | representing calendar times to a higher resolution than one second.  The | 
|  | 469 | functions and the associated data types described in this section are | 
|  | 470 | declared in @file{sys/time.h}. | 
|  | 471 | @pindex sys/time.h | 
|  | 472 |  | 
|  | 473 | @comment sys/time.h | 
|  | 474 | @comment BSD | 
|  | 475 | @deftp {Data Type} {struct timezone} | 
|  | 476 | The @code{struct timezone} structure is used to hold minimal information | 
|  | 477 | about the local time zone.  It has the following members: | 
|  | 478 |  | 
|  | 479 | @table @code | 
|  | 480 | @item int tz_minuteswest | 
|  | 481 | This is the number of minutes west of UTC. | 
|  | 482 |  | 
|  | 483 | @item int tz_dsttime | 
|  | 484 | If nonzero, Daylight Saving Time applies during some part of the year. | 
|  | 485 | @end table | 
|  | 486 |  | 
|  | 487 | The @code{struct timezone} type is obsolete and should never be used. | 
|  | 488 | Instead, use the facilities described in @ref{Time Zone Functions}. | 
|  | 489 | @end deftp | 
|  | 490 |  | 
|  | 491 | @comment sys/time.h | 
|  | 492 | @comment BSD | 
|  | 493 | @deftypefun int gettimeofday (struct timeval *@var{tp}, struct timezone *@var{tzp}) | 
|  | 494 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | 
|  | 495 | @c On most GNU/Linux systems this is a direct syscall, but the posix/ | 
|  | 496 | @c implementation (not used on GNU/Linux or GNU/Hurd) relies on time and | 
|  | 497 | @c localtime_r, saving and restoring tzname in an unsafe manner. | 
|  | 498 | @c On some GNU/Linux variants, ifunc resolvers are used in shared libc | 
|  | 499 | @c for vdso resolution.  ifunc-vdso-revisit. | 
|  | 500 | The @code{gettimeofday} function returns the current calendar time as | 
|  | 501 | the elapsed time since the epoch in the @code{struct timeval} structure | 
|  | 502 | indicated by @var{tp}.  (@pxref{Elapsed Time} for a description of | 
|  | 503 | @code{struct timeval}).  Information about the time zone is returned in | 
|  | 504 | the structure pointed at @var{tzp}.  If the @var{tzp} argument is a null | 
|  | 505 | pointer, time zone information is ignored. | 
|  | 506 |  | 
|  | 507 | The return value is @code{0} on success and @code{-1} on failure.  The | 
|  | 508 | following @code{errno} error condition is defined for this function: | 
|  | 509 |  | 
|  | 510 | @table @code | 
|  | 511 | @item ENOSYS | 
|  | 512 | The operating system does not support getting time zone information, and | 
|  | 513 | @var{tzp} is not a null pointer.  @gnusystems{} do not | 
|  | 514 | support using @w{@code{struct timezone}} to represent time zone | 
|  | 515 | information; that is an obsolete feature of 4.3 BSD. | 
|  | 516 | Instead, use the facilities described in @ref{Time Zone Functions}. | 
|  | 517 | @end table | 
|  | 518 | @end deftypefun | 
|  | 519 |  | 
|  | 520 | @comment sys/time.h | 
|  | 521 | @comment BSD | 
|  | 522 | @deftypefun int settimeofday (const struct timeval *@var{tp}, const struct timezone *@var{tzp}) | 
|  | 523 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | 
|  | 524 | @c On HURD, it calls host_set_time with a privileged port.  On other | 
|  | 525 | @c unix systems, it's a syscall. | 
|  | 526 | The @code{settimeofday} function sets the current calendar time in the | 
|  | 527 | system clock according to the arguments.  As for @code{gettimeofday}, | 
|  | 528 | the calendar time is represented as the elapsed time since the epoch. | 
|  | 529 | As for @code{gettimeofday}, time zone information is ignored if | 
|  | 530 | @var{tzp} is a null pointer. | 
|  | 531 |  | 
|  | 532 | You must be a privileged user in order to use @code{settimeofday}. | 
|  | 533 |  | 
|  | 534 | Some kernels automatically set the system clock from some source such as | 
|  | 535 | a hardware clock when they start up.  Others, including Linux, place the | 
|  | 536 | system clock in an ``invalid'' state (in which attempts to read the clock | 
|  | 537 | fail).  A call of @code{stime} removes the system clock from an invalid | 
|  | 538 | state, and system startup scripts typically run a program that calls | 
|  | 539 | @code{stime}. | 
|  | 540 |  | 
|  | 541 | @code{settimeofday} causes a sudden jump forwards or backwards, which | 
|  | 542 | can cause a variety of problems in a system.  Use @code{adjtime} (below) | 
|  | 543 | to make a smooth transition from one time to another by temporarily | 
|  | 544 | speeding up or slowing down the clock. | 
|  | 545 |  | 
|  | 546 | With a Linux kernel, @code{adjtimex} does the same thing and can also | 
|  | 547 | make permanent changes to the speed of the system clock so it doesn't | 
|  | 548 | need to be corrected as often. | 
|  | 549 |  | 
|  | 550 | The return value is @code{0} on success and @code{-1} on failure.  The | 
|  | 551 | following @code{errno} error conditions are defined for this function: | 
|  | 552 |  | 
|  | 553 | @table @code | 
|  | 554 | @item EPERM | 
|  | 555 | This process cannot set the clock because it is not privileged. | 
|  | 556 |  | 
|  | 557 | @item ENOSYS | 
|  | 558 | The operating system does not support setting time zone information, and | 
|  | 559 | @var{tzp} is not a null pointer. | 
|  | 560 | @end table | 
|  | 561 | @end deftypefun | 
|  | 562 |  | 
|  | 563 | @c On Linux, GNU libc implements adjtime() as a call to adjtimex(). | 
|  | 564 | @comment sys/time.h | 
|  | 565 | @comment BSD | 
|  | 566 | @deftypefun int adjtime (const struct timeval *@var{delta}, struct timeval *@var{olddelta}) | 
|  | 567 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | 
|  | 568 | @c On hurd and mach, call host_adjust_time with a privileged port.  On | 
|  | 569 | @c Linux, it's implemented in terms of adjtimex.  On other unixen, it's | 
|  | 570 | @c a syscall. | 
|  | 571 | This function speeds up or slows down the system clock in order to make | 
|  | 572 | a gradual adjustment.  This ensures that the calendar time reported by | 
|  | 573 | the system clock is always monotonically increasing, which might not | 
|  | 574 | happen if you simply set the clock. | 
|  | 575 |  | 
|  | 576 | The @var{delta} argument specifies a relative adjustment to be made to | 
|  | 577 | the clock time.  If negative, the system clock is slowed down for a | 
|  | 578 | while until it has lost this much elapsed time.  If positive, the system | 
|  | 579 | clock is speeded up for a while. | 
|  | 580 |  | 
|  | 581 | If the @var{olddelta} argument is not a null pointer, the @code{adjtime} | 
|  | 582 | function returns information about any previous time adjustment that | 
|  | 583 | has not yet completed. | 
|  | 584 |  | 
|  | 585 | This function is typically used to synchronize the clocks of computers | 
|  | 586 | in a local network.  You must be a privileged user to use it. | 
|  | 587 |  | 
|  | 588 | With a Linux kernel, you can use the @code{adjtimex} function to | 
|  | 589 | permanently change the clock speed. | 
|  | 590 |  | 
|  | 591 | The return value is @code{0} on success and @code{-1} on failure.  The | 
|  | 592 | following @code{errno} error condition is defined for this function: | 
|  | 593 |  | 
|  | 594 | @table @code | 
|  | 595 | @item EPERM | 
|  | 596 | You do not have privilege to set the time. | 
|  | 597 | @end table | 
|  | 598 | @end deftypefun | 
|  | 599 |  | 
|  | 600 | @strong{Portability Note:}  The @code{gettimeofday}, @code{settimeofday}, | 
|  | 601 | and @code{adjtime} functions are derived from BSD. | 
|  | 602 |  | 
|  | 603 |  | 
|  | 604 | Symbols for the following function are declared in @file{sys/timex.h}. | 
|  | 605 |  | 
|  | 606 | @comment sys/timex.h | 
|  | 607 | @comment GNU | 
|  | 608 | @deftypefun int adjtimex (struct timex *@var{timex}) | 
|  | 609 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | 
|  | 610 | @c It's a syscall, only available on linux. | 
|  | 611 |  | 
|  | 612 | @code{adjtimex} is functionally identical to @code{ntp_adjtime}. | 
|  | 613 | @xref{High Accuracy Clock}. | 
|  | 614 |  | 
|  | 615 | This function is present only with a Linux kernel. | 
|  | 616 |  | 
|  | 617 | @end deftypefun | 
|  | 618 |  | 
|  | 619 | @node Broken-down Time | 
|  | 620 | @subsection Broken-down Time | 
|  | 621 | @cindex broken-down time | 
|  | 622 | @cindex calendar time and broken-down time | 
|  | 623 |  | 
|  | 624 | Calendar time is represented by the usual @glibcadj{} functions as an | 
|  | 625 | elapsed time since a fixed base calendar time.  This is convenient for | 
|  | 626 | computation, but has no relation to the way people normally think of | 
|  | 627 | calendar time.  By contrast, @dfn{broken-down time} is a binary | 
|  | 628 | representation of calendar time separated into year, month, day, and so | 
|  | 629 | on.  Broken-down time values are not useful for calculations, but they | 
|  | 630 | are useful for printing human readable time information. | 
|  | 631 |  | 
|  | 632 | A broken-down time value is always relative to a choice of time | 
|  | 633 | zone, and it also indicates which time zone that is. | 
|  | 634 |  | 
|  | 635 | The symbols in this section are declared in the header file @file{time.h}. | 
|  | 636 |  | 
|  | 637 | @comment time.h | 
|  | 638 | @comment ISO | 
|  | 639 | @deftp {Data Type} {struct tm} | 
|  | 640 | This is the data type used to represent a broken-down time.  The structure | 
|  | 641 | contains at least the following members, which can appear in any order. | 
|  | 642 |  | 
|  | 643 | @table @code | 
|  | 644 | @item int tm_sec | 
|  | 645 | This is the number of full seconds since the top of the minute (normally | 
|  | 646 | in the range @code{0} through @code{59}, but the actual upper limit is | 
|  | 647 | @code{60}, to allow for leap seconds if leap second support is | 
|  | 648 | available). | 
|  | 649 | @cindex leap second | 
|  | 650 |  | 
|  | 651 | @item int tm_min | 
|  | 652 | This is the number of full minutes since the top of the hour (in the | 
|  | 653 | range @code{0} through @code{59}). | 
|  | 654 |  | 
|  | 655 | @item int tm_hour | 
|  | 656 | This is the number of full hours past midnight (in the range @code{0} through | 
|  | 657 | @code{23}). | 
|  | 658 |  | 
|  | 659 | @item int tm_mday | 
|  | 660 | This is the ordinal day of the month (in the range @code{1} through @code{31}). | 
|  | 661 | Watch out for this one!  As the only ordinal number in the structure, it is | 
|  | 662 | inconsistent with the rest of the structure. | 
|  | 663 |  | 
|  | 664 | @item int tm_mon | 
|  | 665 | This is the number of full calendar months since the beginning of the | 
|  | 666 | year (in the range @code{0} through @code{11}).  Watch out for this one! | 
|  | 667 | People usually use ordinal numbers for month-of-year (where January = 1). | 
|  | 668 |  | 
|  | 669 | @item int tm_year | 
|  | 670 | This is the number of full calendar years since 1900. | 
|  | 671 |  | 
|  | 672 | @item int tm_wday | 
|  | 673 | This is the number of full days since Sunday (in the range @code{0} through | 
|  | 674 | @code{6}). | 
|  | 675 |  | 
|  | 676 | @item int tm_yday | 
|  | 677 | This is the number of full days since the beginning of the year (in the | 
|  | 678 | range @code{0} through @code{365}). | 
|  | 679 |  | 
|  | 680 | @item int tm_isdst | 
|  | 681 | @cindex Daylight Saving Time | 
|  | 682 | @cindex summer time | 
|  | 683 | This is a flag that indicates whether Daylight Saving Time is (or was, or | 
|  | 684 | will be) in effect at the time described.  The value is positive if | 
|  | 685 | Daylight Saving Time is in effect, zero if it is not, and negative if the | 
|  | 686 | information is not available. | 
|  | 687 |  | 
|  | 688 | @item long int tm_gmtoff | 
|  | 689 | This field describes the time zone that was used to compute this | 
|  | 690 | broken-down time value, including any adjustment for daylight saving; it | 
|  | 691 | is the number of seconds that you must add to UTC to get local time. | 
|  | 692 | You can also think of this as the number of seconds east of UTC.  For | 
|  | 693 | example, for U.S. Eastern Standard Time, the value is @code{-5*60*60}. | 
|  | 694 | The @code{tm_gmtoff} field is derived from BSD and is a GNU library | 
|  | 695 | extension; it is not visible in a strict @w{ISO C} environment. | 
|  | 696 |  | 
|  | 697 | @item const char *tm_zone | 
|  | 698 | This field is the name for the time zone that was used to compute this | 
|  | 699 | broken-down time value.  Like @code{tm_gmtoff}, this field is a BSD and | 
|  | 700 | GNU extension, and is not visible in a strict @w{ISO C} environment. | 
|  | 701 | @end table | 
|  | 702 | @end deftp | 
|  | 703 |  | 
|  | 704 |  | 
|  | 705 | @comment time.h | 
|  | 706 | @comment ISO | 
|  | 707 | @deftypefun {struct tm *} localtime (const time_t *@var{time}) | 
|  | 708 | @safety{@prelim{}@mtunsafe{@mtasurace{:tmbuf} @mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}} | 
|  | 709 | @c Calls tz_convert with a static buffer. | 
|  | 710 | @c localtime @mtasurace:tmbuf @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 711 | @c  tz_convert dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 712 | The @code{localtime} function converts the simple time pointed to by | 
|  | 713 | @var{time} to broken-down time representation, expressed relative to the | 
|  | 714 | user's specified time zone. | 
|  | 715 |  | 
|  | 716 | The return value is a pointer to a static broken-down time structure, which | 
|  | 717 | might be overwritten by subsequent calls to @code{ctime}, @code{gmtime}, | 
|  | 718 | or @code{localtime}.  (But no other library function overwrites the contents | 
|  | 719 | of this object.) | 
|  | 720 |  | 
|  | 721 | The return value is the null pointer if @var{time} cannot be represented | 
|  | 722 | as a broken-down time; typically this is because the year cannot fit into | 
|  | 723 | an @code{int}. | 
|  | 724 |  | 
|  | 725 | Calling @code{localtime} also sets the current time zone as if | 
|  | 726 | @code{tzset} were called.  @xref{Time Zone Functions}. | 
|  | 727 | @end deftypefun | 
|  | 728 |  | 
|  | 729 | Using the @code{localtime} function is a big problem in multi-threaded | 
|  | 730 | programs.  The result is returned in a static buffer and this is used in | 
|  | 731 | all threads.  POSIX.1c introduced a variant of this function. | 
|  | 732 |  | 
|  | 733 | @comment time.h | 
|  | 734 | @comment POSIX.1c | 
|  | 735 | @deftypefun {struct tm *} localtime_r (const time_t *@var{time}, struct tm *@var{resultp}) | 
|  | 736 | @safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}} | 
|  | 737 | @c localtime_r @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 738 | @c  tz_convert(use_localtime) @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 739 | @c   libc_lock_lock dup @asulock @aculock | 
|  | 740 | @c   tzset_internal @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 741 | @c     always called with tzset_lock held | 
|  | 742 | @c     sets static is_initialized before initialization; | 
|  | 743 | @c     reads and sets old_tz; sets tz_rules. | 
|  | 744 | @c     some of the issues only apply on the first call. | 
|  | 745 | @c     subsequent calls only trigger these when called by localtime; | 
|  | 746 | @c     otherwise, they're ok. | 
|  | 747 | @c    getenv dup @mtsenv | 
|  | 748 | @c    strcmp dup ok | 
|  | 749 | @c    strdup @ascuheap | 
|  | 750 | @c    tzfile_read @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 751 | @c     memcmp dup ok | 
|  | 752 | @c     strstr dup ok | 
|  | 753 | @c     getenv dup @mtsenv | 
|  | 754 | @c     asprintf dup @mtslocale @ascuheap @acsmem | 
|  | 755 | @c     stat64 dup ok | 
|  | 756 | @c     fopen dup @ascuheap @asulock @acsmem @acsfd @aculock | 
|  | 757 | @c     fileno dup ok | 
|  | 758 | @c     fstat64 dup ok | 
|  | 759 | @c     fclose dup @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 760 | @c     free dup @ascuheap @acsmem | 
|  | 761 | @c     fsetlocking dup ok [no @mtasurace:stream @asulock, exclusive] | 
|  | 762 | @c     fread_unlocked dup ok [no @mtasurace:stream @asucorrupt @acucorrupt] | 
|  | 763 | @c     memcpy dup ok | 
|  | 764 | @c     decode ok | 
|  | 765 | @c      bswap_32 dup ok | 
|  | 766 | @c     fseek dup ok [no @mtasurace:stream @asucorrupt @acucorrupt] | 
|  | 767 | @c     ftello dup ok [no @mtasurace:stream @asucorrupt @acucorrupt] | 
|  | 768 | @c     malloc dup @ascuheap @acsmem | 
|  | 769 | @c     decode64 ok | 
|  | 770 | @c      bswap_64 dup ok | 
|  | 771 | @c     getc_unlocked ok [no @mtasurace:stream @asucorrupt @acucorrupt] | 
|  | 772 | @c     tzstring dup @ascuheap @acsmem | 
|  | 773 | @c     compute_tzname_max dup ok [guarded by tzset_lock] | 
|  | 774 | @c    memset dup ok | 
|  | 775 | @c    update_vars ok [guarded by tzset_lock] | 
|  | 776 | @c      sets daylight, timezone, tzname and tzname_cur_max; | 
|  | 777 | @c      called only with tzset_lock held, unless tzset_parse_tz | 
|  | 778 | @c      (internal, but not static) gets called by users; given the its | 
|  | 779 | @c      double-underscore-prefixed name, this interface violation could | 
|  | 780 | @c      be regarded as undefined behavior. | 
|  | 781 | @c     strlen ok | 
|  | 782 | @c    tzset_parse_tz @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 783 | @c     sscanf dup @mtslocale @ascuheap @acsmem | 
|  | 784 | @c     isalnum dup @mtsenv | 
|  | 785 | @c     tzstring @ascuheap @acsmem | 
|  | 786 | @c       reads and changes tzstring_list without synchronization, but | 
|  | 787 | @c       only called with tzset_lock held (save for interface violations) | 
|  | 788 | @c      strlen dup ok | 
|  | 789 | @c      malloc dup @ascuheap @acsmem | 
|  | 790 | @c      strcpy dup ok | 
|  | 791 | @c     isdigit dup @mtslocale | 
|  | 792 | @c     compute_offset ok | 
|  | 793 | @c     tzfile_default @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 794 | @c       sets tzname, timezone, types, zone_names, rule_*off, etc; no guards | 
|  | 795 | @c      strlen dup ok | 
|  | 796 | @c      tzfile_read dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 797 | @c      mempcpy dup ok | 
|  | 798 | @c      compute_tzname_max ok [if guarded by tzset_lock] | 
|  | 799 | @c        iterates over zone_names; no guards | 
|  | 800 | @c     free dup @ascuheap @acsmem | 
|  | 801 | @c     strtoul dup @mtslocale | 
|  | 802 | @c     update_vars dup ok | 
|  | 803 | @c   tzfile_compute(use_localtime) @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 804 | @c     sets tzname; no guards.  with !use_localtime, as in gmtime, it's ok | 
|  | 805 | @c    tzstring dup @acsuheap @acsmem | 
|  | 806 | @c    tzset_parse_tz dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 807 | @c    offtime dup ok | 
|  | 808 | @c    tz_compute dup ok | 
|  | 809 | @c    strcmp dup ok | 
|  | 810 | @c   offtime ok | 
|  | 811 | @c    isleap dup ok | 
|  | 812 | @c   tz_compute ok | 
|  | 813 | @c    compute_change ok | 
|  | 814 | @c     isleap ok | 
|  | 815 | @c   libc_lock_unlock dup @aculock | 
|  | 816 |  | 
|  | 817 | The @code{localtime_r} function works just like the @code{localtime} | 
|  | 818 | function.  It takes a pointer to a variable containing a simple time | 
|  | 819 | and converts it to the broken-down time format. | 
|  | 820 |  | 
|  | 821 | But the result is not placed in a static buffer.  Instead it is placed | 
|  | 822 | in the object of type @code{struct tm} to which the parameter | 
|  | 823 | @var{resultp} points. | 
|  | 824 |  | 
|  | 825 | If the conversion is successful the function returns a pointer to the | 
|  | 826 | object the result was written into, i.e., it returns @var{resultp}. | 
|  | 827 | @end deftypefun | 
|  | 828 |  | 
|  | 829 |  | 
|  | 830 | @comment time.h | 
|  | 831 | @comment ISO | 
|  | 832 | @deftypefun {struct tm *} gmtime (const time_t *@var{time}) | 
|  | 833 | @safety{@prelim{}@mtunsafe{@mtasurace{:tmbuf} @mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}} | 
|  | 834 | @c gmtime @mtasurace:tmbuf @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 835 | @c  tz_convert dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 836 | This function is similar to @code{localtime}, except that the broken-down | 
|  | 837 | time is expressed as Coordinated Universal Time (UTC) (formerly called | 
|  | 838 | Greenwich Mean Time (GMT)) rather than relative to a local time zone. | 
|  | 839 |  | 
|  | 840 | @end deftypefun | 
|  | 841 |  | 
|  | 842 | As for the @code{localtime} function we have the problem that the result | 
|  | 843 | is placed in a static variable.  POSIX.1c also provides a replacement for | 
|  | 844 | @code{gmtime}. | 
|  | 845 |  | 
|  | 846 | @comment time.h | 
|  | 847 | @comment POSIX.1c | 
|  | 848 | @deftypefun {struct tm *} gmtime_r (const time_t *@var{time}, struct tm *@var{resultp}) | 
|  | 849 | @safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}} | 
|  | 850 | @c You'd think tz_convert could avoid some safety issues with | 
|  | 851 | @c !use_localtime, but no such luck: tzset_internal will always bring | 
|  | 852 | @c about all possible AS and AC problems when it's first called. | 
|  | 853 | @c Calling any of localtime,gmtime_r once would run the initialization | 
|  | 854 | @c and avoid the heap, mem and fd issues in gmtime* in subsequent calls, | 
|  | 855 | @c but the unsafe locking would remain. | 
|  | 856 | @c gmtime_r @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 857 | @c  tz_convert(gmtime_r) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 858 | This function is similar to @code{localtime_r}, except that it converts | 
|  | 859 | just like @code{gmtime} the given time as Coordinated Universal Time. | 
|  | 860 |  | 
|  | 861 | If the conversion is successful the function returns a pointer to the | 
|  | 862 | object the result was written into, i.e., it returns @var{resultp}. | 
|  | 863 | @end deftypefun | 
|  | 864 |  | 
|  | 865 |  | 
|  | 866 | @comment time.h | 
|  | 867 | @comment ISO | 
|  | 868 | @deftypefun time_t mktime (struct tm *@var{brokentime}) | 
|  | 869 | @safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}} | 
|  | 870 | @c mktime @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 871 | @c   passes a static localtime_offset to mktime_internal; it is read | 
|  | 872 | @c   once, used as an initial guess, and updated at the end, but not | 
|  | 873 | @c   used except as a guess for subsequent calls, so it should be safe. | 
|  | 874 | @c   Even though a compiler might delay the load and perform it multiple | 
|  | 875 | @c   times (bug 16346), there are at least two unconditional uses of the | 
|  | 876 | @c   auto variable in which the first load is stored, separated by a | 
|  | 877 | @c   call to an external function, and a conditional change of the | 
|  | 878 | @c   variable before the external call, so refraining from allocating a | 
|  | 879 | @c   local variable at the first load would be a very bad optimization. | 
|  | 880 | @c  tzset dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 881 | @c  mktime_internal(localtime_r) @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 882 | @c   ydhms_diff ok | 
|  | 883 | @c   ranged_convert(localtime_r) @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 884 | @c    *convert = localtime_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 885 | @c    time_t_avg dup ok | 
|  | 886 | @c   guess_time_tm dup ok | 
|  | 887 | @c    ydhms_diff dup ok | 
|  | 888 | @c    time_t_add_ok ok | 
|  | 889 | @c     time_t_avg ok | 
|  | 890 | @c   isdst_differ ok | 
|  | 891 | @c   time_t_int_add_ok ok | 
|  | 892 | The @code{mktime} function converts a broken-down time structure to a | 
|  | 893 | simple time representation.  It also normalizes the contents of the | 
|  | 894 | broken-down time structure, and fills in some components based on the | 
|  | 895 | values of the others. | 
|  | 896 |  | 
|  | 897 | The @code{mktime} function ignores the specified contents of the | 
|  | 898 | @code{tm_wday}, @code{tm_yday}, @code{tm_gmtoff}, and @code{tm_zone} | 
|  | 899 | members of the broken-down time | 
|  | 900 | structure.  It uses the values of the other components to determine the | 
|  | 901 | calendar time; it's permissible for these components to have | 
|  | 902 | unnormalized values outside their normal ranges.  The last thing that | 
|  | 903 | @code{mktime} does is adjust the components of the @var{brokentime} | 
|  | 904 | structure, including the members that were initially ignored. | 
|  | 905 |  | 
|  | 906 | If the specified broken-down time cannot be represented as a simple time, | 
|  | 907 | @code{mktime} returns a value of @code{(time_t)(-1)} and does not modify | 
|  | 908 | the contents of @var{brokentime}. | 
|  | 909 |  | 
|  | 910 | Calling @code{mktime} also sets the current time zone as if | 
|  | 911 | @code{tzset} were called; @code{mktime} uses this information instead | 
|  | 912 | of @var{brokentime}'s initial @code{tm_gmtoff} and @code{tm_zone} | 
|  | 913 | members.  @xref{Time Zone Functions}. | 
|  | 914 | @end deftypefun | 
|  | 915 |  | 
|  | 916 | @comment time.h | 
|  | 917 | @comment ??? | 
|  | 918 | @deftypefun time_t timelocal (struct tm *@var{brokentime}) | 
|  | 919 | @safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}} | 
|  | 920 | @c Alias to mktime. | 
|  | 921 |  | 
|  | 922 | @code{timelocal} is functionally identical to @code{mktime}, but more | 
|  | 923 | mnemonically named.  Note that it is the inverse of the @code{localtime} | 
|  | 924 | function. | 
|  | 925 |  | 
|  | 926 | @strong{Portability note:}  @code{mktime} is essentially universally | 
|  | 927 | available.  @code{timelocal} is rather rare. | 
|  | 928 |  | 
|  | 929 | @end deftypefun | 
|  | 930 |  | 
|  | 931 | @comment time.h | 
|  | 932 | @comment ??? | 
|  | 933 | @deftypefun time_t timegm (struct tm *@var{brokentime}) | 
|  | 934 | @safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}} | 
|  | 935 | @c timegm @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 936 | @c   gmtime_offset triggers the same caveats as localtime_offset in mktime. | 
|  | 937 | @c   although gmtime_r, as called by mktime, might save some issues, | 
|  | 938 | @c   tzset calls tzset_internal with always, which forces | 
|  | 939 | @c   reinitialization, so all issues may arise. | 
|  | 940 | @c  tzset dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 941 | @c  mktime_internal(gmtime_r) @asulock @aculock | 
|  | 942 | @c ..gmtime_r @asulock @aculock | 
|  | 943 | @c    ... dup ok | 
|  | 944 | @c    tz_convert(!use_localtime) @asulock @aculock | 
|  | 945 | @c     ... dup @asulock @aculock | 
|  | 946 | @c     tzfile_compute(!use_localtime) ok | 
|  | 947 |  | 
|  | 948 | @code{timegm} is functionally identical to @code{mktime} except it | 
|  | 949 | always takes the input values to be Coordinated Universal Time (UTC) | 
|  | 950 | regardless of any local time zone setting. | 
|  | 951 |  | 
|  | 952 | Note that @code{timegm} is the inverse of @code{gmtime}. | 
|  | 953 |  | 
|  | 954 | @strong{Portability note:}  @code{mktime} is essentially universally | 
|  | 955 | available.  @code{timegm} is rather rare.  For the most portable | 
|  | 956 | conversion from a UTC broken-down time to a simple time, set | 
|  | 957 | the @code{TZ} environment variable to UTC, call @code{mktime}, then set | 
|  | 958 | @code{TZ} back. | 
|  | 959 |  | 
|  | 960 | @end deftypefun | 
|  | 961 |  | 
|  | 962 |  | 
|  | 963 |  | 
|  | 964 | @node High Accuracy Clock | 
|  | 965 | @subsection High Accuracy Clock | 
|  | 966 |  | 
|  | 967 | @cindex time, high precision | 
|  | 968 | @cindex clock, high accuracy | 
|  | 969 | @pindex sys/timex.h | 
|  | 970 | @c On Linux, GNU libc implements ntp_gettime() and npt_adjtime() as calls | 
|  | 971 | @c to adjtimex(). | 
|  | 972 | The @code{ntp_gettime} and @code{ntp_adjtime} functions provide an | 
|  | 973 | interface to monitor and manipulate the system clock to maintain high | 
|  | 974 | accuracy time.  For example, you can fine tune the speed of the clock | 
|  | 975 | or synchronize it with another time source. | 
|  | 976 |  | 
|  | 977 | A typical use of these functions is by a server implementing the Network | 
|  | 978 | Time Protocol to synchronize the clocks of multiple systems and high | 
|  | 979 | precision clocks. | 
|  | 980 |  | 
|  | 981 | These functions are declared in @file{sys/timex.h}. | 
|  | 982 |  | 
|  | 983 | @tindex struct ntptimeval | 
|  | 984 | @deftp {Data Type} {struct ntptimeval} | 
|  | 985 | This structure is used for information about the system clock.  It | 
|  | 986 | contains the following members: | 
|  | 987 | @table @code | 
|  | 988 | @item struct timeval time | 
|  | 989 | This is the current calendar time, expressed as the elapsed time since | 
|  | 990 | the epoch.  The @code{struct timeval} data type is described in | 
|  | 991 | @ref{Elapsed Time}. | 
|  | 992 |  | 
|  | 993 | @item long int maxerror | 
|  | 994 | This is the maximum error, measured in microseconds.  Unless updated | 
|  | 995 | via @code{ntp_adjtime} periodically, this value will reach some | 
|  | 996 | platform-specific maximum value. | 
|  | 997 |  | 
|  | 998 | @item long int esterror | 
|  | 999 | This is the estimated error, measured in microseconds.  This value can | 
|  | 1000 | be set by @code{ntp_adjtime} to indicate the estimated offset of the | 
|  | 1001 | system clock from the true calendar time. | 
|  | 1002 | @end table | 
|  | 1003 | @end deftp | 
|  | 1004 |  | 
|  | 1005 | @comment sys/timex.h | 
|  | 1006 | @comment GNU | 
|  | 1007 | @deftypefun int ntp_gettime (struct ntptimeval *@var{tptr}) | 
|  | 1008 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | 
|  | 1009 | @c Wrapper for adjtimex. | 
|  | 1010 | The @code{ntp_gettime} function sets the structure pointed to by | 
|  | 1011 | @var{tptr} to current values.  The elements of the structure afterwards | 
|  | 1012 | contain the values the timer implementation in the kernel assumes.  They | 
|  | 1013 | might or might not be correct.  If they are not a @code{ntp_adjtime} | 
|  | 1014 | call is necessary. | 
|  | 1015 |  | 
|  | 1016 | The return value is @code{0} on success and other values on failure.  The | 
|  | 1017 | following @code{errno} error conditions are defined for this function: | 
|  | 1018 |  | 
|  | 1019 | @table @code | 
|  | 1020 | @item TIME_ERROR | 
|  | 1021 | The precision clock model is not properly set up at the moment, thus the | 
|  | 1022 | clock must be considered unsynchronized, and the values should be | 
|  | 1023 | treated with care. | 
|  | 1024 | @end table | 
|  | 1025 | @end deftypefun | 
|  | 1026 |  | 
|  | 1027 | @tindex struct timex | 
|  | 1028 | @deftp {Data Type} {struct timex} | 
|  | 1029 | This structure is used to control and monitor the system clock.  It | 
|  | 1030 | contains the following members: | 
|  | 1031 | @table @code | 
|  | 1032 | @item unsigned int modes | 
|  | 1033 | This variable controls whether and which values are set.  Several | 
|  | 1034 | symbolic constants have to be combined with @emph{binary or} to specify | 
|  | 1035 | the effective mode.  These constants start with @code{MOD_}. | 
|  | 1036 |  | 
|  | 1037 | @item long int offset | 
|  | 1038 | This value indicates the current offset of the system clock from the true | 
|  | 1039 | calendar time.  The value is given in microseconds.  If bit | 
|  | 1040 | @code{MOD_OFFSET} is set in @code{modes}, the offset (and possibly other | 
|  | 1041 | dependent values) can be set.  The offset's absolute value must not | 
|  | 1042 | exceed @code{MAXPHASE}. | 
|  | 1043 |  | 
|  | 1044 |  | 
|  | 1045 | @item long int frequency | 
|  | 1046 | This value indicates the difference in frequency between the true | 
|  | 1047 | calendar time and the system clock.  The value is expressed as scaled | 
|  | 1048 | PPM (parts per million, 0.0001%).  The scaling is @code{1 << | 
|  | 1049 | SHIFT_USEC}.  The value can be set with bit @code{MOD_FREQUENCY}, but | 
|  | 1050 | the absolute value must not exceed @code{MAXFREQ}. | 
|  | 1051 |  | 
|  | 1052 | @item long int maxerror | 
|  | 1053 | This is the maximum error, measured in microseconds.  A new value can be | 
|  | 1054 | set using bit @code{MOD_MAXERROR}.  Unless updated via | 
|  | 1055 | @code{ntp_adjtime} periodically, this value will increase steadily | 
|  | 1056 | and reach some platform-specific maximum value. | 
|  | 1057 |  | 
|  | 1058 | @item long int esterror | 
|  | 1059 | This is the estimated error, measured in microseconds.  This value can | 
|  | 1060 | be set using bit @code{MOD_ESTERROR}. | 
|  | 1061 |  | 
|  | 1062 | @item int status | 
|  | 1063 | This variable reflects the various states of the clock machinery.  There | 
|  | 1064 | are symbolic constants for the significant bits, starting with | 
|  | 1065 | @code{STA_}.  Some of these flags can be updated using the | 
|  | 1066 | @code{MOD_STATUS} bit. | 
|  | 1067 |  | 
|  | 1068 | @item long int constant | 
|  | 1069 | This value represents the bandwidth or stiffness of the PLL (phase | 
|  | 1070 | locked loop) implemented in the kernel.  The value can be changed using | 
|  | 1071 | bit @code{MOD_TIMECONST}. | 
|  | 1072 |  | 
|  | 1073 | @item long int precision | 
|  | 1074 | This value represents the accuracy or the maximum error when reading the | 
|  | 1075 | system clock.  The value is expressed in microseconds. | 
|  | 1076 |  | 
|  | 1077 | @item long int tolerance | 
|  | 1078 | This value represents the maximum frequency error of the system clock in | 
|  | 1079 | scaled PPM.  This value is used to increase the @code{maxerror} every | 
|  | 1080 | second. | 
|  | 1081 |  | 
|  | 1082 | @item struct timeval time | 
|  | 1083 | The current calendar time. | 
|  | 1084 |  | 
|  | 1085 | @item long int tick | 
|  | 1086 | The elapsed time between clock ticks in microseconds.  A clock tick is a | 
|  | 1087 | periodic timer interrupt on which the system clock is based. | 
|  | 1088 |  | 
|  | 1089 | @item long int ppsfreq | 
|  | 1090 | This is the first of a few optional variables that are present only if | 
|  | 1091 | the system clock can use a PPS (pulse per second) signal to discipline | 
|  | 1092 | the system clock.  The value is expressed in scaled PPM and it denotes | 
|  | 1093 | the difference in frequency between the system clock and the PPS signal. | 
|  | 1094 |  | 
|  | 1095 | @item long int jitter | 
|  | 1096 | This value expresses a median filtered average of the PPS signal's | 
|  | 1097 | dispersion in microseconds. | 
|  | 1098 |  | 
|  | 1099 | @item int shift | 
|  | 1100 | This value is a binary exponent for the duration of the PPS calibration | 
|  | 1101 | interval, ranging from @code{PPS_SHIFT} to @code{PPS_SHIFTMAX}. | 
|  | 1102 |  | 
|  | 1103 | @item long int stabil | 
|  | 1104 | This value represents the median filtered dispersion of the PPS | 
|  | 1105 | frequency in scaled PPM. | 
|  | 1106 |  | 
|  | 1107 | @item long int jitcnt | 
|  | 1108 | This counter represents the number of pulses where the jitter exceeded | 
|  | 1109 | the allowed maximum @code{MAXTIME}. | 
|  | 1110 |  | 
|  | 1111 | @item long int calcnt | 
|  | 1112 | This counter reflects the number of successful calibration intervals. | 
|  | 1113 |  | 
|  | 1114 | @item long int errcnt | 
|  | 1115 | This counter represents the number of calibration errors (caused by | 
|  | 1116 | large offsets or jitter). | 
|  | 1117 |  | 
|  | 1118 | @item long int stbcnt | 
|  | 1119 | This counter denotes the number of calibrations where the stability | 
|  | 1120 | exceeded the threshold. | 
|  | 1121 | @end table | 
|  | 1122 | @end deftp | 
|  | 1123 |  | 
|  | 1124 | @comment sys/timex.h | 
|  | 1125 | @comment GNU | 
|  | 1126 | @deftypefun int ntp_adjtime (struct timex *@var{tptr}) | 
|  | 1127 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | 
|  | 1128 | @c Alias to adjtimex syscall. | 
|  | 1129 | The @code{ntp_adjtime} function sets the structure specified by | 
|  | 1130 | @var{tptr} to current values. | 
|  | 1131 |  | 
|  | 1132 | In addition, @code{ntp_adjtime} updates some settings to match what you | 
|  | 1133 | pass to it in *@var{tptr}.  Use the @code{modes} element of *@var{tptr} | 
|  | 1134 | to select what settings to update.  You can set @code{offset}, | 
|  | 1135 | @code{freq}, @code{maxerror}, @code{esterror}, @code{status}, | 
|  | 1136 | @code{constant}, and @code{tick}. | 
|  | 1137 |  | 
|  | 1138 | @code{modes} = zero means set nothing. | 
|  | 1139 |  | 
|  | 1140 | Only the superuser can update settings. | 
|  | 1141 |  | 
|  | 1142 | @c On Linux, ntp_adjtime() also does the adjtime() function if you set | 
|  | 1143 | @c modes = ADJ_OFFSET_SINGLESHOT (in fact, that is how GNU libc implements | 
|  | 1144 | @c adjtime()).  But this should be considered an internal function because | 
|  | 1145 | @c it's so inconsistent with the rest of what ntp_adjtime() does and is | 
|  | 1146 | @c forced in an ugly way into the struct timex.  So we don't document it | 
|  | 1147 | @c and instead document adjtime() as the way to achieve the function. | 
|  | 1148 |  | 
|  | 1149 | The return value is @code{0} on success and other values on failure.  The | 
|  | 1150 | following @code{errno} error conditions are defined for this function: | 
|  | 1151 |  | 
|  | 1152 | @table @code | 
|  | 1153 | @item TIME_ERROR | 
|  | 1154 | The high accuracy clock model is not properly set up at the moment, thus the | 
|  | 1155 | clock must be considered unsynchronized, and the values should be | 
|  | 1156 | treated with care.  Another reason could be that the specified new values | 
|  | 1157 | are not allowed. | 
|  | 1158 |  | 
|  | 1159 | @item EPERM | 
|  | 1160 | The process specified a settings update, but is not superuser. | 
|  | 1161 |  | 
|  | 1162 | @end table | 
|  | 1163 |  | 
|  | 1164 | For more details see RFC1305 (Network Time Protocol, Version 3) and | 
|  | 1165 | related documents. | 
|  | 1166 |  | 
|  | 1167 | @strong{Portability note:} Early versions of @theglibc{} did not | 
|  | 1168 | have this function but did have the synonymous @code{adjtimex}. | 
|  | 1169 |  | 
|  | 1170 | @end deftypefun | 
|  | 1171 |  | 
|  | 1172 |  | 
|  | 1173 | @node Formatting Calendar Time | 
|  | 1174 | @subsection Formatting Calendar Time | 
|  | 1175 |  | 
|  | 1176 | The functions described in this section format calendar time values as | 
|  | 1177 | strings.  These functions are declared in the header file @file{time.h}. | 
|  | 1178 | @pindex time.h | 
|  | 1179 |  | 
|  | 1180 | @comment time.h | 
|  | 1181 | @comment ISO | 
|  | 1182 | @deftypefun {char *} asctime (const struct tm *@var{brokentime}) | 
|  | 1183 | @safety{@prelim{}@mtunsafe{@mtasurace{:asctime} @mtslocale{}}@asunsafe{}@acsafe{}} | 
|  | 1184 | @c asctime @mtasurace:asctime @mtslocale | 
|  | 1185 | @c   Uses a static buffer. | 
|  | 1186 | @c  asctime_internal @mtslocale | 
|  | 1187 | @c   snprintf dup @mtslocale [no @acsuheap @acsmem] | 
|  | 1188 | @c   ab_day_name @mtslocale | 
|  | 1189 | @c   ab_month_name @mtslocale | 
|  | 1190 | The @code{asctime} function converts the broken-down time value that | 
|  | 1191 | @var{brokentime} points to into a string in a standard format: | 
|  | 1192 |  | 
|  | 1193 | @smallexample | 
|  | 1194 | "Tue May 21 13:46:22 1991\n" | 
|  | 1195 | @end smallexample | 
|  | 1196 |  | 
|  | 1197 | The abbreviations for the days of week are: @samp{Sun}, @samp{Mon}, | 
|  | 1198 | @samp{Tue}, @samp{Wed}, @samp{Thu}, @samp{Fri}, and @samp{Sat}. | 
|  | 1199 |  | 
|  | 1200 | The abbreviations for the months are: @samp{Jan}, @samp{Feb}, | 
|  | 1201 | @samp{Mar}, @samp{Apr}, @samp{May}, @samp{Jun}, @samp{Jul}, @samp{Aug}, | 
|  | 1202 | @samp{Sep}, @samp{Oct}, @samp{Nov}, and @samp{Dec}. | 
|  | 1203 |  | 
|  | 1204 | The return value points to a statically allocated string, which might be | 
|  | 1205 | overwritten by subsequent calls to @code{asctime} or @code{ctime}. | 
|  | 1206 | (But no other library function overwrites the contents of this | 
|  | 1207 | string.) | 
|  | 1208 | @end deftypefun | 
|  | 1209 |  | 
|  | 1210 | @comment time.h | 
|  | 1211 | @comment POSIX.1c | 
|  | 1212 | @deftypefun {char *} asctime_r (const struct tm *@var{brokentime}, char *@var{buffer}) | 
|  | 1213 | @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}} | 
|  | 1214 | @c asctime_r @mtslocale | 
|  | 1215 | @c  asctime_internal dup @mtslocale | 
|  | 1216 | This function is similar to @code{asctime} but instead of placing the | 
|  | 1217 | result in a static buffer it writes the string in the buffer pointed to | 
|  | 1218 | by the parameter @var{buffer}.  This buffer should have room | 
|  | 1219 | for at least 26 bytes, including the terminating null. | 
|  | 1220 |  | 
|  | 1221 | If no error occurred the function returns a pointer to the string the | 
|  | 1222 | result was written into, i.e., it returns @var{buffer}.  Otherwise | 
|  | 1223 | return @code{NULL}. | 
|  | 1224 | @end deftypefun | 
|  | 1225 |  | 
|  | 1226 |  | 
|  | 1227 | @comment time.h | 
|  | 1228 | @comment ISO | 
|  | 1229 | @deftypefun {char *} ctime (const time_t *@var{time}) | 
|  | 1230 | @safety{@prelim{}@mtunsafe{@mtasurace{:tmbuf} @mtasurace{:asctime} @mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}} | 
|  | 1231 | @c ctime @mtasurace:tmbuf @mtasurace:asctime @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 1232 | @c  localtime dup @mtasurace:tmbuf @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 1233 | @c  asctime dup @mtasurace:asctime @mtslocale | 
|  | 1234 | The @code{ctime} function is similar to @code{asctime}, except that you | 
|  | 1235 | specify the calendar time argument as a @code{time_t} simple time value | 
|  | 1236 | rather than in broken-down local time format.  It is equivalent to | 
|  | 1237 |  | 
|  | 1238 | @smallexample | 
|  | 1239 | asctime (localtime (@var{time})) | 
|  | 1240 | @end smallexample | 
|  | 1241 |  | 
|  | 1242 | Calling @code{ctime} also sets the current time zone as if | 
|  | 1243 | @code{tzset} were called.  @xref{Time Zone Functions}. | 
|  | 1244 | @end deftypefun | 
|  | 1245 |  | 
|  | 1246 | @comment time.h | 
|  | 1247 | @comment POSIX.1c | 
|  | 1248 | @deftypefun {char *} ctime_r (const time_t *@var{time}, char *@var{buffer}) | 
|  | 1249 | @safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}} | 
|  | 1250 | @c ctime_r @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 1251 | @c  localtime_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 1252 | @c  asctime_r dup @mtslocale | 
|  | 1253 | This function is similar to @code{ctime}, but places the result in the | 
|  | 1254 | string pointed to by @var{buffer}.  It is equivalent to (written using | 
|  | 1255 | gcc extensions, @pxref{Statement Exprs,,,gcc,Porting and Using gcc}): | 
|  | 1256 |  | 
|  | 1257 | @smallexample | 
|  | 1258 | (@{ struct tm tm; asctime_r (localtime_r (time, &tm), buf); @}) | 
|  | 1259 | @end smallexample | 
|  | 1260 |  | 
|  | 1261 | If no error occurred the function returns a pointer to the string the | 
|  | 1262 | result was written into, i.e., it returns @var{buffer}.  Otherwise | 
|  | 1263 | return @code{NULL}. | 
|  | 1264 | @end deftypefun | 
|  | 1265 |  | 
|  | 1266 |  | 
|  | 1267 | @comment time.h | 
|  | 1268 | @comment ISO | 
|  | 1269 | @deftypefun size_t strftime (char *@var{s}, size_t @var{size}, const char *@var{template}, const struct tm *@var{brokentime}) | 
|  | 1270 | @safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{} @ascudlopen{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{} @acsfd{}}} | 
|  | 1271 | @c strftime @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd | 
|  | 1272 | @c  strftime_l @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd | 
|  | 1273 | @c   strftime_internal @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd | 
|  | 1274 | @c    add ok | 
|  | 1275 | @c     memset_zero dup ok | 
|  | 1276 | @c     memset_space dup ok | 
|  | 1277 | @c    strlen dup ok | 
|  | 1278 | @c    mbrlen @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd [no @mtasurace:mbstate/!ps] | 
|  | 1279 | @c    mbsinit dup ok | 
|  | 1280 | @c    cpy ok | 
|  | 1281 | @c     add dup ok | 
|  | 1282 | @c     memcpy_lowcase ok | 
|  | 1283 | @c      TOLOWER ok | 
|  | 1284 | @c       tolower_l ok | 
|  | 1285 | @c     memcpy_uppcase ok | 
|  | 1286 | @c      TOUPPER ok | 
|  | 1287 | @c       toupper_l ok | 
|  | 1288 | @c     MEMCPY ok | 
|  | 1289 | @c      memcpy dup ok | 
|  | 1290 | @c    ISDIGIT ok | 
|  | 1291 | @c    STRLEN ok | 
|  | 1292 | @c     strlen dup ok | 
|  | 1293 | @c    strftime_internal dup @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd | 
|  | 1294 | @c    TOUPPER dup ok | 
|  | 1295 | @c    nl_get_era_entry @ascuheap @asulock @acsmem @aculock | 
|  | 1296 | @c     nl_init_era_entries @ascuheap @asulock @acsmem @aculock | 
|  | 1297 | @c      libc_rwlock_wrlock dup @asulock @aculock | 
|  | 1298 | @c      malloc dup @ascuheap @acsmem | 
|  | 1299 | @c      memset dup ok | 
|  | 1300 | @c      free dup @ascuheap @acsmem | 
|  | 1301 | @c      realloc dup @ascuheap @acsmem | 
|  | 1302 | @c      memcpy dup ok | 
|  | 1303 | @c      strchr dup ok | 
|  | 1304 | @c      wcschr dup ok | 
|  | 1305 | @c      libc_rwlock_unlock dup @asulock @aculock | 
|  | 1306 | @c     ERA_DATE_CMP ok | 
|  | 1307 | @c    DO_NUMBER ok | 
|  | 1308 | @c    DO_NUMBER_SPACEPAD ok | 
|  | 1309 | @c    nl_get_alt_digit @ascuheap @asulock @acsmem @aculock | 
|  | 1310 | @c     libc_rwlock_wrlock dup @asulock @aculock | 
|  | 1311 | @c     nl_init_alt_digit @ascuheap @acsmem | 
|  | 1312 | @c      malloc dup @ascuheap @acsmem | 
|  | 1313 | @c      memset dup ok | 
|  | 1314 | @c      strchr dup ok | 
|  | 1315 | @c     libc_rwlock_unlock dup @aculock | 
|  | 1316 | @c    memset_space ok | 
|  | 1317 | @c     memset dup ok | 
|  | 1318 | @c    memset_zero ok | 
|  | 1319 | @c     memset dup ok | 
|  | 1320 | @c    mktime dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 1321 | @c    iso_week_days ok | 
|  | 1322 | @c    isleap ok | 
|  | 1323 | @c    tzset dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 1324 | @c    localtime_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 1325 | @c    gmtime_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 1326 | @c    tm_diff ok | 
|  | 1327 | This function is similar to the @code{sprintf} function (@pxref{Formatted | 
|  | 1328 | Input}), but the conversion specifications that can appear in the format | 
|  | 1329 | template @var{template} are specialized for printing components of the date | 
|  | 1330 | and time @var{brokentime} according to the locale currently specified for | 
|  | 1331 | time conversion (@pxref{Locales}) and the current time zone | 
|  | 1332 | (@pxref{Time Zone Functions}). | 
|  | 1333 |  | 
|  | 1334 | Ordinary characters appearing in the @var{template} are copied to the | 
|  | 1335 | output string @var{s}; this can include multibyte character sequences. | 
|  | 1336 | Conversion specifiers are introduced by a @samp{%} character, followed | 
|  | 1337 | by an optional flag which can be one of the following.  These flags | 
|  | 1338 | are all GNU extensions.  The first three affect only the output of | 
|  | 1339 | numbers: | 
|  | 1340 |  | 
|  | 1341 | @table @code | 
|  | 1342 | @item _ | 
|  | 1343 | The number is padded with spaces. | 
|  | 1344 |  | 
|  | 1345 | @item - | 
|  | 1346 | The number is not padded at all. | 
|  | 1347 |  | 
|  | 1348 | @item 0 | 
|  | 1349 | The number is padded with zeros even if the format specifies padding | 
|  | 1350 | with spaces. | 
|  | 1351 |  | 
|  | 1352 | @item ^ | 
|  | 1353 | The output uses uppercase characters, but only if this is possible | 
|  | 1354 | (@pxref{Case Conversion}). | 
|  | 1355 | @end table | 
|  | 1356 |  | 
|  | 1357 | The default action is to pad the number with zeros to keep it a constant | 
|  | 1358 | width.  Numbers that do not have a range indicated below are never | 
|  | 1359 | padded, since there is no natural width for them. | 
|  | 1360 |  | 
|  | 1361 | Following the flag an optional specification of the width is possible. | 
|  | 1362 | This is specified in decimal notation.  If the natural size of the | 
|  | 1363 | output is of the field has less than the specified number of characters, | 
|  | 1364 | the result is written right adjusted and space padded to the given | 
|  | 1365 | size. | 
|  | 1366 |  | 
|  | 1367 | An optional modifier can follow the optional flag and width | 
|  | 1368 | specification.  The modifiers, which were first standardized by | 
|  | 1369 | POSIX.2-1992 and by @w{ISO C99}, are: | 
|  | 1370 |  | 
|  | 1371 | @table @code | 
|  | 1372 | @item E | 
|  | 1373 | Use the locale's alternate representation for date and time.  This | 
|  | 1374 | modifier applies to the @code{%c}, @code{%C}, @code{%x}, @code{%X}, | 
|  | 1375 | @code{%y} and @code{%Y} format specifiers.  In a Japanese locale, for | 
|  | 1376 | example, @code{%Ex} might yield a date format based on the Japanese | 
|  | 1377 | Emperors' reigns. | 
|  | 1378 |  | 
|  | 1379 | @item O | 
|  | 1380 | Use the locale's alternate numeric symbols for numbers.  This modifier | 
|  | 1381 | applies only to numeric format specifiers. | 
|  | 1382 | @end table | 
|  | 1383 |  | 
|  | 1384 | If the format supports the modifier but no alternate representation | 
|  | 1385 | is available, it is ignored. | 
|  | 1386 |  | 
|  | 1387 | The conversion specifier ends with a format specifier taken from the | 
|  | 1388 | following list.  The whole @samp{%} sequence is replaced in the output | 
|  | 1389 | string as follows: | 
|  | 1390 |  | 
|  | 1391 | @table @code | 
|  | 1392 | @item %a | 
|  | 1393 | The abbreviated weekday name according to the current locale. | 
|  | 1394 |  | 
|  | 1395 | @item %A | 
|  | 1396 | The full weekday name according to the current locale. | 
|  | 1397 |  | 
|  | 1398 | @item %b | 
|  | 1399 | The abbreviated month name according to the current locale. | 
|  | 1400 |  | 
|  | 1401 | @item %B | 
|  | 1402 | The full month name according to the current locale. | 
|  | 1403 |  | 
|  | 1404 | Using @code{%B} together with @code{%d} produces grammatically | 
|  | 1405 | incorrect results for some locales. | 
|  | 1406 |  | 
|  | 1407 | @item %c | 
|  | 1408 | The preferred calendar time representation for the current locale. | 
|  | 1409 |  | 
|  | 1410 | @item %C | 
|  | 1411 | The century of the year.  This is equivalent to the greatest integer not | 
|  | 1412 | greater than the year divided by 100. | 
|  | 1413 |  | 
|  | 1414 | This format was first standardized by POSIX.2-1992 and by @w{ISO C99}. | 
|  | 1415 |  | 
|  | 1416 | @item %d | 
|  | 1417 | The day of the month as a decimal number (range @code{01} through @code{31}). | 
|  | 1418 |  | 
|  | 1419 | @item %D | 
|  | 1420 | The date using the format @code{%m/%d/%y}. | 
|  | 1421 |  | 
|  | 1422 | This format was first standardized by POSIX.2-1992 and by @w{ISO C99}. | 
|  | 1423 |  | 
|  | 1424 | @item %e | 
|  | 1425 | The day of the month like with @code{%d}, but padded with blank (range | 
|  | 1426 | @code{ 1} through @code{31}). | 
|  | 1427 |  | 
|  | 1428 | This format was first standardized by POSIX.2-1992 and by @w{ISO C99}. | 
|  | 1429 |  | 
|  | 1430 | @item %F | 
|  | 1431 | The date using the format @code{%Y-%m-%d}.  This is the form specified | 
|  | 1432 | in the @w{ISO 8601} standard and is the preferred form for all uses. | 
|  | 1433 |  | 
|  | 1434 | This format was first standardized by @w{ISO C99} and by POSIX.1-2001. | 
|  | 1435 |  | 
|  | 1436 | @item %g | 
|  | 1437 | The year corresponding to the ISO week number, but without the century | 
|  | 1438 | (range @code{00} through @code{99}).  This has the same format and value | 
|  | 1439 | as @code{%y}, except that if the ISO week number (see @code{%V}) belongs | 
|  | 1440 | to the previous or next year, that year is used instead. | 
|  | 1441 |  | 
|  | 1442 | This format was first standardized by @w{ISO C99} and by POSIX.1-2001. | 
|  | 1443 |  | 
|  | 1444 | @item %G | 
|  | 1445 | The year corresponding to the ISO week number.  This has the same format | 
|  | 1446 | and value as @code{%Y}, except that if the ISO week number (see | 
|  | 1447 | @code{%V}) belongs to the previous or next year, that year is used | 
|  | 1448 | instead. | 
|  | 1449 |  | 
|  | 1450 | This format was first standardized by @w{ISO C99} and by POSIX.1-2001 | 
|  | 1451 | but was previously available as a GNU extension. | 
|  | 1452 |  | 
|  | 1453 | @item %h | 
|  | 1454 | The abbreviated month name according to the current locale.  The action | 
|  | 1455 | is the same as for @code{%b}. | 
|  | 1456 |  | 
|  | 1457 | This format was first standardized by POSIX.2-1992 and by @w{ISO C99}. | 
|  | 1458 |  | 
|  | 1459 | @item %H | 
|  | 1460 | The hour as a decimal number, using a 24-hour clock (range @code{00} through | 
|  | 1461 | @code{23}). | 
|  | 1462 |  | 
|  | 1463 | @item %I | 
|  | 1464 | The hour as a decimal number, using a 12-hour clock (range @code{01} through | 
|  | 1465 | @code{12}). | 
|  | 1466 |  | 
|  | 1467 | @item %j | 
|  | 1468 | The day of the year as a decimal number (range @code{001} through @code{366}). | 
|  | 1469 |  | 
|  | 1470 | @item %k | 
|  | 1471 | The hour as a decimal number, using a 24-hour clock like @code{%H}, but | 
|  | 1472 | padded with blank (range @code{ 0} through @code{23}). | 
|  | 1473 |  | 
|  | 1474 | This format is a GNU extension. | 
|  | 1475 |  | 
|  | 1476 | @item %l | 
|  | 1477 | The hour as a decimal number, using a 12-hour clock like @code{%I}, but | 
|  | 1478 | padded with blank (range @code{ 1} through @code{12}). | 
|  | 1479 |  | 
|  | 1480 | This format is a GNU extension. | 
|  | 1481 |  | 
|  | 1482 | @item %m | 
|  | 1483 | The month as a decimal number (range @code{01} through @code{12}). | 
|  | 1484 |  | 
|  | 1485 | @item %M | 
|  | 1486 | The minute as a decimal number (range @code{00} through @code{59}). | 
|  | 1487 |  | 
|  | 1488 | @item %n | 
|  | 1489 | A single @samp{\n} (newline) character. | 
|  | 1490 |  | 
|  | 1491 | This format was first standardized by POSIX.2-1992 and by @w{ISO C99}. | 
|  | 1492 |  | 
|  | 1493 | @item %p | 
|  | 1494 | Either @samp{AM} or @samp{PM}, according to the given time value; or the | 
|  | 1495 | corresponding strings for the current locale.  Noon is treated as | 
|  | 1496 | @samp{PM} and midnight as @samp{AM}.  In most locales | 
|  | 1497 | @samp{AM}/@samp{PM} format is not supported, in such cases @code{"%p"} | 
|  | 1498 | yields an empty string. | 
|  | 1499 |  | 
|  | 1500 | @ignore | 
|  | 1501 | We currently have a problem with makeinfo.  Write @samp{AM} and @samp{am} | 
|  | 1502 | both results in `am'.  I.e., the difference in case is not visible anymore. | 
|  | 1503 | @end ignore | 
|  | 1504 | @item %P | 
|  | 1505 | Either @samp{am} or @samp{pm}, according to the given time value; or the | 
|  | 1506 | corresponding strings for the current locale, printed in lowercase | 
|  | 1507 | characters.  Noon is treated as @samp{pm} and midnight as @samp{am}.  In | 
|  | 1508 | most locales @samp{AM}/@samp{PM} format is not supported, in such cases | 
|  | 1509 | @code{"%P"} yields an empty string. | 
|  | 1510 |  | 
|  | 1511 | This format is a GNU extension. | 
|  | 1512 |  | 
|  | 1513 | @item %r | 
|  | 1514 | The complete calendar time using the AM/PM format of the current locale. | 
|  | 1515 |  | 
|  | 1516 | This format was first standardized by POSIX.2-1992 and by @w{ISO C99}. | 
|  | 1517 | In the POSIX locale, this format is equivalent to @code{%I:%M:%S %p}. | 
|  | 1518 |  | 
|  | 1519 | @item %R | 
|  | 1520 | The hour and minute in decimal numbers using the format @code{%H:%M}. | 
|  | 1521 |  | 
|  | 1522 | This format was first standardized by @w{ISO C99} and by POSIX.1-2001 | 
|  | 1523 | but was previously available as a GNU extension. | 
|  | 1524 |  | 
|  | 1525 | @item %s | 
|  | 1526 | The number of seconds since the epoch, i.e., since 1970-01-01 00:00:00 UTC. | 
|  | 1527 | Leap seconds are not counted unless leap second support is available. | 
|  | 1528 |  | 
|  | 1529 | This format is a GNU extension. | 
|  | 1530 |  | 
|  | 1531 | @item %S | 
|  | 1532 | The seconds as a decimal number (range @code{00} through @code{60}). | 
|  | 1533 |  | 
|  | 1534 | @item %t | 
|  | 1535 | A single @samp{\t} (tabulator) character. | 
|  | 1536 |  | 
|  | 1537 | This format was first standardized by POSIX.2-1992 and by @w{ISO C99}. | 
|  | 1538 |  | 
|  | 1539 | @item %T | 
|  | 1540 | The time of day using decimal numbers using the format @code{%H:%M:%S}. | 
|  | 1541 |  | 
|  | 1542 | This format was first standardized by POSIX.2-1992 and by @w{ISO C99}. | 
|  | 1543 |  | 
|  | 1544 | @item %u | 
|  | 1545 | The day of the week as a decimal number (range @code{1} through | 
|  | 1546 | @code{7}), Monday being @code{1}. | 
|  | 1547 |  | 
|  | 1548 | This format was first standardized by POSIX.2-1992 and by @w{ISO C99}. | 
|  | 1549 |  | 
|  | 1550 | @item %U | 
|  | 1551 | The week number of the current year as a decimal number (range @code{00} | 
|  | 1552 | through @code{53}), starting with the first Sunday as the first day of | 
|  | 1553 | the first week.  Days preceding the first Sunday in the year are | 
|  | 1554 | considered to be in week @code{00}. | 
|  | 1555 |  | 
|  | 1556 | @item %V | 
|  | 1557 | The @w{ISO 8601:1988} week number as a decimal number (range @code{01} | 
|  | 1558 | through @code{53}).  ISO weeks start with Monday and end with Sunday. | 
|  | 1559 | Week @code{01} of a year is the first week which has the majority of its | 
|  | 1560 | days in that year; this is equivalent to the week containing the year's | 
|  | 1561 | first Thursday, and it is also equivalent to the week containing January | 
|  | 1562 | 4.  Week @code{01} of a year can contain days from the previous year. | 
|  | 1563 | The week before week @code{01} of a year is the last week (@code{52} or | 
|  | 1564 | @code{53}) of the previous year even if it contains days from the new | 
|  | 1565 | year. | 
|  | 1566 |  | 
|  | 1567 | This format was first standardized by POSIX.2-1992 and by @w{ISO C99}. | 
|  | 1568 |  | 
|  | 1569 | @item %w | 
|  | 1570 | The day of the week as a decimal number (range @code{0} through | 
|  | 1571 | @code{6}), Sunday being @code{0}. | 
|  | 1572 |  | 
|  | 1573 | @item %W | 
|  | 1574 | The week number of the current year as a decimal number (range @code{00} | 
|  | 1575 | through @code{53}), starting with the first Monday as the first day of | 
|  | 1576 | the first week.  All days preceding the first Monday in the year are | 
|  | 1577 | considered to be in week @code{00}. | 
|  | 1578 |  | 
|  | 1579 | @item %x | 
|  | 1580 | The preferred date representation for the current locale. | 
|  | 1581 |  | 
|  | 1582 | @item %X | 
|  | 1583 | The preferred time of day representation for the current locale. | 
|  | 1584 |  | 
|  | 1585 | @item %y | 
|  | 1586 | The year without a century as a decimal number (range @code{00} through | 
|  | 1587 | @code{99}).  This is equivalent to the year modulo 100. | 
|  | 1588 |  | 
|  | 1589 | @item %Y | 
|  | 1590 | The year as a decimal number, using the Gregorian calendar.  Years | 
|  | 1591 | before the year @code{1} are numbered @code{0}, @code{-1}, and so on. | 
|  | 1592 |  | 
|  | 1593 | @item %z | 
|  | 1594 | @w{RFC 822}/@w{ISO 8601:1988} style numeric time zone (e.g., | 
|  | 1595 | @code{-0600} or @code{+0100}), or nothing if no time zone is | 
|  | 1596 | determinable. | 
|  | 1597 |  | 
|  | 1598 | This format was first standardized by @w{ISO C99} and by POSIX.1-2001 | 
|  | 1599 | but was previously available as a GNU extension. | 
|  | 1600 |  | 
|  | 1601 | In the POSIX locale, a full @w{RFC 822} timestamp is generated by the format | 
|  | 1602 | @w{@samp{"%a, %d %b %Y %H:%M:%S %z"}} (or the equivalent | 
|  | 1603 | @w{@samp{"%a, %d %b %Y %T %z"}}). | 
|  | 1604 |  | 
|  | 1605 | @item %Z | 
|  | 1606 | The time zone abbreviation (empty if the time zone can't be determined). | 
|  | 1607 |  | 
|  | 1608 | @item %% | 
|  | 1609 | A literal @samp{%} character. | 
|  | 1610 | @end table | 
|  | 1611 |  | 
|  | 1612 | The @var{size} parameter can be used to specify the maximum number of | 
|  | 1613 | characters to be stored in the array @var{s}, including the terminating | 
|  | 1614 | null character.  If the formatted time requires more than @var{size} | 
|  | 1615 | characters, @code{strftime} returns zero and the contents of the array | 
|  | 1616 | @var{s} are undefined.  Otherwise the return value indicates the | 
|  | 1617 | number of characters placed in the array @var{s}, not including the | 
|  | 1618 | terminating null character. | 
|  | 1619 |  | 
|  | 1620 | @emph{Warning:} This convention for the return value which is prescribed | 
|  | 1621 | in @w{ISO C} can lead to problems in some situations.  For certain | 
|  | 1622 | format strings and certain locales the output really can be the empty | 
|  | 1623 | string and this cannot be discovered by testing the return value only. | 
|  | 1624 | E.g., in most locales the AM/PM time format is not supported (most of | 
|  | 1625 | the world uses the 24 hour time representation).  In such locales | 
|  | 1626 | @code{"%p"} will return the empty string, i.e., the return value is | 
|  | 1627 | zero.  To detect situations like this something similar to the following | 
|  | 1628 | code should be used: | 
|  | 1629 |  | 
|  | 1630 | @smallexample | 
|  | 1631 | buf[0] = '\1'; | 
|  | 1632 | len = strftime (buf, bufsize, format, tp); | 
|  | 1633 | if (len == 0 && buf[0] != '\0') | 
|  | 1634 | @{ | 
|  | 1635 | /* Something went wrong in the strftime call.  */ | 
|  | 1636 | @dots{} | 
|  | 1637 | @} | 
|  | 1638 | @end smallexample | 
|  | 1639 |  | 
|  | 1640 | If @var{s} is a null pointer, @code{strftime} does not actually write | 
|  | 1641 | anything, but instead returns the number of characters it would have written. | 
|  | 1642 |  | 
|  | 1643 | Calling @code{strftime} also sets the current time zone as if | 
|  | 1644 | @code{tzset} were called; @code{strftime} uses this information | 
|  | 1645 | instead of @var{brokentime}'s @code{tm_gmtoff} and @code{tm_zone} | 
|  | 1646 | members.  @xref{Time Zone Functions}. | 
|  | 1647 |  | 
|  | 1648 | For an example of @code{strftime}, see @ref{Time Functions Example}. | 
|  | 1649 | @end deftypefun | 
|  | 1650 |  | 
|  | 1651 | @comment time.h | 
|  | 1652 | @comment ISO/Amend1 | 
|  | 1653 | @deftypefun size_t wcsftime (wchar_t *@var{s}, size_t @var{size}, const wchar_t *@var{template}, const struct tm *@var{brokentime}) | 
|  | 1654 | @safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{} @ascudlopen{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{} @acsfd{}}} | 
|  | 1655 | @c wcsftime @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd | 
|  | 1656 | @c  wcsftime_l @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd | 
|  | 1657 | @c   wcsftime_internal @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd | 
|  | 1658 | @c    add ok | 
|  | 1659 | @c     memset_zero dup ok | 
|  | 1660 | @c     memset_space dup ok | 
|  | 1661 | @c    wcslen dup ok | 
|  | 1662 | @c    cpy ok | 
|  | 1663 | @c     add dup ok | 
|  | 1664 | @c     memcpy_lowcase ok | 
|  | 1665 | @c      TOLOWER ok | 
|  | 1666 | @c       towlower_l dup ok | 
|  | 1667 | @c     memcpy_uppcase ok | 
|  | 1668 | @c      TOUPPER ok | 
|  | 1669 | @c       towupper_l dup ok | 
|  | 1670 | @c     MEMCPY ok | 
|  | 1671 | @c      wmemcpy dup ok | 
|  | 1672 | @c    widen @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd | 
|  | 1673 | @c     memset dup ok | 
|  | 1674 | @c     mbsrtowcs_l @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd [no @mtasurace:mbstate/!ps] | 
|  | 1675 | @c    ISDIGIT ok | 
|  | 1676 | @c    STRLEN ok | 
|  | 1677 | @c     wcslen dup ok | 
|  | 1678 | @c    wcsftime_internal dup @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd | 
|  | 1679 | @c    TOUPPER dup ok | 
|  | 1680 | @c    nl_get_era_entry dup @ascuheap @asulock @acsmem @aculock | 
|  | 1681 | @c    DO_NUMBER ok | 
|  | 1682 | @c    DO_NUMBER_SPACEPAD ok | 
|  | 1683 | @c    nl_get_walt_digit dup @ascuheap @asulock @acsmem @aculock | 
|  | 1684 | @c     libc_rwlock_wrlock dup @asulock @aculock | 
|  | 1685 | @c     nl_init_alt_digit dup @ascuheap @acsmem | 
|  | 1686 | @c     malloc dup @ascuheap @acsmem | 
|  | 1687 | @c     memset dup ok | 
|  | 1688 | @c     wcschr dup ok | 
|  | 1689 | @c     libc_rwlock_unlock dup @aculock | 
|  | 1690 | @c    memset_space ok | 
|  | 1691 | @c     wmemset dup ok | 
|  | 1692 | @c    memset_zero ok | 
|  | 1693 | @c     wmemset dup ok | 
|  | 1694 | @c    mktime dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 1695 | @c    iso_week_days ok | 
|  | 1696 | @c    isleap ok | 
|  | 1697 | @c    tzset dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 1698 | @c    localtime_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 1699 | @c    gmtime_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 1700 | @c    tm_diff ok | 
|  | 1701 | The @code{wcsftime} function is equivalent to the @code{strftime} | 
|  | 1702 | function with the difference that it operates on wide character | 
|  | 1703 | strings.  The buffer where the result is stored, pointed to by @var{s}, | 
|  | 1704 | must be an array of wide characters.  The parameter @var{size} which | 
|  | 1705 | specifies the size of the output buffer gives the number of wide | 
|  | 1706 | character, not the number of bytes. | 
|  | 1707 |  | 
|  | 1708 | Also the format string @var{template} is a wide character string.  Since | 
|  | 1709 | all characters needed to specify the format string are in the basic | 
|  | 1710 | character set it is portably possible to write format strings in the C | 
|  | 1711 | source code using the @code{L"@dots{}"} notation.  The parameter | 
|  | 1712 | @var{brokentime} has the same meaning as in the @code{strftime} call. | 
|  | 1713 |  | 
|  | 1714 | The @code{wcsftime} function supports the same flags, modifiers, and | 
|  | 1715 | format specifiers as the @code{strftime} function. | 
|  | 1716 |  | 
|  | 1717 | The return value of @code{wcsftime} is the number of wide characters | 
|  | 1718 | stored in @code{s}.  When more characters would have to be written than | 
|  | 1719 | can be placed in the buffer @var{s} the return value is zero, with the | 
|  | 1720 | same problems indicated in the @code{strftime} documentation. | 
|  | 1721 | @end deftypefun | 
|  | 1722 |  | 
|  | 1723 | @node Parsing Date and Time | 
|  | 1724 | @subsection Convert textual time and date information back | 
|  | 1725 |  | 
|  | 1726 | The @w{ISO C} standard does not specify any functions which can convert | 
|  | 1727 | the output of the @code{strftime} function back into a binary format. | 
|  | 1728 | This led to a variety of more-or-less successful implementations with | 
|  | 1729 | different interfaces over the years.  Then the Unix standard was | 
|  | 1730 | extended by the addition of two functions: @code{strptime} and | 
|  | 1731 | @code{getdate}.  Both have strange interfaces but at least they are | 
|  | 1732 | widely available. | 
|  | 1733 |  | 
|  | 1734 | @menu | 
|  | 1735 | * Low-Level Time String Parsing::  Interpret string according to given format. | 
|  | 1736 | * General Time String Parsing::    User-friendly function to parse data and | 
|  | 1737 | time strings. | 
|  | 1738 | @end menu | 
|  | 1739 |  | 
|  | 1740 | @node Low-Level Time String Parsing | 
|  | 1741 | @subsubsection Interpret string according to given format | 
|  | 1742 |  | 
|  | 1743 | The first function is rather low-level.  It is nevertheless frequently | 
|  | 1744 | used in software since it is better known.  Its interface and | 
|  | 1745 | implementation are heavily influenced by the @code{getdate} function, | 
|  | 1746 | which is defined and implemented in terms of calls to @code{strptime}. | 
|  | 1747 |  | 
|  | 1748 | @comment time.h | 
|  | 1749 | @comment XPG4 | 
|  | 1750 | @deftypefun {char *} strptime (const char *@var{s}, const char *@var{fmt}, struct tm *@var{tp}) | 
|  | 1751 | @safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}} | 
|  | 1752 | @c strptime @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 1753 | @c  strptime_internal @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 1754 | @c   memset dup ok | 
|  | 1755 | @c   ISSPACE ok | 
|  | 1756 | @c    isspace_l dup ok | 
|  | 1757 | @c   match_char ok | 
|  | 1758 | @c   match_string ok | 
|  | 1759 | @c    strlen dup ok | 
|  | 1760 | @c    strncasecmp_l dup ok | 
|  | 1761 | @c   strcmp dup ok | 
|  | 1762 | @c   recursive @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 1763 | @c    strptime_internal dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 1764 | @c   get_number ok | 
|  | 1765 | @c    ISSPACE dup ok | 
|  | 1766 | @c   localtime_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 1767 | @c   nl_select_era_entry @ascuheap @asulock @acsmem @aculock | 
|  | 1768 | @c    nl_init_era_entries dup @ascuheap @asulock @acsmem @aculock | 
|  | 1769 | @c   get_alt_number dup @ascuheap @asulock @acsmem @aculock | 
|  | 1770 | @c    nl_parse_alt_digit dup @ascuheap @asulock @acsmem @aculock | 
|  | 1771 | @c     libc_rwlock_wrlock dup @asulock @aculock | 
|  | 1772 | @c     nl_init_alt_digit dup @ascuheap @acsmem | 
|  | 1773 | @c     libc_rwlock_unlock dup @aculock | 
|  | 1774 | @c    get_number dup ok | 
|  | 1775 | @c   day_of_the_week ok | 
|  | 1776 | @c   day_of_the_year ok | 
|  | 1777 | The @code{strptime} function parses the input string @var{s} according | 
|  | 1778 | to the format string @var{fmt} and stores its results in the | 
|  | 1779 | structure @var{tp}. | 
|  | 1780 |  | 
|  | 1781 | The input string could be generated by a @code{strftime} call or | 
|  | 1782 | obtained any other way.  It does not need to be in a human-recognizable | 
|  | 1783 | format; e.g. a date passed as @code{"02:1999:9"} is acceptable, even | 
|  | 1784 | though it is ambiguous without context.  As long as the format string | 
|  | 1785 | @var{fmt} matches the input string the function will succeed. | 
|  | 1786 |  | 
|  | 1787 | The user has to make sure, though, that the input can be parsed in a | 
|  | 1788 | unambiguous way.  The string @code{"1999112"} can be parsed using the | 
|  | 1789 | format @code{"%Y%m%d"} as 1999-1-12, 1999-11-2, or even 19991-1-2.  It | 
|  | 1790 | is necessary to add appropriate separators to reliably get results. | 
|  | 1791 |  | 
|  | 1792 | The format string consists of the same components as the format string | 
|  | 1793 | of the @code{strftime} function.  The only difference is that the flags | 
|  | 1794 | @code{_}, @code{-}, @code{0}, and @code{^} are not allowed. | 
|  | 1795 | @comment Is this really the intention?  --drepper | 
|  | 1796 | Several of the distinct formats of @code{strftime} do the same work in | 
|  | 1797 | @code{strptime} since differences like case of the input do not matter. | 
|  | 1798 | For reasons of symmetry all formats are supported, though. | 
|  | 1799 |  | 
|  | 1800 | The modifiers @code{E} and @code{O} are also allowed everywhere the | 
|  | 1801 | @code{strftime} function allows them. | 
|  | 1802 |  | 
|  | 1803 | The formats are: | 
|  | 1804 |  | 
|  | 1805 | @table @code | 
|  | 1806 | @item %a | 
|  | 1807 | @itemx %A | 
|  | 1808 | The weekday name according to the current locale, in abbreviated form or | 
|  | 1809 | the full name. | 
|  | 1810 |  | 
|  | 1811 | @item %b | 
|  | 1812 | @itemx %B | 
|  | 1813 | @itemx %h | 
|  | 1814 | The month name according to the current locale, in abbreviated form or | 
|  | 1815 | the full name. | 
|  | 1816 |  | 
|  | 1817 | @item %c | 
|  | 1818 | The date and time representation for the current locale. | 
|  | 1819 |  | 
|  | 1820 | @item %Ec | 
|  | 1821 | Like @code{%c} but the locale's alternative date and time format is used. | 
|  | 1822 |  | 
|  | 1823 | @item %C | 
|  | 1824 | The century of the year. | 
|  | 1825 |  | 
|  | 1826 | It makes sense to use this format only if the format string also | 
|  | 1827 | contains the @code{%y} format. | 
|  | 1828 |  | 
|  | 1829 | @item %EC | 
|  | 1830 | The locale's representation of the period. | 
|  | 1831 |  | 
|  | 1832 | Unlike @code{%C} it sometimes makes sense to use this format since some | 
|  | 1833 | cultures represent years relative to the beginning of eras instead of | 
|  | 1834 | using the Gregorian years. | 
|  | 1835 |  | 
|  | 1836 | @item %d | 
|  | 1837 | @item %e | 
|  | 1838 | The day of the month as a decimal number (range @code{1} through @code{31}). | 
|  | 1839 | Leading zeroes are permitted but not required. | 
|  | 1840 |  | 
|  | 1841 | @item %Od | 
|  | 1842 | @itemx %Oe | 
|  | 1843 | Same as @code{%d} but using the locale's alternative numeric symbols. | 
|  | 1844 |  | 
|  | 1845 | Leading zeroes are permitted but not required. | 
|  | 1846 |  | 
|  | 1847 | @item %D | 
|  | 1848 | Equivalent to @code{%m/%d/%y}. | 
|  | 1849 |  | 
|  | 1850 | @item %F | 
|  | 1851 | Equivalent to @code{%Y-%m-%d}, which is the @w{ISO 8601} date | 
|  | 1852 | format. | 
|  | 1853 |  | 
|  | 1854 | This is a GNU extension following an @w{ISO C99} extension to | 
|  | 1855 | @code{strftime}. | 
|  | 1856 |  | 
|  | 1857 | @item %g | 
|  | 1858 | The year corresponding to the ISO week number, but without the century | 
|  | 1859 | (range @code{00} through @code{99}). | 
|  | 1860 |  | 
|  | 1861 | @emph{Note:} Currently, this is not fully implemented.  The format is | 
|  | 1862 | recognized, input is consumed but no field in @var{tm} is set. | 
|  | 1863 |  | 
|  | 1864 | This format is a GNU extension following a GNU extension of @code{strftime}. | 
|  | 1865 |  | 
|  | 1866 | @item %G | 
|  | 1867 | The year corresponding to the ISO week number. | 
|  | 1868 |  | 
|  | 1869 | @emph{Note:} Currently, this is not fully implemented.  The format is | 
|  | 1870 | recognized, input is consumed but no field in @var{tm} is set. | 
|  | 1871 |  | 
|  | 1872 | This format is a GNU extension following a GNU extension of @code{strftime}. | 
|  | 1873 |  | 
|  | 1874 | @item %H | 
|  | 1875 | @itemx %k | 
|  | 1876 | The hour as a decimal number, using a 24-hour clock (range @code{00} through | 
|  | 1877 | @code{23}). | 
|  | 1878 |  | 
|  | 1879 | @code{%k} is a GNU extension following a GNU extension of @code{strftime}. | 
|  | 1880 |  | 
|  | 1881 | @item %OH | 
|  | 1882 | Same as @code{%H} but using the locale's alternative numeric symbols. | 
|  | 1883 |  | 
|  | 1884 | @item %I | 
|  | 1885 | @itemx %l | 
|  | 1886 | The hour as a decimal number, using a 12-hour clock (range @code{01} through | 
|  | 1887 | @code{12}). | 
|  | 1888 |  | 
|  | 1889 | @code{%l} is a GNU extension following a GNU extension of @code{strftime}. | 
|  | 1890 |  | 
|  | 1891 | @item %OI | 
|  | 1892 | Same as @code{%I} but using the locale's alternative numeric symbols. | 
|  | 1893 |  | 
|  | 1894 | @item %j | 
|  | 1895 | The day of the year as a decimal number (range @code{1} through @code{366}). | 
|  | 1896 |  | 
|  | 1897 | Leading zeroes are permitted but not required. | 
|  | 1898 |  | 
|  | 1899 | @item %m | 
|  | 1900 | The month as a decimal number (range @code{1} through @code{12}). | 
|  | 1901 |  | 
|  | 1902 | Leading zeroes are permitted but not required. | 
|  | 1903 |  | 
|  | 1904 | @item %Om | 
|  | 1905 | Same as @code{%m} but using the locale's alternative numeric symbols. | 
|  | 1906 |  | 
|  | 1907 | @item %M | 
|  | 1908 | The minute as a decimal number (range @code{0} through @code{59}). | 
|  | 1909 |  | 
|  | 1910 | Leading zeroes are permitted but not required. | 
|  | 1911 |  | 
|  | 1912 | @item %OM | 
|  | 1913 | Same as @code{%M} but using the locale's alternative numeric symbols. | 
|  | 1914 |  | 
|  | 1915 | @item %n | 
|  | 1916 | @itemx %t | 
|  | 1917 | Matches any white space. | 
|  | 1918 |  | 
|  | 1919 | @item %p | 
|  | 1920 | @item %P | 
|  | 1921 | The locale-dependent equivalent to @samp{AM} or @samp{PM}. | 
|  | 1922 |  | 
|  | 1923 | This format is not useful unless @code{%I} or @code{%l} is also used. | 
|  | 1924 | Another complication is that the locale might not define these values at | 
|  | 1925 | all and therefore the conversion fails. | 
|  | 1926 |  | 
|  | 1927 | @code{%P} is a GNU extension following a GNU extension to @code{strftime}. | 
|  | 1928 |  | 
|  | 1929 | @item %r | 
|  | 1930 | The complete time using the AM/PM format of the current locale. | 
|  | 1931 |  | 
|  | 1932 | A complication is that the locale might not define this format at all | 
|  | 1933 | and therefore the conversion fails. | 
|  | 1934 |  | 
|  | 1935 | @item %R | 
|  | 1936 | The hour and minute in decimal numbers using the format @code{%H:%M}. | 
|  | 1937 |  | 
|  | 1938 | @code{%R} is a GNU extension following a GNU extension to @code{strftime}. | 
|  | 1939 |  | 
|  | 1940 | @item %s | 
|  | 1941 | The number of seconds since the epoch, i.e., since 1970-01-01 00:00:00 UTC. | 
|  | 1942 | Leap seconds are not counted unless leap second support is available. | 
|  | 1943 |  | 
|  | 1944 | @code{%s} is a GNU extension following a GNU extension to @code{strftime}. | 
|  | 1945 |  | 
|  | 1946 | @item %S | 
|  | 1947 | The seconds as a decimal number (range @code{0} through @code{60}). | 
|  | 1948 |  | 
|  | 1949 | Leading zeroes are permitted but not required. | 
|  | 1950 |  | 
|  | 1951 | @strong{NB:} The Unix specification says the upper bound on this value | 
|  | 1952 | is @code{61}, a result of a decision to allow double leap seconds.  You | 
|  | 1953 | will not see the value @code{61} because no minute has more than one | 
|  | 1954 | leap second, but the myth persists. | 
|  | 1955 |  | 
|  | 1956 | @item %OS | 
|  | 1957 | Same as @code{%S} but using the locale's alternative numeric symbols. | 
|  | 1958 |  | 
|  | 1959 | @item %T | 
|  | 1960 | Equivalent to the use of @code{%H:%M:%S} in this place. | 
|  | 1961 |  | 
|  | 1962 | @item %u | 
|  | 1963 | The day of the week as a decimal number (range @code{1} through | 
|  | 1964 | @code{7}), Monday being @code{1}. | 
|  | 1965 |  | 
|  | 1966 | Leading zeroes are permitted but not required. | 
|  | 1967 |  | 
|  | 1968 | @emph{Note:} Currently, this is not fully implemented.  The format is | 
|  | 1969 | recognized, input is consumed but no field in @var{tm} is set. | 
|  | 1970 |  | 
|  | 1971 | @item %U | 
|  | 1972 | The week number of the current year as a decimal number (range @code{0} | 
|  | 1973 | through @code{53}). | 
|  | 1974 |  | 
|  | 1975 | Leading zeroes are permitted but not required. | 
|  | 1976 |  | 
|  | 1977 | @item %OU | 
|  | 1978 | Same as @code{%U} but using the locale's alternative numeric symbols. | 
|  | 1979 |  | 
|  | 1980 | @item %V | 
|  | 1981 | The @w{ISO 8601:1988} week number as a decimal number (range @code{1} | 
|  | 1982 | through @code{53}). | 
|  | 1983 |  | 
|  | 1984 | Leading zeroes are permitted but not required. | 
|  | 1985 |  | 
|  | 1986 | @emph{Note:} Currently, this is not fully implemented.  The format is | 
|  | 1987 | recognized, input is consumed but no field in @var{tm} is set. | 
|  | 1988 |  | 
|  | 1989 | @item %w | 
|  | 1990 | The day of the week as a decimal number (range @code{0} through | 
|  | 1991 | @code{6}), Sunday being @code{0}. | 
|  | 1992 |  | 
|  | 1993 | Leading zeroes are permitted but not required. | 
|  | 1994 |  | 
|  | 1995 | @emph{Note:} Currently, this is not fully implemented.  The format is | 
|  | 1996 | recognized, input is consumed but no field in @var{tm} is set. | 
|  | 1997 |  | 
|  | 1998 | @item %Ow | 
|  | 1999 | Same as @code{%w} but using the locale's alternative numeric symbols. | 
|  | 2000 |  | 
|  | 2001 | @item %W | 
|  | 2002 | The week number of the current year as a decimal number (range @code{0} | 
|  | 2003 | through @code{53}). | 
|  | 2004 |  | 
|  | 2005 | Leading zeroes are permitted but not required. | 
|  | 2006 |  | 
|  | 2007 | @emph{Note:} Currently, this is not fully implemented.  The format is | 
|  | 2008 | recognized, input is consumed but no field in @var{tm} is set. | 
|  | 2009 |  | 
|  | 2010 | @item %OW | 
|  | 2011 | Same as @code{%W} but using the locale's alternative numeric symbols. | 
|  | 2012 |  | 
|  | 2013 | @item %x | 
|  | 2014 | The date using the locale's date format. | 
|  | 2015 |  | 
|  | 2016 | @item %Ex | 
|  | 2017 | Like @code{%x} but the locale's alternative data representation is used. | 
|  | 2018 |  | 
|  | 2019 | @item %X | 
|  | 2020 | The time using the locale's time format. | 
|  | 2021 |  | 
|  | 2022 | @item %EX | 
|  | 2023 | Like @code{%X} but the locale's alternative time representation is used. | 
|  | 2024 |  | 
|  | 2025 | @item %y | 
|  | 2026 | The year without a century as a decimal number (range @code{0} through | 
|  | 2027 | @code{99}). | 
|  | 2028 |  | 
|  | 2029 | Leading zeroes are permitted but not required. | 
|  | 2030 |  | 
|  | 2031 | Note that it is questionable to use this format without | 
|  | 2032 | the @code{%C} format.  The @code{strptime} function does regard input | 
|  | 2033 | values in the range @math{68} to @math{99} as the years @math{1969} to | 
|  | 2034 | @math{1999} and the values @math{0} to @math{68} as the years | 
|  | 2035 | @math{2000} to @math{2068}.  But maybe this heuristic fails for some | 
|  | 2036 | input data. | 
|  | 2037 |  | 
|  | 2038 | Therefore it is best to avoid @code{%y} completely and use @code{%Y} | 
|  | 2039 | instead. | 
|  | 2040 |  | 
|  | 2041 | @item %Ey | 
|  | 2042 | The offset from @code{%EC} in the locale's alternative representation. | 
|  | 2043 |  | 
|  | 2044 | @item %Oy | 
|  | 2045 | The offset of the year (from @code{%C}) using the locale's alternative | 
|  | 2046 | numeric symbols. | 
|  | 2047 |  | 
|  | 2048 | @item %Y | 
|  | 2049 | The year as a decimal number, using the Gregorian calendar. | 
|  | 2050 |  | 
|  | 2051 | @item %EY | 
|  | 2052 | The full alternative year representation. | 
|  | 2053 |  | 
|  | 2054 | @item %z | 
|  | 2055 | The offset from GMT in @w{ISO 8601}/RFC822 format. | 
|  | 2056 |  | 
|  | 2057 | @item %Z | 
|  | 2058 | The timezone name. | 
|  | 2059 |  | 
|  | 2060 | @emph{Note:} Currently, this is not fully implemented.  The format is | 
|  | 2061 | recognized, input is consumed but no field in @var{tm} is set. | 
|  | 2062 |  | 
|  | 2063 | @item %% | 
|  | 2064 | A literal @samp{%} character. | 
|  | 2065 | @end table | 
|  | 2066 |  | 
|  | 2067 | All other characters in the format string must have a matching character | 
|  | 2068 | in the input string.  Exceptions are white spaces in the input string | 
|  | 2069 | which can match zero or more whitespace characters in the format string. | 
|  | 2070 |  | 
|  | 2071 | @strong{Portability Note:} The XPG standard advises applications to use | 
|  | 2072 | at least one whitespace character (as specified by @code{isspace}) or | 
|  | 2073 | other non-alphanumeric characters between any two conversion | 
|  | 2074 | specifications.  @Theglibc{} does not have this limitation but | 
|  | 2075 | other libraries might have trouble parsing formats like | 
|  | 2076 | @code{"%d%m%Y%H%M%S"}. | 
|  | 2077 |  | 
|  | 2078 | The @code{strptime} function processes the input string from right to | 
|  | 2079 | left.  Each of the three possible input elements (white space, literal, | 
|  | 2080 | or format) are handled one after the other.  If the input cannot be | 
|  | 2081 | matched to the format string the function stops.  The remainder of the | 
|  | 2082 | format and input strings are not processed. | 
|  | 2083 |  | 
|  | 2084 | The function returns a pointer to the first character it was unable to | 
|  | 2085 | process.  If the input string contains more characters than required by | 
|  | 2086 | the format string the return value points right after the last consumed | 
|  | 2087 | input character.  If the whole input string is consumed the return value | 
|  | 2088 | points to the @code{NULL} byte at the end of the string.  If an error | 
|  | 2089 | occurs, i.e., @code{strptime} fails to match all of the format string, | 
|  | 2090 | the function returns @code{NULL}. | 
|  | 2091 | @end deftypefun | 
|  | 2092 |  | 
|  | 2093 | The specification of the function in the XPG standard is rather vague, | 
|  | 2094 | leaving out a few important pieces of information.  Most importantly, it | 
|  | 2095 | does not specify what happens to those elements of @var{tm} which are | 
|  | 2096 | not directly initialized by the different formats.  The | 
|  | 2097 | implementations on different Unix systems vary here. | 
|  | 2098 |  | 
|  | 2099 | The @glibcadj{} implementation does not touch those fields which are not | 
|  | 2100 | directly initialized.  Exceptions are the @code{tm_wday} and | 
|  | 2101 | @code{tm_yday} elements, which are recomputed if any of the year, month, | 
|  | 2102 | or date elements changed.  This has two implications: | 
|  | 2103 |  | 
|  | 2104 | @itemize @bullet | 
|  | 2105 | @item | 
|  | 2106 | Before calling the @code{strptime} function for a new input string, you | 
|  | 2107 | should prepare the @var{tm} structure you pass.  Normally this will mean | 
|  | 2108 | initializing all values are to zero.  Alternatively, you can set all | 
|  | 2109 | fields to values like @code{INT_MAX}, allowing you to determine which | 
|  | 2110 | elements were set by the function call.  Zero does not work here since | 
|  | 2111 | it is a valid value for many of the fields. | 
|  | 2112 |  | 
|  | 2113 | Careful initialization is necessary if you want to find out whether a | 
|  | 2114 | certain field in @var{tm} was initialized by the function call. | 
|  | 2115 |  | 
|  | 2116 | @item | 
|  | 2117 | You can construct a @code{struct tm} value with several consecutive | 
|  | 2118 | @code{strptime} calls.  A useful application of this is e.g. the parsing | 
|  | 2119 | of two separate strings, one containing date information and the other | 
|  | 2120 | time information.  By parsing one after the other without clearing the | 
|  | 2121 | structure in-between, you can construct a complete broken-down time. | 
|  | 2122 | @end itemize | 
|  | 2123 |  | 
|  | 2124 | The following example shows a function which parses a string which is | 
|  | 2125 | contains the date information in either US style or @w{ISO 8601} form: | 
|  | 2126 |  | 
|  | 2127 | @smallexample | 
|  | 2128 | const char * | 
|  | 2129 | parse_date (const char *input, struct tm *tm) | 
|  | 2130 | @{ | 
|  | 2131 | const char *cp; | 
|  | 2132 |  | 
|  | 2133 | /* @r{First clear the result structure.}  */ | 
|  | 2134 | memset (tm, '\0', sizeof (*tm)); | 
|  | 2135 |  | 
|  | 2136 | /* @r{Try the ISO format first.}  */ | 
|  | 2137 | cp = strptime (input, "%F", tm); | 
|  | 2138 | if (cp == NULL) | 
|  | 2139 | @{ | 
|  | 2140 | /* @r{Does not match.  Try the US form.}  */ | 
|  | 2141 | cp = strptime (input, "%D", tm); | 
|  | 2142 | @} | 
|  | 2143 |  | 
|  | 2144 | return cp; | 
|  | 2145 | @} | 
|  | 2146 | @end smallexample | 
|  | 2147 |  | 
|  | 2148 | @node General Time String Parsing | 
|  | 2149 | @subsubsection A More User-friendly Way to Parse Times and Dates | 
|  | 2150 |  | 
|  | 2151 | The Unix standard defines another function for parsing date strings. | 
|  | 2152 | The interface is weird, but if the function happens to suit your | 
|  | 2153 | application it is just fine.  It is problematic to use this function | 
|  | 2154 | in multi-threaded programs or libraries, since it returns a pointer to | 
|  | 2155 | a static variable, and uses a global variable and global state (an | 
|  | 2156 | environment variable). | 
|  | 2157 |  | 
|  | 2158 | @comment time.h | 
|  | 2159 | @comment Unix98 | 
|  | 2160 | @defvar getdate_err | 
|  | 2161 | This variable of type @code{int} contains the error code of the last | 
|  | 2162 | unsuccessful call to @code{getdate}.  Defined values are: | 
|  | 2163 |  | 
|  | 2164 | @table @math | 
|  | 2165 | @item 1 | 
|  | 2166 | The environment variable @code{DATEMSK} is not defined or null. | 
|  | 2167 | @item 2 | 
|  | 2168 | The template file denoted by the @code{DATEMSK} environment variable | 
|  | 2169 | cannot be opened. | 
|  | 2170 | @item 3 | 
|  | 2171 | Information about the template file cannot retrieved. | 
|  | 2172 | @item 4 | 
|  | 2173 | The template file is not a regular file. | 
|  | 2174 | @item 5 | 
|  | 2175 | An I/O error occurred while reading the template file. | 
|  | 2176 | @item 6 | 
|  | 2177 | Not enough memory available to execute the function. | 
|  | 2178 | @item 7 | 
|  | 2179 | The template file contains no matching template. | 
|  | 2180 | @item 8 | 
|  | 2181 | The input date is invalid, but would match a template otherwise.  This | 
|  | 2182 | includes dates like February 31st, and dates which cannot be represented | 
|  | 2183 | in a @code{time_t} variable. | 
|  | 2184 | @end table | 
|  | 2185 | @end defvar | 
|  | 2186 |  | 
|  | 2187 | @comment time.h | 
|  | 2188 | @comment Unix98 | 
|  | 2189 | @deftypefun {struct tm *} getdate (const char *@var{string}) | 
|  | 2190 | @safety{@prelim{}@mtunsafe{@mtasurace{:getdate} @mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}} | 
|  | 2191 | @c getdate @mtasurace:getdate @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 2192 | @c  getdate_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 2193 | The interface to @code{getdate} is the simplest possible for a function | 
|  | 2194 | to parse a string and return the value.  @var{string} is the input | 
|  | 2195 | string and the result is returned in a statically-allocated variable. | 
|  | 2196 |  | 
|  | 2197 | The details about how the string is processed are hidden from the user. | 
|  | 2198 | In fact, they can be outside the control of the program.  Which formats | 
|  | 2199 | are recognized is controlled by the file named by the environment | 
|  | 2200 | variable @code{DATEMSK}.  This file should contain | 
|  | 2201 | lines of valid format strings which could be passed to @code{strptime}. | 
|  | 2202 |  | 
|  | 2203 | The @code{getdate} function reads these format strings one after the | 
|  | 2204 | other and tries to match the input string.  The first line which | 
|  | 2205 | completely matches the input string is used. | 
|  | 2206 |  | 
|  | 2207 | Elements not initialized through the format string retain the values | 
|  | 2208 | present at the time of the @code{getdate} function call. | 
|  | 2209 |  | 
|  | 2210 | The formats recognized by @code{getdate} are the same as for | 
|  | 2211 | @code{strptime}.  See above for an explanation.  There are only a few | 
|  | 2212 | extensions to the @code{strptime} behavior: | 
|  | 2213 |  | 
|  | 2214 | @itemize @bullet | 
|  | 2215 | @item | 
|  | 2216 | If the @code{%Z} format is given the broken-down time is based on the | 
|  | 2217 | current time of the timezone matched, not of the current timezone of the | 
|  | 2218 | runtime environment. | 
|  | 2219 |  | 
|  | 2220 | @emph{Note}: This is not implemented (currently).  The problem is that | 
|  | 2221 | timezone names are not unique.  If a fixed timezone is assumed for a | 
|  | 2222 | given string (say @code{EST} meaning US East Coast time), then uses for | 
|  | 2223 | countries other than the USA will fail.  So far we have found no good | 
|  | 2224 | solution to this. | 
|  | 2225 |  | 
|  | 2226 | @item | 
|  | 2227 | If only the weekday is specified the selected day depends on the current | 
|  | 2228 | date.  If the current weekday is greater or equal to the @code{tm_wday} | 
|  | 2229 | value the current week's day is chosen, otherwise the day next week is chosen. | 
|  | 2230 |  | 
|  | 2231 | @item | 
|  | 2232 | A similar heuristic is used when only the month is given and not the | 
|  | 2233 | year.  If the month is greater than or equal to the current month, then | 
|  | 2234 | the current year is used.  Otherwise it wraps to next year.  The first | 
|  | 2235 | day of the month is assumed if one is not explicitly specified. | 
|  | 2236 |  | 
|  | 2237 | @item | 
|  | 2238 | The current hour, minute, and second are used if the appropriate value is | 
|  | 2239 | not set through the format. | 
|  | 2240 |  | 
|  | 2241 | @item | 
|  | 2242 | If no date is given tomorrow's date is used if the time is | 
|  | 2243 | smaller than the current time.  Otherwise today's date is taken. | 
|  | 2244 | @end itemize | 
|  | 2245 |  | 
|  | 2246 | It should be noted that the format in the template file need not only | 
|  | 2247 | contain format elements.  The following is a list of possible format | 
|  | 2248 | strings (taken from the Unix standard): | 
|  | 2249 |  | 
|  | 2250 | @smallexample | 
|  | 2251 | %m | 
|  | 2252 | %A %B %d, %Y %H:%M:%S | 
|  | 2253 | %A | 
|  | 2254 | %B | 
|  | 2255 | %m/%d/%y %I %p | 
|  | 2256 | %d,%m,%Y %H:%M | 
|  | 2257 | at %A the %dst of %B in %Y | 
|  | 2258 | run job at %I %p,%B %dnd | 
|  | 2259 | %A den %d. %B %Y %H.%M Uhr | 
|  | 2260 | @end smallexample | 
|  | 2261 |  | 
|  | 2262 | As you can see, the template list can contain very specific strings like | 
|  | 2263 | @code{run job at %I %p,%B %dnd}.  Using the above list of templates and | 
|  | 2264 | assuming the current time is Mon Sep 22 12:19:47 EDT 1986 we can obtain the | 
|  | 2265 | following results for the given input. | 
|  | 2266 |  | 
|  | 2267 | @multitable {xxxxxxxxxxxx} {xxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx} | 
|  | 2268 | @item        Input @tab     Match @tab Result | 
|  | 2269 | @item        Mon @tab       %a @tab    Mon Sep 22 12:19:47 EDT 1986 | 
|  | 2270 | @item        Sun @tab       %a @tab    Sun Sep 28 12:19:47 EDT 1986 | 
|  | 2271 | @item        Fri @tab       %a @tab    Fri Sep 26 12:19:47 EDT 1986 | 
|  | 2272 | @item        September @tab %B @tab    Mon Sep 1 12:19:47 EDT 1986 | 
|  | 2273 | @item        January @tab   %B @tab    Thu Jan 1 12:19:47 EST 1987 | 
|  | 2274 | @item        December @tab  %B @tab    Mon Dec 1 12:19:47 EST 1986 | 
|  | 2275 | @item        Sep Mon @tab   %b %a @tab Mon Sep 1 12:19:47 EDT 1986 | 
|  | 2276 | @item        Jan Fri @tab   %b %a @tab Fri Jan 2 12:19:47 EST 1987 | 
|  | 2277 | @item        Dec Mon @tab   %b %a @tab Mon Dec 1 12:19:47 EST 1986 | 
|  | 2278 | @item        Jan Wed 1989 @tab  %b %a %Y @tab Wed Jan 4 12:19:47 EST 1989 | 
|  | 2279 | @item        Fri 9 @tab     %a %H @tab Fri Sep 26 09:00:00 EDT 1986 | 
|  | 2280 | @item        Feb 10:30 @tab %b %H:%S @tab Sun Feb 1 10:00:30 EST 1987 | 
|  | 2281 | @item        10:30 @tab     %H:%M @tab Tue Sep 23 10:30:00 EDT 1986 | 
|  | 2282 | @item        13:30 @tab     %H:%M @tab Mon Sep 22 13:30:00 EDT 1986 | 
|  | 2283 | @end multitable | 
|  | 2284 |  | 
|  | 2285 | The return value of the function is a pointer to a static variable of | 
|  | 2286 | type @w{@code{struct tm}}, or a null pointer if an error occurred.  The | 
|  | 2287 | result is only valid until the next @code{getdate} call, making this | 
|  | 2288 | function unusable in multi-threaded applications. | 
|  | 2289 |  | 
|  | 2290 | The @code{errno} variable is @emph{not} changed.  Error conditions are | 
|  | 2291 | stored in the global variable @code{getdate_err}.  See the | 
|  | 2292 | description above for a list of the possible error values. | 
|  | 2293 |  | 
|  | 2294 | @emph{Warning:} The @code{getdate} function should @emph{never} be | 
|  | 2295 | used in SUID-programs.  The reason is obvious: using the | 
|  | 2296 | @code{DATEMSK} environment variable you can get the function to open | 
|  | 2297 | any arbitrary file and chances are high that with some bogus input | 
|  | 2298 | (such as a binary file) the program will crash. | 
|  | 2299 | @end deftypefun | 
|  | 2300 |  | 
|  | 2301 | @comment time.h | 
|  | 2302 | @comment GNU | 
|  | 2303 | @deftypefun int getdate_r (const char *@var{string}, struct tm *@var{tp}) | 
|  | 2304 | @safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}} | 
|  | 2305 | @c getdate_r @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 2306 | @c  getenv dup @mtsenv | 
|  | 2307 | @c  stat64 dup ok | 
|  | 2308 | @c  access dup ok | 
|  | 2309 | @c  fopen dup @ascuheap @asulock @acsmem @acsfd @aculock | 
|  | 2310 | @c  fsetlocking dup ok [no @mtasurace:stream @asulock, exclusive] | 
|  | 2311 | @c  isspace dup @mtslocale | 
|  | 2312 | @c  strlen dup ok | 
|  | 2313 | @c  malloc dup @ascuheap @acsmem | 
|  | 2314 | @c  fclose dup @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 2315 | @c  memcpy dup ok | 
|  | 2316 | @c  getline dup @ascuheap @acsmem [no @asucorrupt @aculock @acucorrupt, exclusive] | 
|  | 2317 | @c  strptime dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 2318 | @c  feof_unlocked dup ok | 
|  | 2319 | @c  free dup @ascuheap @acsmem | 
|  | 2320 | @c  ferror_unlocked dup dup ok | 
|  | 2321 | @c  time dup ok | 
|  | 2322 | @c  localtime_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 2323 | @c  first_wday @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 2324 | @c   memset dup ok | 
|  | 2325 | @c   mktime dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 2326 | @c  check_mday ok | 
|  | 2327 | @c  mktime dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 2328 | The @code{getdate_r} function is the reentrant counterpart of | 
|  | 2329 | @code{getdate}.  It does not use the global variable @code{getdate_err} | 
|  | 2330 | to signal an error, but instead returns an error code.  The same error | 
|  | 2331 | codes as described in the @code{getdate_err} documentation above are | 
|  | 2332 | used, with 0 meaning success. | 
|  | 2333 |  | 
|  | 2334 | Moreover, @code{getdate_r} stores the broken-down time in the variable | 
|  | 2335 | of type @code{struct tm} pointed to by the second argument, rather than | 
|  | 2336 | in a static variable. | 
|  | 2337 |  | 
|  | 2338 | This function is not defined in the Unix standard.  Nevertheless it is | 
|  | 2339 | available on some other Unix systems as well. | 
|  | 2340 |  | 
|  | 2341 | The warning against using @code{getdate} in SUID-programs applies to | 
|  | 2342 | @code{getdate_r} as well. | 
|  | 2343 | @end deftypefun | 
|  | 2344 |  | 
|  | 2345 | @node TZ Variable | 
|  | 2346 | @subsection Specifying the Time Zone with @code{TZ} | 
|  | 2347 |  | 
|  | 2348 | In POSIX systems, a user can specify the time zone by means of the | 
|  | 2349 | @code{TZ} environment variable.  For information about how to set | 
|  | 2350 | environment variables, see @ref{Environment Variables}.  The functions | 
|  | 2351 | for accessing the time zone are declared in @file{time.h}. | 
|  | 2352 | @pindex time.h | 
|  | 2353 | @cindex time zone | 
|  | 2354 |  | 
|  | 2355 | You should not normally need to set @code{TZ}.  If the system is | 
|  | 2356 | configured properly, the default time zone will be correct.  You might | 
|  | 2357 | set @code{TZ} if you are using a computer over a network from a | 
|  | 2358 | different time zone, and would like times reported to you in the time | 
|  | 2359 | zone local to you, rather than what is local to the computer. | 
|  | 2360 |  | 
|  | 2361 | In POSIX.1 systems the value of the @code{TZ} variable can be in one of | 
|  | 2362 | three formats.  With @theglibc{}, the most common format is the | 
|  | 2363 | last one, which can specify a selection from a large database of time | 
|  | 2364 | zone information for many regions of the world.  The first two formats | 
|  | 2365 | are used to describe the time zone information directly, which is both | 
|  | 2366 | more cumbersome and less precise.  But the POSIX.1 standard only | 
|  | 2367 | specifies the details of the first two formats, so it is good to be | 
|  | 2368 | familiar with them in case you come across a POSIX.1 system that doesn't | 
|  | 2369 | support a time zone information database. | 
|  | 2370 |  | 
|  | 2371 | The first format is used when there is no Daylight Saving Time (or | 
|  | 2372 | summer time) in the local time zone: | 
|  | 2373 |  | 
|  | 2374 | @smallexample | 
|  | 2375 | @r{@var{std} @var{offset}} | 
|  | 2376 | @end smallexample | 
|  | 2377 |  | 
|  | 2378 | The @var{std} string specifies the name of the time zone.  It must be | 
|  | 2379 | three or more characters long and must not contain a leading colon, | 
|  | 2380 | embedded digits, commas, nor plus and minus signs.  There is no space | 
|  | 2381 | character separating the time zone name from the @var{offset}, so these | 
|  | 2382 | restrictions are necessary to parse the specification correctly. | 
|  | 2383 |  | 
|  | 2384 | The @var{offset} specifies the time value you must add to the local time | 
|  | 2385 | to get a Coordinated Universal Time value.  It has syntax like | 
|  | 2386 | [@code{+}|@code{-}]@var{hh}[@code{:}@var{mm}[@code{:}@var{ss}]].  This | 
|  | 2387 | is positive if the local time zone is west of the Prime Meridian and | 
|  | 2388 | negative if it is east.  The hour must be between @code{0} and | 
|  | 2389 | @code{24}, and the minute and seconds between @code{0} and @code{59}. | 
|  | 2390 |  | 
|  | 2391 | For example, here is how we would specify Eastern Standard Time, but | 
|  | 2392 | without any Daylight Saving Time alternative: | 
|  | 2393 |  | 
|  | 2394 | @smallexample | 
|  | 2395 | EST+5 | 
|  | 2396 | @end smallexample | 
|  | 2397 |  | 
|  | 2398 | The second format is used when there is Daylight Saving Time: | 
|  | 2399 |  | 
|  | 2400 | @smallexample | 
|  | 2401 | @r{@var{std} @var{offset} @var{dst} [@var{offset}]@code{,}@var{start}[@code{/}@var{time}]@code{,}@var{end}[@code{/}@var{time}]} | 
|  | 2402 | @end smallexample | 
|  | 2403 |  | 
|  | 2404 | The initial @var{std} and @var{offset} specify the standard time zone, as | 
|  | 2405 | described above.  The @var{dst} string and @var{offset} specify the name | 
|  | 2406 | and offset for the corresponding Daylight Saving Time zone; if the | 
|  | 2407 | @var{offset} is omitted, it defaults to one hour ahead of standard time. | 
|  | 2408 |  | 
|  | 2409 | The remainder of the specification describes when Daylight Saving Time is | 
|  | 2410 | in effect.  The @var{start} field is when Daylight Saving Time goes into | 
|  | 2411 | effect and the @var{end} field is when the change is made back to standard | 
|  | 2412 | time.  The following formats are recognized for these fields: | 
|  | 2413 |  | 
|  | 2414 | @table @code | 
|  | 2415 | @item J@var{n} | 
|  | 2416 | This specifies the Julian day, with @var{n} between @code{1} and @code{365}. | 
|  | 2417 | February 29 is never counted, even in leap years. | 
|  | 2418 |  | 
|  | 2419 | @item @var{n} | 
|  | 2420 | This specifies the Julian day, with @var{n} between @code{0} and @code{365}. | 
|  | 2421 | February 29 is counted in leap years. | 
|  | 2422 |  | 
|  | 2423 | @item M@var{m}.@var{w}.@var{d} | 
|  | 2424 | This specifies day @var{d} of week @var{w} of month @var{m}.  The day | 
|  | 2425 | @var{d} must be between @code{0} (Sunday) and @code{6}.  The week | 
|  | 2426 | @var{w} must be between @code{1} and @code{5}; week @code{1} is the | 
|  | 2427 | first week in which day @var{d} occurs, and week @code{5} specifies the | 
|  | 2428 | @emph{last} @var{d} day in the month.  The month @var{m} should be | 
|  | 2429 | between @code{1} and @code{12}. | 
|  | 2430 | @end table | 
|  | 2431 |  | 
|  | 2432 | The @var{time} fields specify when, in the local time currently in | 
|  | 2433 | effect, the change to the other time occurs.  If omitted, the default is | 
|  | 2434 | @code{02:00:00}.  The hours part of the time fields can range from | 
|  | 2435 | @minus{}167 through 167; this is an extension to POSIX.1, which allows | 
|  | 2436 | only the range 0 through 24. | 
|  | 2437 |  | 
|  | 2438 | Here are some example @code{TZ} values, including the appropriate | 
|  | 2439 | Daylight Saving Time and its dates of applicability.  In North | 
|  | 2440 | American Eastern Standard Time (EST) and Eastern Daylight Time (EDT), | 
|  | 2441 | the normal offset from UTC is 5 hours; since this is | 
|  | 2442 | west of the prime meridian, the sign is positive.  Summer time begins on | 
|  | 2443 | March's second Sunday at 2:00am, and ends on November's first Sunday | 
|  | 2444 | at 2:00am. | 
|  | 2445 |  | 
|  | 2446 | @smallexample | 
|  | 2447 | EST+5EDT,M3.2.0/2,M11.1.0/2 | 
|  | 2448 | @end smallexample | 
|  | 2449 |  | 
|  | 2450 | Israel Standard Time (IST) and Israel Daylight Time (IDT) are 2 hours | 
|  | 2451 | ahead of the prime meridian in winter, springing forward an hour on | 
|  | 2452 | March's fourth Thursday at 26:00 (i.e., 02:00 on the first Friday on or | 
|  | 2453 | after March 23), and falling back on October's last Sunday at 02:00. | 
|  | 2454 |  | 
|  | 2455 | @smallexample | 
|  | 2456 | IST-2IDT,M3.4.4/26,M10.5.0 | 
|  | 2457 | @end smallexample | 
|  | 2458 |  | 
|  | 2459 | Western Argentina Summer Time (WARST) is 3 hours behind the prime | 
|  | 2460 | meridian all year.  There is a dummy fall-back transition on December | 
|  | 2461 | 31 at 25:00 daylight saving time (i.e., 24:00 standard time, | 
|  | 2462 | equivalent to January 1 at 00:00 standard time), and a simultaneous | 
|  | 2463 | spring-forward transition on January 1 at 00:00 standard time, so | 
|  | 2464 | daylight saving time is in effect all year and the initial @code{WART} | 
|  | 2465 | is a placeholder. | 
|  | 2466 |  | 
|  | 2467 | @smallexample | 
|  | 2468 | WART4WARST,J1/0,J365/25 | 
|  | 2469 | @end smallexample | 
|  | 2470 |  | 
|  | 2471 | Western Greenland Time (WGT) and Western Greenland Summer Time (WGST) | 
|  | 2472 | are 3 hours behind UTC in the winter.  Its clocks follow the European | 
|  | 2473 | Union rules of springing forward by one hour on March's last Sunday at | 
|  | 2474 | 01:00 UTC (@minus{}02:00 local time) and falling back on October's | 
|  | 2475 | last Sunday at 01:00 UTC (@minus{}01:00 local time). | 
|  | 2476 |  | 
|  | 2477 | @smallexample | 
|  | 2478 | WGT3WGST,M3.5.0/-2,M10.5.0/-1 | 
|  | 2479 | @end smallexample | 
|  | 2480 |  | 
|  | 2481 | The schedule of Daylight Saving Time in any particular jurisdiction has | 
|  | 2482 | changed over the years.  To be strictly correct, the conversion of dates | 
|  | 2483 | and times in the past should be based on the schedule that was in effect | 
|  | 2484 | then.  However, this format has no facilities to let you specify how the | 
|  | 2485 | schedule has changed from year to year.  The most you can do is specify | 
|  | 2486 | one particular schedule---usually the present day schedule---and this is | 
|  | 2487 | used to convert any date, no matter when.  For precise time zone | 
|  | 2488 | specifications, it is best to use the time zone information database | 
|  | 2489 | (see below). | 
|  | 2490 |  | 
|  | 2491 | The third format looks like this: | 
|  | 2492 |  | 
|  | 2493 | @smallexample | 
|  | 2494 | :@var{characters} | 
|  | 2495 | @end smallexample | 
|  | 2496 |  | 
|  | 2497 | Each operating system interprets this format differently; in | 
|  | 2498 | @theglibc{}, @var{characters} is the name of a file which describes the time | 
|  | 2499 | zone. | 
|  | 2500 |  | 
|  | 2501 | @pindex /etc/localtime | 
|  | 2502 | @pindex localtime | 
|  | 2503 | If the @code{TZ} environment variable does not have a value, the | 
|  | 2504 | operation chooses a time zone by default.  In @theglibc{}, the | 
|  | 2505 | default time zone is like the specification @samp{TZ=:/etc/localtime} | 
|  | 2506 | (or @samp{TZ=:/usr/local/etc/localtime}, depending on how @theglibc{} | 
|  | 2507 | was configured; @pxref{Installation}).  Other C libraries use their own | 
|  | 2508 | rule for choosing the default time zone, so there is little we can say | 
|  | 2509 | about them. | 
|  | 2510 |  | 
|  | 2511 | @cindex time zone database | 
|  | 2512 | @pindex /usr/share/zoneinfo | 
|  | 2513 | @pindex zoneinfo | 
|  | 2514 | If @var{characters} begins with a slash, it is an absolute file name; | 
|  | 2515 | otherwise the library looks for the file | 
|  | 2516 | @w{@file{/usr/share/zoneinfo/@var{characters}}}.  The @file{zoneinfo} | 
|  | 2517 | directory contains data files describing local time zones in many | 
|  | 2518 | different parts of the world.  The names represent major cities, with | 
|  | 2519 | subdirectories for geographical areas; for example, | 
|  | 2520 | @file{America/New_York}, @file{Europe/London}, @file{Asia/Hong_Kong}. | 
|  | 2521 | These data files are installed by the system administrator, who also | 
|  | 2522 | sets @file{/etc/localtime} to point to the data file for the local time | 
|  | 2523 | zone.  The files typically come from the @url{http://www.iana.org/time-zones, | 
|  | 2524 | Time Zone Database} of time zone and daylight saving time | 
|  | 2525 | information for most regions of the world, which is maintained by a | 
|  | 2526 | community of volunteers and put in the public domain. | 
|  | 2527 |  | 
|  | 2528 | @node Time Zone Functions | 
|  | 2529 | @subsection Functions and Variables for Time Zones | 
|  | 2530 |  | 
|  | 2531 | @comment time.h | 
|  | 2532 | @comment POSIX.1 | 
|  | 2533 | @deftypevar {char *} tzname [2] | 
|  | 2534 | The array @code{tzname} contains two strings, which are the standard | 
|  | 2535 | names of the pair of time zones (standard and Daylight | 
|  | 2536 | Saving) that the user has selected.  @code{tzname[0]} is the name of | 
|  | 2537 | the standard time zone (for example, @code{"EST"}), and @code{tzname[1]} | 
|  | 2538 | is the name for the time zone when Daylight Saving Time is in use (for | 
|  | 2539 | example, @code{"EDT"}).  These correspond to the @var{std} and @var{dst} | 
|  | 2540 | strings (respectively) from the @code{TZ} environment variable.  If | 
|  | 2541 | Daylight Saving Time is never used, @code{tzname[1]} is the empty string. | 
|  | 2542 |  | 
|  | 2543 | The @code{tzname} array is initialized from the @code{TZ} environment | 
|  | 2544 | variable whenever @code{tzset}, @code{ctime}, @code{strftime}, | 
|  | 2545 | @code{mktime}, or @code{localtime} is called.  If multiple abbreviations | 
|  | 2546 | have been used (e.g. @code{"EWT"} and @code{"EDT"} for U.S. Eastern War | 
|  | 2547 | Time and Eastern Daylight Time), the array contains the most recent | 
|  | 2548 | abbreviation. | 
|  | 2549 |  | 
|  | 2550 | The @code{tzname} array is required for POSIX.1 compatibility, but in | 
|  | 2551 | GNU programs it is better to use the @code{tm_zone} member of the | 
|  | 2552 | broken-down time structure, since @code{tm_zone} reports the correct | 
|  | 2553 | abbreviation even when it is not the latest one. | 
|  | 2554 |  | 
|  | 2555 | Though the strings are declared as @code{char *} the user must refrain | 
|  | 2556 | from modifying these strings.  Modifying the strings will almost certainly | 
|  | 2557 | lead to trouble. | 
|  | 2558 |  | 
|  | 2559 | @end deftypevar | 
|  | 2560 |  | 
|  | 2561 | @comment time.h | 
|  | 2562 | @comment POSIX.1 | 
|  | 2563 | @deftypefun void tzset (void) | 
|  | 2564 | @safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}} | 
|  | 2565 | @c tzset @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 2566 | @c  libc_lock_lock dup @asulock @aculock | 
|  | 2567 | @c  tzset_internal dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd | 
|  | 2568 | @c  libc_lock_unlock dup @aculock | 
|  | 2569 | The @code{tzset} function initializes the @code{tzname} variable from | 
|  | 2570 | the value of the @code{TZ} environment variable.  It is not usually | 
|  | 2571 | necessary for your program to call this function, because it is called | 
|  | 2572 | automatically when you use the other time conversion functions that | 
|  | 2573 | depend on the time zone. | 
|  | 2574 | @end deftypefun | 
|  | 2575 |  | 
|  | 2576 | The following variables are defined for compatibility with System V | 
|  | 2577 | Unix.  Like @code{tzname}, these variables are set by calling | 
|  | 2578 | @code{tzset} or the other time conversion functions. | 
|  | 2579 |  | 
|  | 2580 | @comment time.h | 
|  | 2581 | @comment SVID | 
|  | 2582 | @deftypevar {long int} timezone | 
|  | 2583 | This contains the difference between UTC and the latest local standard | 
|  | 2584 | time, in seconds west of UTC.  For example, in the U.S. Eastern time | 
|  | 2585 | zone, the value is @code{5*60*60}.  Unlike the @code{tm_gmtoff} member | 
|  | 2586 | of the broken-down time structure, this value is not adjusted for | 
|  | 2587 | daylight saving, and its sign is reversed.  In GNU programs it is better | 
|  | 2588 | to use @code{tm_gmtoff}, since it contains the correct offset even when | 
|  | 2589 | it is not the latest one. | 
|  | 2590 | @end deftypevar | 
|  | 2591 |  | 
|  | 2592 | @comment time.h | 
|  | 2593 | @comment SVID | 
|  | 2594 | @deftypevar int daylight | 
|  | 2595 | This variable has a nonzero value if Daylight Saving Time rules apply. | 
|  | 2596 | A nonzero value does not necessarily mean that Daylight Saving Time is | 
|  | 2597 | now in effect; it means only that Daylight Saving Time is sometimes in | 
|  | 2598 | effect. | 
|  | 2599 | @end deftypevar | 
|  | 2600 |  | 
|  | 2601 | @node Time Functions Example | 
|  | 2602 | @subsection Time Functions Example | 
|  | 2603 |  | 
|  | 2604 | Here is an example program showing the use of some of the calendar time | 
|  | 2605 | functions. | 
|  | 2606 |  | 
|  | 2607 | @smallexample | 
|  | 2608 | @include strftim.c.texi | 
|  | 2609 | @end smallexample | 
|  | 2610 |  | 
|  | 2611 | It produces output like this: | 
|  | 2612 |  | 
|  | 2613 | @smallexample | 
|  | 2614 | Wed Jul 31 13:02:36 1991 | 
|  | 2615 | Today is Wednesday, July 31. | 
|  | 2616 | The time is 01:02 PM. | 
|  | 2617 | @end smallexample | 
|  | 2618 |  | 
|  | 2619 |  | 
|  | 2620 | @node Setting an Alarm | 
|  | 2621 | @section Setting an Alarm | 
|  | 2622 |  | 
|  | 2623 | The @code{alarm} and @code{setitimer} functions provide a mechanism for a | 
|  | 2624 | process to interrupt itself in the future.  They do this by setting a | 
|  | 2625 | timer; when the timer expires, the process receives a signal. | 
|  | 2626 |  | 
|  | 2627 | @cindex setting an alarm | 
|  | 2628 | @cindex interval timer, setting | 
|  | 2629 | @cindex alarms, setting | 
|  | 2630 | @cindex timers, setting | 
|  | 2631 | Each process has three independent interval timers available: | 
|  | 2632 |  | 
|  | 2633 | @itemize @bullet | 
|  | 2634 | @item | 
|  | 2635 | A real-time timer that counts elapsed time.  This timer sends a | 
|  | 2636 | @code{SIGALRM} signal to the process when it expires. | 
|  | 2637 | @cindex real-time timer | 
|  | 2638 | @cindex timer, real-time | 
|  | 2639 |  | 
|  | 2640 | @item | 
|  | 2641 | A virtual timer that counts processor time used by the process.  This timer | 
|  | 2642 | sends a @code{SIGVTALRM} signal to the process when it expires. | 
|  | 2643 | @cindex virtual timer | 
|  | 2644 | @cindex timer, virtual | 
|  | 2645 |  | 
|  | 2646 | @item | 
|  | 2647 | A profiling timer that counts both processor time used by the process, | 
|  | 2648 | and processor time spent in system calls on behalf of the process.  This | 
|  | 2649 | timer sends a @code{SIGPROF} signal to the process when it expires. | 
|  | 2650 | @cindex profiling timer | 
|  | 2651 | @cindex timer, profiling | 
|  | 2652 |  | 
|  | 2653 | This timer is useful for profiling in interpreters.  The interval timer | 
|  | 2654 | mechanism does not have the fine granularity necessary for profiling | 
|  | 2655 | native code. | 
|  | 2656 | @c @xref{profil} !!! | 
|  | 2657 | @end itemize | 
|  | 2658 |  | 
|  | 2659 | You can only have one timer of each kind set at any given time.  If you | 
|  | 2660 | set a timer that has not yet expired, that timer is simply reset to the | 
|  | 2661 | new value. | 
|  | 2662 |  | 
|  | 2663 | You should establish a handler for the appropriate alarm signal using | 
|  | 2664 | @code{signal} or @code{sigaction} before issuing a call to | 
|  | 2665 | @code{setitimer} or @code{alarm}.  Otherwise, an unusual chain of events | 
|  | 2666 | could cause the timer to expire before your program establishes the | 
|  | 2667 | handler.  In this case it would be terminated, since termination is the | 
|  | 2668 | default action for the alarm signals.  @xref{Signal Handling}. | 
|  | 2669 |  | 
|  | 2670 | To be able to use the alarm function to interrupt a system call which | 
|  | 2671 | might block otherwise indefinitely it is important to @emph{not} set the | 
|  | 2672 | @code{SA_RESTART} flag when registering the signal handler using | 
|  | 2673 | @code{sigaction}.  When not using @code{sigaction} things get even | 
|  | 2674 | uglier: the @code{signal} function has to fixed semantics with respect | 
|  | 2675 | to restarts.  The BSD semantics for this function is to set the flag. | 
|  | 2676 | Therefore, if @code{sigaction} for whatever reason cannot be used, it is | 
|  | 2677 | necessary to use @code{sysv_signal} and not @code{signal}. | 
|  | 2678 |  | 
|  | 2679 | The @code{setitimer} function is the primary means for setting an alarm. | 
|  | 2680 | This facility is declared in the header file @file{sys/time.h}.  The | 
|  | 2681 | @code{alarm} function, declared in @file{unistd.h}, provides a somewhat | 
|  | 2682 | simpler interface for setting the real-time timer. | 
|  | 2683 | @pindex unistd.h | 
|  | 2684 | @pindex sys/time.h | 
|  | 2685 |  | 
|  | 2686 | @comment sys/time.h | 
|  | 2687 | @comment BSD | 
|  | 2688 | @deftp {Data Type} {struct itimerval} | 
|  | 2689 | This structure is used to specify when a timer should expire.  It contains | 
|  | 2690 | the following members: | 
|  | 2691 | @table @code | 
|  | 2692 | @item struct timeval it_interval | 
|  | 2693 | This is the period between successive timer interrupts.  If zero, the | 
|  | 2694 | alarm will only be sent once. | 
|  | 2695 |  | 
|  | 2696 | @item struct timeval it_value | 
|  | 2697 | This is the period between now and the first timer interrupt.  If zero, | 
|  | 2698 | the alarm is disabled. | 
|  | 2699 | @end table | 
|  | 2700 |  | 
|  | 2701 | The @code{struct timeval} data type is described in @ref{Elapsed Time}. | 
|  | 2702 | @end deftp | 
|  | 2703 |  | 
|  | 2704 | @comment sys/time.h | 
|  | 2705 | @comment BSD | 
|  | 2706 | @deftypefun int setitimer (int @var{which}, const struct itimerval *@var{new}, struct itimerval *@var{old}) | 
|  | 2707 | @safety{@prelim{}@mtsafe{@mtstimer{}}@assafe{}@acsafe{}} | 
|  | 2708 | @c This function is marked with @mtstimer because the same set of timers | 
|  | 2709 | @c is shared by all threads of a process, so calling it in one thread | 
|  | 2710 | @c may interfere with timers set by another thread.  This interference | 
|  | 2711 | @c is not regarded as destructive, because the interface specification | 
|  | 2712 | @c makes this overriding while returning the previous value the expected | 
|  | 2713 | @c behavior, and the kernel will serialize concurrent calls so that the | 
|  | 2714 | @c last one prevails, with each call getting the timer information from | 
|  | 2715 | @c the timer installed by the previous call in that serialization. | 
|  | 2716 | The @code{setitimer} function sets the timer specified by @var{which} | 
|  | 2717 | according to @var{new}.  The @var{which} argument can have a value of | 
|  | 2718 | @code{ITIMER_REAL}, @code{ITIMER_VIRTUAL}, or @code{ITIMER_PROF}. | 
|  | 2719 |  | 
|  | 2720 | If @var{old} is not a null pointer, @code{setitimer} returns information | 
|  | 2721 | about any previous unexpired timer of the same kind in the structure it | 
|  | 2722 | points to. | 
|  | 2723 |  | 
|  | 2724 | The return value is @code{0} on success and @code{-1} on failure.  The | 
|  | 2725 | following @code{errno} error conditions are defined for this function: | 
|  | 2726 |  | 
|  | 2727 | @table @code | 
|  | 2728 | @item EINVAL | 
|  | 2729 | The timer period is too large. | 
|  | 2730 | @end table | 
|  | 2731 | @end deftypefun | 
|  | 2732 |  | 
|  | 2733 | @comment sys/time.h | 
|  | 2734 | @comment BSD | 
|  | 2735 | @deftypefun int getitimer (int @var{which}, struct itimerval *@var{old}) | 
|  | 2736 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | 
|  | 2737 | The @code{getitimer} function stores information about the timer specified | 
|  | 2738 | by @var{which} in the structure pointed at by @var{old}. | 
|  | 2739 |  | 
|  | 2740 | The return value and error conditions are the same as for @code{setitimer}. | 
|  | 2741 | @end deftypefun | 
|  | 2742 |  | 
|  | 2743 | @comment sys/time.h | 
|  | 2744 | @comment BSD | 
|  | 2745 | @vtable @code | 
|  | 2746 | @item ITIMER_REAL | 
|  | 2747 | This constant can be used as the @var{which} argument to the | 
|  | 2748 | @code{setitimer} and @code{getitimer} functions to specify the real-time | 
|  | 2749 | timer. | 
|  | 2750 |  | 
|  | 2751 | @comment sys/time.h | 
|  | 2752 | @comment BSD | 
|  | 2753 | @item ITIMER_VIRTUAL | 
|  | 2754 | This constant can be used as the @var{which} argument to the | 
|  | 2755 | @code{setitimer} and @code{getitimer} functions to specify the virtual | 
|  | 2756 | timer. | 
|  | 2757 |  | 
|  | 2758 | @comment sys/time.h | 
|  | 2759 | @comment BSD | 
|  | 2760 | @item ITIMER_PROF | 
|  | 2761 | This constant can be used as the @var{which} argument to the | 
|  | 2762 | @code{setitimer} and @code{getitimer} functions to specify the profiling | 
|  | 2763 | timer. | 
|  | 2764 | @end vtable | 
|  | 2765 |  | 
|  | 2766 | @comment unistd.h | 
|  | 2767 | @comment POSIX.1 | 
|  | 2768 | @deftypefun {unsigned int} alarm (unsigned int @var{seconds}) | 
|  | 2769 | @safety{@prelim{}@mtsafe{@mtstimer{}}@assafe{}@acsafe{}} | 
|  | 2770 | @c Wrapper for setitimer. | 
|  | 2771 | The @code{alarm} function sets the real-time timer to expire in | 
|  | 2772 | @var{seconds} seconds.  If you want to cancel any existing alarm, you | 
|  | 2773 | can do this by calling @code{alarm} with a @var{seconds} argument of | 
|  | 2774 | zero. | 
|  | 2775 |  | 
|  | 2776 | The return value indicates how many seconds remain before the previous | 
|  | 2777 | alarm would have been sent.  If there is no previous alarm, @code{alarm} | 
|  | 2778 | returns zero. | 
|  | 2779 | @end deftypefun | 
|  | 2780 |  | 
|  | 2781 | The @code{alarm} function could be defined in terms of @code{setitimer} | 
|  | 2782 | like this: | 
|  | 2783 |  | 
|  | 2784 | @smallexample | 
|  | 2785 | unsigned int | 
|  | 2786 | alarm (unsigned int seconds) | 
|  | 2787 | @{ | 
|  | 2788 | struct itimerval old, new; | 
|  | 2789 | new.it_interval.tv_usec = 0; | 
|  | 2790 | new.it_interval.tv_sec = 0; | 
|  | 2791 | new.it_value.tv_usec = 0; | 
|  | 2792 | new.it_value.tv_sec = (long int) seconds; | 
|  | 2793 | if (setitimer (ITIMER_REAL, &new, &old) < 0) | 
|  | 2794 | return 0; | 
|  | 2795 | else | 
|  | 2796 | return old.it_value.tv_sec; | 
|  | 2797 | @} | 
|  | 2798 | @end smallexample | 
|  | 2799 |  | 
|  | 2800 | There is an example showing the use of the @code{alarm} function in | 
|  | 2801 | @ref{Handler Returns}. | 
|  | 2802 |  | 
|  | 2803 | If you simply want your process to wait for a given number of seconds, | 
|  | 2804 | you should use the @code{sleep} function.  @xref{Sleeping}. | 
|  | 2805 |  | 
|  | 2806 | You shouldn't count on the signal arriving precisely when the timer | 
|  | 2807 | expires.  In a multiprocessing environment there is typically some | 
|  | 2808 | amount of delay involved. | 
|  | 2809 |  | 
|  | 2810 | @strong{Portability Note:} The @code{setitimer} and @code{getitimer} | 
|  | 2811 | functions are derived from BSD Unix, while the @code{alarm} function is | 
|  | 2812 | specified by the POSIX.1 standard.  @code{setitimer} is more powerful than | 
|  | 2813 | @code{alarm}, but @code{alarm} is more widely used. | 
|  | 2814 |  | 
|  | 2815 | @node Sleeping | 
|  | 2816 | @section Sleeping | 
|  | 2817 |  | 
|  | 2818 | The function @code{sleep} gives a simple way to make the program wait | 
|  | 2819 | for a short interval.  If your program doesn't use signals (except to | 
|  | 2820 | terminate), then you can expect @code{sleep} to wait reliably throughout | 
|  | 2821 | the specified interval.  Otherwise, @code{sleep} can return sooner if a | 
|  | 2822 | signal arrives; if you want to wait for a given interval regardless of | 
|  | 2823 | signals, use @code{select} (@pxref{Waiting for I/O}) and don't specify | 
|  | 2824 | any descriptors to wait for. | 
|  | 2825 | @c !!! select can get EINTR; using SA_RESTART makes sleep win too. | 
|  | 2826 |  | 
|  | 2827 | @comment unistd.h | 
|  | 2828 | @comment POSIX.1 | 
|  | 2829 | @deftypefun {unsigned int} sleep (unsigned int @var{seconds}) | 
|  | 2830 | @safety{@prelim{}@mtunsafe{@mtascusig{:SIGCHLD/linux}}@asunsafe{}@acunsafe{}} | 
|  | 2831 | @c On Mach, it uses ports and calls time.  On generic posix, it calls | 
|  | 2832 | @c nanosleep.  On Linux, it temporarily blocks SIGCHLD, which is MT- and | 
|  | 2833 | @c AS-Unsafe, and in a way that makes it AC-Unsafe (C-unsafe, even!). | 
|  | 2834 | The @code{sleep} function waits for @var{seconds} or until a signal | 
|  | 2835 | is delivered, whichever happens first. | 
|  | 2836 |  | 
|  | 2837 | If @code{sleep} function returns because the requested interval is over, | 
|  | 2838 | it returns a value of zero.  If it returns because of delivery of a | 
|  | 2839 | signal, its return value is the remaining time in the sleep interval. | 
|  | 2840 |  | 
|  | 2841 | The @code{sleep} function is declared in @file{unistd.h}. | 
|  | 2842 | @end deftypefun | 
|  | 2843 |  | 
|  | 2844 | Resist the temptation to implement a sleep for a fixed amount of time by | 
|  | 2845 | using the return value of @code{sleep}, when nonzero, to call | 
|  | 2846 | @code{sleep} again.  This will work with a certain amount of accuracy as | 
|  | 2847 | long as signals arrive infrequently.  But each signal can cause the | 
|  | 2848 | eventual wakeup time to be off by an additional second or so.  Suppose a | 
|  | 2849 | few signals happen to arrive in rapid succession by bad luck---there is | 
|  | 2850 | no limit on how much this could shorten or lengthen the wait. | 
|  | 2851 |  | 
|  | 2852 | Instead, compute the calendar time at which the program should stop | 
|  | 2853 | waiting, and keep trying to wait until that calendar time.  This won't | 
|  | 2854 | be off by more than a second.  With just a little more work, you can use | 
|  | 2855 | @code{select} and make the waiting period quite accurate.  (Of course, | 
|  | 2856 | heavy system load can cause additional unavoidable delays---unless the | 
|  | 2857 | machine is dedicated to one application, there is no way you can avoid | 
|  | 2858 | this.) | 
|  | 2859 |  | 
|  | 2860 | On some systems, @code{sleep} can do strange things if your program uses | 
|  | 2861 | @code{SIGALRM} explicitly.  Even if @code{SIGALRM} signals are being | 
|  | 2862 | ignored or blocked when @code{sleep} is called, @code{sleep} might | 
|  | 2863 | return prematurely on delivery of a @code{SIGALRM} signal.  If you have | 
|  | 2864 | established a handler for @code{SIGALRM} signals and a @code{SIGALRM} | 
|  | 2865 | signal is delivered while the process is sleeping, the action taken | 
|  | 2866 | might be just to cause @code{sleep} to return instead of invoking your | 
|  | 2867 | handler.  And, if @code{sleep} is interrupted by delivery of a signal | 
|  | 2868 | whose handler requests an alarm or alters the handling of @code{SIGALRM}, | 
|  | 2869 | this handler and @code{sleep} will interfere. | 
|  | 2870 |  | 
|  | 2871 | On @gnusystems{}, it is safe to use @code{sleep} and @code{SIGALRM} in | 
|  | 2872 | the same program, because @code{sleep} does not work by means of | 
|  | 2873 | @code{SIGALRM}. | 
|  | 2874 |  | 
|  | 2875 | @comment time.h | 
|  | 2876 | @comment POSIX.1 | 
|  | 2877 | @deftypefun int nanosleep (const struct timespec *@var{requested_time}, struct timespec *@var{remaining}) | 
|  | 2878 | @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} | 
|  | 2879 | @c On Linux, it's a syscall.  On Mach, it calls gettimeofday and uses | 
|  | 2880 | @c ports. | 
|  | 2881 | If resolution to seconds is not enough the @code{nanosleep} function can | 
|  | 2882 | be used.  As the name suggests the sleep interval can be specified in | 
|  | 2883 | nanoseconds.  The actual elapsed time of the sleep interval might be | 
|  | 2884 | longer since the system rounds the elapsed time you request up to the | 
|  | 2885 | next integer multiple of the actual resolution the system can deliver. | 
|  | 2886 |  | 
|  | 2887 | *@code{requested_time} is the elapsed time of the interval you want to | 
|  | 2888 | sleep. | 
|  | 2889 |  | 
|  | 2890 | The function returns as *@code{remaining} the elapsed time left in the | 
|  | 2891 | interval for which you requested to sleep.  If the interval completed | 
|  | 2892 | without getting interrupted by a signal, this is zero. | 
|  | 2893 |  | 
|  | 2894 | @code{struct timespec} is described in @xref{Elapsed Time}. | 
|  | 2895 |  | 
|  | 2896 | If the function returns because the interval is over the return value is | 
|  | 2897 | zero.  If the function returns @math{-1} the global variable @var{errno} | 
|  | 2898 | is set to the following values: | 
|  | 2899 |  | 
|  | 2900 | @table @code | 
|  | 2901 | @item EINTR | 
|  | 2902 | The call was interrupted because a signal was delivered to the thread. | 
|  | 2903 | If the @var{remaining} parameter is not the null pointer the structure | 
|  | 2904 | pointed to by @var{remaining} is updated to contain the remaining | 
|  | 2905 | elapsed time. | 
|  | 2906 |  | 
|  | 2907 | @item EINVAL | 
|  | 2908 | The nanosecond value in the @var{requested_time} parameter contains an | 
|  | 2909 | illegal value.  Either the value is negative or greater than or equal to | 
|  | 2910 | 1000 million. | 
|  | 2911 | @end table | 
|  | 2912 |  | 
|  | 2913 | This function is a cancellation point in multi-threaded programs.  This | 
|  | 2914 | is a problem if the thread allocates some resources (like memory, file | 
|  | 2915 | descriptors, semaphores or whatever) at the time @code{nanosleep} is | 
|  | 2916 | called.  If the thread gets canceled these resources stay allocated | 
|  | 2917 | until the program ends.  To avoid this calls to @code{nanosleep} should | 
|  | 2918 | be protected using cancellation handlers. | 
|  | 2919 | @c ref pthread_cleanup_push / pthread_cleanup_pop | 
|  | 2920 |  | 
|  | 2921 | The @code{nanosleep} function is declared in @file{time.h}. | 
|  | 2922 | @end deftypefun |