Import upstream version 1.26
[debian/tar] / doc / parse-datetime.texi
1 @c GNU date syntax documentation
2
3 @c Copyright (C) 1994-2006, 2009-2011 Free Software Foundation, Inc.
4
5 @c Permission is granted to copy, distribute and/or modify this document
6 @c under the terms of the GNU Free Documentation License, Version 1.3 or
7 @c any later version published by the Free Software Foundation; with no
8 @c Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
9 @c Texts.  A copy of the license is included in the ``GNU Free
10 @c Documentation License'' file as part of this distribution.
11
12 @node Date input formats
13 @chapter Date input formats
14
15 @cindex date input formats
16 @findex parse_datetime
17
18 First, a quote:
19
20 @quotation
21 Our units of temporal measurement, from seconds on up to months, are so
22 complicated, asymmetrical and disjunctive so as to make coherent mental
23 reckoning in time all but impossible.  Indeed, had some tyrannical god
24 contrived to enslave our minds to time, to make it all but impossible
25 for us to escape subjection to sodden routines and unpleasant surprises,
26 he could hardly have done better than handing down our present system.
27 It is like a set of trapezoidal building blocks, with no vertical or
28 horizontal surfaces, like a language in which the simplest thought
29 demands ornate constructions, useless particles and lengthy
30 circumlocutions.  Unlike the more successful patterns of language and
31 science, which enable us to face experience boldly or at least
32 level-headedly, our system of temporal calculation silently and
33 persistently encourages our terror of time.
34
35 @dots{}  It is as though architects had to measure length in feet, width
36 in meters and height in ells; as though basic instruction manuals
37 demanded a knowledge of five different languages.  It is no wonder then
38 that we often look into our own immediate past or future, last Tuesday
39 or a week from Sunday, with feelings of helpless confusion.  @dots{}
40
41 --- Robert Grudin, @cite{Time and the Art of Living}.
42 @end quotation
43
44 This section describes the textual date representations that @sc{gnu}
45 programs accept.  These are the strings you, as a user, can supply as
46 arguments to the various programs.  The C interface (via the
47 @code{parse_datetime} function) is not described here.
48
49 @menu
50 * General date syntax::            Common rules.
51 * Calendar date items::            19 Dec 1994.
52 * Time of day items::              9:20pm.
53 * Time zone items::                @sc{est}, @sc{pdt}, @sc{gmt}.
54 * Day of week items::              Monday and others.
55 * Relative items in date strings:: next tuesday, 2 years ago.
56 * Pure numbers in date strings::   19931219, 1440.
57 * Seconds since the Epoch::        @@1078100502.
58 * Specifying time zone rules::     TZ="America/New_York", TZ="UTC0".
59 * Authors of parse_datetime::      Bellovin, Eggert, Salz, Berets, et al.
60 @end menu
61
62
63 @node General date syntax
64 @section General date syntax
65
66 @cindex general date syntax
67
68 @cindex items in date strings
69 A @dfn{date} is a string, possibly empty, containing many items
70 separated by whitespace.  The whitespace may be omitted when no
71 ambiguity arises.  The empty string means the beginning of today (i.e.,
72 midnight).  Order of the items is immaterial.  A date string may contain
73 many flavors of items:
74
75 @itemize @bullet
76 @item calendar date items
77 @item time of day items
78 @item time zone items
79 @item day of the week items
80 @item relative items
81 @item pure numbers.
82 @end itemize
83
84 @noindent We describe each of these item types in turn, below.
85
86 @cindex numbers, written-out
87 @cindex ordinal numbers
88 @findex first @r{in date strings}
89 @findex next @r{in date strings}
90 @findex last @r{in date strings}
91 A few ordinal numbers may be written out in words in some contexts.  This is
92 most useful for specifying day of the week items or relative items (see
93 below).  Among the most commonly used ordinal numbers, the word
94 @samp{last} stands for @math{-1}, @samp{this} stands for 0, and
95 @samp{first} and @samp{next} both stand for 1.  Because the word
96 @samp{second} stands for the unit of time there is no way to write the
97 ordinal number 2, but for convenience @samp{third} stands for 3,
98 @samp{fourth} for 4, @samp{fifth} for 5,
99 @samp{sixth} for 6, @samp{seventh} for 7, @samp{eighth} for 8,
100 @samp{ninth} for 9, @samp{tenth} for 10, @samp{eleventh} for 11 and
101 @samp{twelfth} for 12.
102
103 @cindex months, written-out
104 When a month is written this way, it is still considered to be written
105 numerically, instead of being ``spelled in full''; this changes the
106 allowed strings.
107
108 @cindex language, in dates
109 In the current implementation, only English is supported for words and
110 abbreviations like @samp{AM}, @samp{DST}, @samp{EST}, @samp{first},
111 @samp{January}, @samp{Sunday}, @samp{tomorrow}, and @samp{year}.
112
113 @cindex language, in dates
114 @cindex time zone item
115 The output of the @command{date} command
116 is not always acceptable as a date string,
117 not only because of the language problem, but also because there is no
118 standard meaning for time zone items like @samp{IST}.  When using
119 @command{date} to generate a date string intended to be parsed later,
120 specify a date format that is independent of language and that does not
121 use time zone items other than @samp{UTC} and @samp{Z}.  Here are some
122 ways to do this:
123
124 @example
125 $ LC_ALL=C TZ=UTC0 date
126 Mon Mar  1 00:21:42 UTC 2004
127 $ TZ=UTC0 date +'%Y-%m-%d %H:%M:%SZ'
128 2004-03-01 00:21:42Z
129 $ date --iso-8601=ns | tr T ' '  # --iso-8601 is a GNU extension.
130 2004-02-29 16:21:42,692722128-0800
131 $ date --rfc-2822  # a GNU extension
132 Sun, 29 Feb 2004 16:21:42 -0800
133 $ date +'%Y-%m-%d %H:%M:%S %z'  # %z is a GNU extension.
134 2004-02-29 16:21:42 -0800
135 $ date +'@@%s.%N'  # %s and %N are GNU extensions.
136 @@1078100502.692722128
137 @end example
138
139 @cindex case, ignored in dates
140 @cindex comments, in dates
141 Alphabetic case is completely ignored in dates.  Comments may be introduced
142 between round parentheses, as long as included parentheses are properly
143 nested.  Hyphens not followed by a digit are currently ignored.  Leading
144 zeros on numbers are ignored.
145
146 Invalid dates like @samp{2005-02-29} or times like @samp{24:00} are
147 rejected.  In the typical case of a host that does not support leap
148 seconds, a time like @samp{23:59:60} is rejected even if it
149 corresponds to a valid leap second.
150
151
152 @node Calendar date items
153 @section Calendar date items
154
155 @cindex calendar date item
156
157 A @dfn{calendar date item} specifies a day of the year.  It is
158 specified differently, depending on whether the month is specified
159 numerically or literally.  All these strings specify the same calendar date:
160
161 @example
162 1972-09-24     # @sc{iso} 8601.
163 72-9-24        # Assume 19xx for 69 through 99,
164                # 20xx for 00 through 68.
165 72-09-24       # Leading zeros are ignored.
166 9/24/72        # Common U.S. writing.
167 24 September 1972
168 24 Sept 72     # September has a special abbreviation.
169 24 Sep 72      # Three-letter abbreviations always allowed.
170 Sep 24, 1972
171 24-sep-72
172 24sep72
173 @end example
174
175 The year can also be omitted.  In this case, the last specified year is
176 used, or the current year if none.  For example:
177
178 @example
179 9/24
180 sep 24
181 @end example
182
183 Here are the rules.
184
185 @cindex @sc{iso} 8601 date format
186 @cindex date format, @sc{iso} 8601
187 For numeric months, the @sc{iso} 8601 format
188 @samp{@var{year}-@var{month}-@var{day}} is allowed, where @var{year} is
189 any positive number, @var{month} is a number between 01 and 12, and
190 @var{day} is a number between 01 and 31.  A leading zero must be present
191 if a number is less than ten.  If @var{year} is 68 or smaller, then 2000
192 is added to it; otherwise, if @var{year} is less than 100,
193 then 1900 is added to it.  The construct
194 @samp{@var{month}/@var{day}/@var{year}}, popular in the United States,
195 is accepted.  Also @samp{@var{month}/@var{day}}, omitting the year.
196
197 @cindex month names in date strings
198 @cindex abbreviations for months
199 Literal months may be spelled out in full: @samp{January},
200 @samp{February}, @samp{March}, @samp{April}, @samp{May}, @samp{June},
201 @samp{July}, @samp{August}, @samp{September}, @samp{October},
202 @samp{November} or @samp{December}.  Literal months may be abbreviated
203 to their first three letters, possibly followed by an abbreviating dot.
204 It is also permitted to write @samp{Sept} instead of @samp{September}.
205
206 When months are written literally, the calendar date may be given as any
207 of the following:
208
209 @example
210 @var{day} @var{month} @var{year}
211 @var{day} @var{month}
212 @var{month} @var{day} @var{year}
213 @var{day}-@var{month}-@var{year}
214 @end example
215
216 Or, omitting the year:
217
218 @example
219 @var{month} @var{day}
220 @end example
221
222
223 @node Time of day items
224 @section Time of day items
225
226 @cindex time of day item
227
228 A @dfn{time of day item} in date strings specifies the time on a given
229 day.  Here are some examples, all of which represent the same time:
230
231 @example
232 20:02:00.000000
233 20:02
234 8:02pm
235 20:02-0500      # In @sc{est} (U.S. Eastern Standard Time).
236 @end example
237
238 More generally, the time of day may be given as
239 @samp{@var{hour}:@var{minute}:@var{second}}, where @var{hour} is
240 a number between 0 and 23, @var{minute} is a number between 0 and
241 59, and @var{second} is a number between 0 and 59 possibly followed by
242 @samp{.} or @samp{,} and a fraction containing one or more digits.
243 Alternatively,
244 @samp{:@var{second}} can be omitted, in which case it is taken to
245 be zero.  On the rare hosts that support leap seconds, @var{second}
246 may be 60.
247
248 @findex am @r{in date strings}
249 @findex pm @r{in date strings}
250 @findex midnight @r{in date strings}
251 @findex noon @r{in date strings}
252 If the time is followed by @samp{am} or @samp{pm} (or @samp{a.m.}
253 or @samp{p.m.}), @var{hour} is restricted to run from 1 to 12, and
254 @samp{:@var{minute}} may be omitted (taken to be zero).  @samp{am}
255 indicates the first half of the day, @samp{pm} indicates the second
256 half of the day.  In this notation, 12 is the predecessor of 1:
257 midnight is @samp{12am} while noon is @samp{12pm}.
258 (This is the zero-oriented interpretation of @samp{12am} and @samp{12pm},
259 as opposed to the old tradition derived from Latin
260 which uses @samp{12m} for noon and @samp{12pm} for midnight.)
261
262 @cindex time zone correction
263 @cindex minutes, time zone correction by
264 The time may alternatively be followed by a time zone correction,
265 expressed as @samp{@var{s}@var{hh}@var{mm}}, where @var{s} is @samp{+}
266 or @samp{-}, @var{hh} is a number of zone hours and @var{mm} is a number
267 of zone minutes.
268 The zone minutes term, @var{mm}, may be omitted, in which case
269 the one- or two-digit correction is interpreted as a number of hours.
270 You can also separate @var{hh} from @var{mm} with a colon.
271 When a time zone correction is given this way, it
272 forces interpretation of the time relative to
273 Coordinated Universal Time (@sc{utc}), overriding any previous
274 specification for the time zone or the local time zone.  For example,
275 @samp{+0530} and @samp{+05:30} both stand for the time zone 5.5 hours
276 ahead of @sc{utc} (e.g., India).
277 This is the best way to
278 specify a time zone correction by fractional parts of an hour.
279 The maximum zone correction is 24 hours.
280
281 Either @samp{am}/@samp{pm} or a time zone correction may be specified,
282 but not both.
283
284
285 @node Time zone items
286 @section Time zone items
287
288 @cindex time zone item
289
290 A @dfn{time zone item} specifies an international time zone, indicated
291 by a small set of letters, e.g., @samp{UTC} or @samp{Z}
292 for Coordinated Universal
293 Time.  Any included periods are ignored.  By following a
294 non-daylight-saving time zone by the string @samp{DST} in a separate
295 word (that is, separated by some white space), the corresponding
296 daylight saving time zone may be specified.
297 Alternatively, a non-daylight-saving time zone can be followed by a
298 time zone correction, to add the two values.  This is normally done
299 only for @samp{UTC}; for example, @samp{UTC+05:30} is equivalent to
300 @samp{+05:30}.
301
302 Time zone items other than @samp{UTC} and @samp{Z}
303 are obsolescent and are not recommended, because they
304 are ambiguous; for example, @samp{EST} has a different meaning in
305 Australia than in the United States.  Instead, it's better to use
306 unambiguous numeric time zone corrections like @samp{-0500}, as
307 described in the previous section.
308
309 If neither a time zone item nor a time zone correction is supplied,
310 time stamps are interpreted using the rules of the default time zone
311 (@pxref{Specifying time zone rules}).
312
313
314 @node Day of week items
315 @section Day of week items
316
317 @cindex day of week item
318
319 The explicit mention of a day of the week will forward the date
320 (only if necessary) to reach that day of the week in the future.
321
322 Days of the week may be spelled out in full: @samp{Sunday},
323 @samp{Monday}, @samp{Tuesday}, @samp{Wednesday}, @samp{Thursday},
324 @samp{Friday} or @samp{Saturday}.  Days may be abbreviated to their
325 first three letters, optionally followed by a period.  The special
326 abbreviations @samp{Tues} for @samp{Tuesday}, @samp{Wednes} for
327 @samp{Wednesday} and @samp{Thur} or @samp{Thurs} for @samp{Thursday} are
328 also allowed.
329
330 @findex next @var{day}
331 @findex last @var{day}
332 A number may precede a day of the week item to move forward
333 supplementary weeks.  It is best used in expression like @samp{third
334 monday}.  In this context, @samp{last @var{day}} or @samp{next
335 @var{day}} is also acceptable; they move one week before or after
336 the day that @var{day} by itself would represent.
337
338 A comma following a day of the week item is ignored.
339
340
341 @node Relative items in date strings
342 @section Relative items in date strings
343
344 @cindex relative items in date strings
345 @cindex displacement of dates
346
347 @dfn{Relative items} adjust a date (or the current date if none) forward
348 or backward.  The effects of relative items accumulate.  Here are some
349 examples:
350
351 @example
352 1 year
353 1 year ago
354 3 years
355 2 days
356 @end example
357
358 @findex year @r{in date strings}
359 @findex month @r{in date strings}
360 @findex fortnight @r{in date strings}
361 @findex week @r{in date strings}
362 @findex day @r{in date strings}
363 @findex hour @r{in date strings}
364 @findex minute @r{in date strings}
365 The unit of time displacement may be selected by the string @samp{year}
366 or @samp{month} for moving by whole years or months.  These are fuzzy
367 units, as years and months are not all of equal duration.  More precise
368 units are @samp{fortnight} which is worth 14 days, @samp{week} worth 7
369 days, @samp{day} worth 24 hours, @samp{hour} worth 60 minutes,
370 @samp{minute} or @samp{min} worth 60 seconds, and @samp{second} or
371 @samp{sec} worth one second.  An @samp{s} suffix on these units is
372 accepted and ignored.
373
374 @findex ago @r{in date strings}
375 The unit of time may be preceded by a multiplier, given as an optionally
376 signed number.  Unsigned numbers are taken as positively signed.  No
377 number at all implies 1 for a multiplier.  Following a relative item by
378 the string @samp{ago} is equivalent to preceding the unit by a
379 multiplier with value @math{-1}.
380
381 @findex day @r{in date strings}
382 @findex tomorrow @r{in date strings}
383 @findex yesterday @r{in date strings}
384 The string @samp{tomorrow} is worth one day in the future (equivalent
385 to @samp{day}), the string @samp{yesterday} is worth
386 one day in the past (equivalent to @samp{day ago}).
387
388 @findex now @r{in date strings}
389 @findex today @r{in date strings}
390 @findex this @r{in date strings}
391 The strings @samp{now} or @samp{today} are relative items corresponding
392 to zero-valued time displacement, these strings come from the fact
393 a zero-valued time displacement represents the current time when not
394 otherwise changed by previous items.  They may be used to stress other
395 items, like in @samp{12:00 today}.  The string @samp{this} also has
396 the meaning of a zero-valued time displacement, but is preferred in
397 date strings like @samp{this thursday}.
398
399 When a relative item causes the resulting date to cross a boundary
400 where the clocks were adjusted, typically for daylight saving time,
401 the resulting date and time are adjusted accordingly.
402
403 The fuzz in units can cause problems with relative items.  For
404 example, @samp{2003-07-31 -1 month} might evaluate to 2003-07-01,
405 because 2003-06-31 is an invalid date.  To determine the previous
406 month more reliably, you can ask for the month before the 15th of the
407 current month.  For example:
408
409 @example
410 $ date -R
411 Thu, 31 Jul 2003 13:02:39 -0700
412 $ date --date='-1 month' +'Last month was %B?'
413 Last month was July?
414 $ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!'
415 Last month was June!
416 @end example
417
418 Also, take care when manipulating dates around clock changes such as
419 daylight saving leaps.  In a few cases these have added or subtracted
420 as much as 24 hours from the clock, so it is often wise to adopt
421 universal time by setting the @env{TZ} environment variable to
422 @samp{UTC0} before embarking on calendrical calculations.
423
424 @node Pure numbers in date strings
425 @section Pure numbers in date strings
426
427 @cindex pure numbers in date strings
428
429 The precise interpretation of a pure decimal number depends
430 on the context in the date string.
431
432 If the decimal number is of the form @var{yyyy}@var{mm}@var{dd} and no
433 other calendar date item (@pxref{Calendar date items}) appears before it
434 in the date string, then @var{yyyy} is read as the year, @var{mm} as the
435 month number and @var{dd} as the day of the month, for the specified
436 calendar date.
437
438 If the decimal number is of the form @var{hh}@var{mm} and no other time
439 of day item appears before it in the date string, then @var{hh} is read
440 as the hour of the day and @var{mm} as the minute of the hour, for the
441 specified time of day.  @var{mm} can also be omitted.
442
443 If both a calendar date and a time of day appear to the left of a number
444 in the date string, but no relative item, then the number overrides the
445 year.
446
447
448 @node Seconds since the Epoch
449 @section Seconds since the Epoch
450
451 If you precede a number with @samp{@@}, it represents an internal time
452 stamp as a count of seconds.  The number can contain an internal
453 decimal point (either @samp{.} or @samp{,}); any excess precision not
454 supported by the internal representation is truncated toward minus
455 infinity.  Such a number cannot be combined with any other date
456 item, as it specifies a complete time stamp.
457
458 @cindex beginning of time, for @acronym{POSIX}
459 @cindex epoch, for @acronym{POSIX}
460 Internally, computer times are represented as a count of seconds since
461 an epoch---a well-defined point of time.  On @acronym{GNU} and
462 @acronym{POSIX} systems, the epoch is 1970-01-01 00:00:00 @sc{utc}, so
463 @samp{@@0} represents this time, @samp{@@1} represents 1970-01-01
464 00:00:01 @sc{utc}, and so forth.  @acronym{GNU} and most other
465 @acronym{POSIX}-compliant systems support such times as an extension
466 to @acronym{POSIX}, using negative counts, so that @samp{@@-1}
467 represents 1969-12-31 23:59:59 @sc{utc}.
468
469 Traditional Unix systems count seconds with 32-bit two's-complement
470 integers and can represent times from 1901-12-13 20:45:52 through
471 2038-01-19 03:14:07 @sc{utc}.  More modern systems use 64-bit counts
472 of seconds with nanosecond subcounts, and can represent all the times
473 in the known lifetime of the universe to a resolution of 1 nanosecond.
474
475 On most hosts, these counts ignore the presence of leap seconds.
476 For example, on most hosts @samp{@@915148799} represents 1998-12-31
477 23:59:59 @sc{utc}, @samp{@@915148800} represents 1999-01-01 00:00:00
478 @sc{utc}, and there is no way to represent the intervening leap second
479 1998-12-31 23:59:60 @sc{utc}.
480
481 @node Specifying time zone rules
482 @section Specifying time zone rules
483
484 @vindex TZ
485 Normally, dates are interpreted using the rules of the current time
486 zone, which in turn are specified by the @env{TZ} environment
487 variable, or by a system default if @env{TZ} is not set.  To specify a
488 different set of default time zone rules that apply just to one date,
489 start the date with a string of the form @samp{TZ="@var{rule}"}.  The
490 two quote characters (@samp{"}) must be present in the date, and any
491 quotes or backslashes within @var{rule} must be escaped by a
492 backslash.
493
494 For example, with the @acronym{GNU} @command{date} command you can
495 answer the question ``What time is it in New York when a Paris clock
496 shows 6:30am on October 31, 2004?'' by using a date beginning with
497 @samp{TZ="Europe/Paris"} as shown in the following shell transcript:
498
499 @example
500 $ export TZ="America/New_York"
501 $ date --date='TZ="Europe/Paris" 2004-10-31 06:30'
502 Sun Oct 31 01:30:00 EDT 2004
503 @end example
504
505 In this example, the @option{--date} operand begins with its own
506 @env{TZ} setting, so the rest of that operand is processed according
507 to @samp{Europe/Paris} rules, treating the string @samp{2004-10-31
508 06:30} as if it were in Paris.  However, since the output of the
509 @command{date} command is processed according to the overall time zone
510 rules, it uses New York time.  (Paris was normally six hours ahead of
511 New York in 2004, but this example refers to a brief Halloween period
512 when the gap was five hours.)
513
514 A @env{TZ} value is a rule that typically names a location in the
515 @uref{http://www.twinsun.com/tz/tz-link.htm, @samp{tz} database}.
516 A recent catalog of location names appears in the
517 @uref{http://twiki.org/cgi-bin/xtra/tzdate, TWiki Date and Time
518 Gateway}.  A few non-@acronym{GNU} hosts require a colon before a
519 location name in a @env{TZ} setting, e.g.,
520 @samp{TZ=":America/New_York"}.
521
522 The @samp{tz} database includes a wide variety of locations ranging
523 from @samp{Arctic/Longyearbyen} to @samp{Antarctica/South_Pole}, but
524 if you are at sea and have your own private time zone, or if you are
525 using a non-@acronym{GNU} host that does not support the @samp{tz}
526 database, you may need to use a @acronym{POSIX} rule instead.  Simple
527 @acronym{POSIX} rules like @samp{UTC0} specify a time zone without
528 daylight saving time; other rules can specify simple daylight saving
529 regimes.  @xref{TZ Variable,, Specifying the Time Zone with @code{TZ},
530 libc, The GNU C Library}.
531
532 @node Authors of parse_datetime
533 @section Authors of @code{parse_datetime}
534 @c the anchor keeps the old node name, to try to avoid breaking links
535 @anchor{Authors of get_date}
536
537 @cindex authors of @code{parse_datetime}
538
539 @cindex Bellovin, Steven M.
540 @cindex Salz, Rich
541 @cindex Berets, Jim
542 @cindex MacKenzie, David
543 @cindex Meyering, Jim
544 @cindex Eggert, Paul
545 @code{parse_datetime} started life as @code{getdate}, as originally
546 implemented by Steven M. Bellovin
547 (@email{smb@@research.att.com}) while at the University of North Carolina
548 at Chapel Hill.  The code was later tweaked by a couple of people on
549 Usenet, then completely overhauled by Rich $alz (@email{rsalz@@bbn.com})
550 and Jim Berets (@email{jberets@@bbn.com}) in August, 1990.  Various
551 revisions for the @sc{gnu} system were made by David MacKenzie, Jim Meyering,
552 Paul Eggert and others, including renaming it to @code{get_date} to
553 avoid a conflict with the alternative Posix function @code{getdate},
554 and a later rename to @code{parse_datetime}.  The Posix function
555 @code{getdate} can parse more locale-specific dates using
556 @code{strptime}, but relies on an environment variable and external
557 file, and lacks the thread-safety of @code{parse_datetime}.
558
559 @cindex Pinard, F.
560 @cindex Berry, K.
561 This chapter was originally produced by Fran@,{c}ois Pinard
562 (@email{pinard@@iro.umontreal.ca}) from the @file{parse_datetime.y} source code,
563 and then edited by K.@: Berry (@email{kb@@cs.umb.edu}).