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