getdate.texi 18 KB

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