xref: /freebsd/bin/date/date.1 (revision cbb3ec25236ba72f91cbdf23f8b78b9d1af0cedf)
1.\"-
2.\" Copyright (c) 1980, 1990, 1993
3.\"	The Regents of the University of California.  All rights reserved.
4.\"
5.\" This code is derived from software contributed to Berkeley by
6.\" the Institute of Electrical and Electronics Engineers, Inc.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)date.1	8.3 (Berkeley) 4/28/95
33.\"
34.Dd May 19, 2023
35.Dt DATE 1
36.Os
37.Sh NAME
38.Nm date
39.Nd display or set date and time
40.Sh SYNOPSIS
41.\" Display time.
42.Nm
43.Op Fl nRu
44.Op Fl z Ar output_zone
45.Op Fl I Ns Op Ar FMT
46.Op Fl r Ar filename
47.Op Fl r Ar seconds
48.Oo
49.Sm off
50.Fl v
51.Op Cm + | -
52.Ar val Op Cm y | m | w | d | H | M | S
53.Sm on
54.Oc
55.Op Cm + Ns Ar output_fmt
56.\" Set time with the default input format.
57.Nm
58.Op Fl jnRu
59.Op Fl z Ar output_zone
60.Op Fl I Ns Op Ar FMT
61.Oo
62.Sm off
63.Fl v
64.Op Cm + | -
65.Ar val Op Cm y | m | w | d | H | M | S
66.Sm on
67.Oc
68.Sm off
69.Oo Oo Oo Oo Oo
70.Ar cc Oc
71.Ar yy Oc
72.Ar mm Oc
73.Ar dd Oc
74.Ar HH
75.Oc Ar MM Op Cm \&. Ar SS
76.Sm on
77.Op Cm + Ns Ar output_fmt
78.\" Set time with the user-provided input format.
79.Nm
80.Op Fl jnRu
81.Op Fl z Ar output_zone
82.Op Fl I Ns Op Ar FMT
83.Oo
84.Sm off
85.Fl v
86.Op Cm + | -
87.Ar val Op Cm y | m | w | d | H | M | S
88.Sm on
89.Oc
90.Fl f Ar input_fmt
91.Ar new_date
92.Op Cm + Ns Ar output_fmt
93.Sh DESCRIPTION
94When invoked without arguments, the
95.Nm
96utility displays the current date and time.
97Otherwise, depending on the options specified,
98.Nm
99will set the date and time or print it in a user-defined way.
100.Pp
101The
102.Nm
103utility displays the date and time read from the kernel clock.
104When used to set the date and time,
105both the kernel clock and the hardware clock are updated.
106.Pp
107Only the superuser may set the date,
108and if the system securelevel (see
109.Xr securelevel 7 )
110is greater than 1,
111the time may not be changed by more than 1 second.
112.Pp
113The options are as follows:
114.Bl -tag -width Ds
115.It Fl f Ar input_fmt
116Use
117.Ar input_fmt
118as the format string to parse the
119.Ar new_date
120provided rather than using the default
121.Sm off
122.Oo Oo Oo Oo Oo
123.Ar cc Oc
124.Ar yy Oc
125.Ar mm Oc
126.Ar dd Oc
127.Ar HH
128.Oc Ar MM Op Cm \&. Ar SS
129.Sm on
130format.
131Parsing is done using
132.Xr strptime 3 .
133.It Fl I Ns Op Ar FMT
134Use
135.St -iso8601
136output format.
137.Ar FMT
138may be omitted, in which case the default is
139.Cm date .
140Valid
141.Ar FMT
142values are
143.Cm date ,
144.Cm hours ,
145.Cm minutes ,
146and
147.Cm seconds .
148The date and time is formatted to the specified precision.
149When
150.Ar FMT
151is
152.Cm hours
153(or the more precise
154.Cm minutes
155or
156.Cm seconds ) ,
157the
158.St -iso8601
159format includes the timezone.
160.It Fl j
161Do not try to set the date.
162This allows you to use the
163.Fl f
164flag in addition to the
165.Cm +
166option to convert one date format to another.
167Note that any date or time components unspecified by the
168.Fl f
169format string take their values from the current time.
170.It Fl n
171Obsolete flag, accepted and ignored for compatibility.
172.It Fl R
173Use RFC 2822 date and time output format.
174This is equivalent to using
175.Dq Li %a, %d %b %Y \&%T %z
176as
177.Ar output_fmt
178while
179.Ev LC_TIME
180is set to the
181.Dq C
182locale .
183.It Fl r Ar seconds
184Print the date and time represented by
185.Ar seconds ,
186where
187.Ar seconds
188is the number of seconds since the Epoch
189(00:00:00 UTC, January 1, 1970;
190see
191.Xr time 3 ) ,
192and can be specified in decimal, octal, or hex.
193.It Fl r Ar filename
194Print the date and time of the last modification of
195.Ar filename .
196.It Fl u
197Display or set the date in
198.Tn UTC
199(Coordinated Universal) time.
200By default
201.Nm
202displays the time in the time zone described by
203.Pa /etc/localtime
204or the
205.Ev TZ
206environment variable.
207.It Fl z Ar output_zone
208Just before printing the time, change to the specified timezone;
209see the description of
210.Ev TZ
211below.
212This can be used with
213.Fl j
214to easily convert time specifications from one zone to another.
215.It Xo
216.Fl v
217.Sm off
218.Op Cm + | -
219.Ar val Op Cm y | m | w | d | H | M | S
220.Sm on
221.Xc
222Adjust (i.e., take the current date and display the result of the
223adjustment; not actually set the date) the second, minute, hour, month
224day, week day, month or year according to
225.Ar val .
226If
227.Ar val
228is preceded with a plus or minus sign,
229the date is adjusted forwards or backwards according to the remaining string,
230otherwise the relevant part of the date is set.
231The date can be adjusted as many times as required using these flags.
232Flags are processed in the order given.
233.Pp
234When setting values
235(rather than adjusting them),
236seconds are in the range 0-59, minutes are in the range 0-59, hours are
237in the range 0-23, month days are in the range 1-31, week days are in the
238range 0-6 (Sun-Sat),
239months are in the range 1-12 (Jan-Dec)
240and years are in a limited range depending on the platform.
241.Pp
242On i386, years are in the range 69-38 representing 1969-2038.
243On every other platform, years 0-68 are accepted and represent 2000-2068, and
24469-99 are accepted and represent 1969-1999.
245In both cases, years between 100 and 1900 (both included) are accepted and
246interpreted as relative to 1900 of the Gregorian calendar with a limit of 138 on
247i386 and a much higher limit on every other platform.
248Years starting at 1901 are also accepted, and are interpreted as absolute years.
249.Pp
250If
251.Ar val
252is numeric, one of either
253.Cm y ,
254.Cm m ,
255.Cm w ,
256.Cm d ,
257.Cm H ,
258.Cm M
259or
260.Cm S
261must be used to specify which part of the date is to be adjusted.
262.Pp
263The week day or month may be specified using a name rather than a
264number.
265If a name is used with the plus
266(or minus)
267sign, the date will be put forwards
268(or backwards)
269to the next
270(previous)
271date that matches the given week day or month.
272This will not adjust the date,
273if the given week day or month is the same as the current one.
274.Pp
275When a date is adjusted to a specific value or in units greater than hours,
276daylight savings time considerations are ignored.
277Adjustments in units of hours or less honor daylight saving time.
278So, assuming the current date is March 26, 0:30 and that the DST adjustment
279means that the clock goes forward at 01:00 to 02:00, using
280.Fl v No +1H
281will adjust the date to March 26, 2:30.
282Likewise, if the date is October 29, 0:30 and the DST adjustment means that
283the clock goes back at 02:00 to 01:00, using
284.Fl v No +3H
285will be necessary to reach October 29, 2:30.
286.Pp
287When the date is adjusted to a specific value that does not actually exist
288(for example March 26, 1:30 BST 2000 in the Europe/London timezone),
289the date will be silently adjusted forwards in units of one hour until it
290reaches a valid time.
291When the date is adjusted to a specific value that occurs twice
292(for example October 29, 1:30 2000),
293the resulting timezone will be set so that the date matches the earlier of
294the two times.
295.Pp
296It is not possible to adjust a date to an invalid absolute day, so using
297the switches
298.Fl v No 31d Fl v No 12m
299will simply fail five months of the year.
300It is therefore usual to set the month before setting the day; using
301.Fl v No 12m Fl v No 31d
302always works.
303.Pp
304Adjusting the date by months is inherently ambiguous because
305a month is a unit of variable length depending on the current date.
306This kind of date adjustment is applied in the most intuitive way.
307First of all,
308.Nm
309tries to preserve the day of the month.
310If it is impossible because the target month is shorter than the present one,
311the last day of the target month will be the result.
312For example, using
313.Fl v No +1m
314on May 31 will adjust the date to June 30, while using the same option
315on January 30 will result in the date adjusted to the last day of February.
316This approach is also believed to make the most sense for shell scripting.
317Nevertheless, be aware that going forth and back by the same number of
318months may take you to a different date.
319.Pp
320Refer to the examples below for further details.
321.El
322.Pp
323An operand with a leading plus
324.Pq Sq +
325sign signals a user-defined format string
326which specifies the format in which to display the date and time.
327The format string may contain any of the conversion specifications
328described in the
329.Xr strftime 3
330manual page, as well as any arbitrary text.
331A newline
332.Pq Ql \en
333character is always output after the characters specified by
334the format string.
335The format string for the default display is
336.Dq +%+ .
337.Pp
338If an operand does not have a leading plus sign, it is interpreted as
339a value for setting the system's notion of the current date and time.
340The canonical representation for setting the date and time is:
341.Pp
342.Bl -tag -width Ds -compact -offset indent
343.It Ar cc
344Century
345(either 19 or 20)
346prepended to the abbreviated year.
347.It Ar yy
348Year in abbreviated form
349(e.g., 89 for 1989, 06 for 2006).
350.It Ar mm
351Numeric month, a number from 1 to 12.
352.It Ar dd
353Day, a number from 1 to 31.
354.It Ar HH
355Hour, a number from 0 to 23.
356.It Ar MM
357Minutes, a number from 0 to 59.
358.It Ar SS
359Seconds, a number from 0 to 60
360(59 plus a potential leap second).
361.El
362.Pp
363Everything but the minutes is optional.
364.Pp
365.Nm
366understands the time zone definitions from the IANA Time Zone Database,
367.Sy tzdata ,
368located in
369.Pa /usr/share/zoneinfo .
370Time changes for Daylight Saving Time, standard time, leap seconds
371and leap years are handled automatically.
372.Pp
373There are two ways to specify the time zone:
374.Pp
375If the file or symlink
376.Pa /etc/localtime
377exists, it is interpreted as a time zone definition file, usually in
378the directory hierarchy
379.Pa /usr/share/zoneinfo ,
380which contains the time zone definitions from
381.Sy tzdata .
382.Pp
383If the environment variable
384.Ev TZ
385is set, its value is interpreted as the name of a time zone definition
386file, either an absolute path or a relative path to a time zone
387definition in
388.Pa /usr/share/zoneinfo .
389The
390.Ev TZ
391variable overrides
392.Pa /etc/localtime .
393.Pp
394If the time zone definition file is invalid,
395.Nm
396silently reverts to UTC.
397.Pp
398Previous versions of
399.Nm
400included the
401.Fl d
402(set daylight saving time flag) and
403.Fl t
404(set negative time zone offset) options, but these details are now
405handled automatically by
406.Sy tzdata .
407Modern offsets are positive for time zones ahead of UTC and negative
408for time zones behind UTC, but like the obsolete
409.Fl t
410option, the
411.Sy tzdata
412files in the subdirectory
413.Pa /usr/share/zoneinfo/Etc
414still use an older convention where times ahead of UTC are considered
415negative.
416.Sh ENVIRONMENT
417The following environment variable affects the execution of
418.Nm :
419.Bl -tag -width Ds
420.It Ev TZ
421The timezone to use when displaying dates.
422The normal format is a pathname relative to
423.Pa /usr/share/zoneinfo .
424For example, the command
425.Dq TZ=America/Los_Angeles date
426displays the current time in California.
427The variable can also specify an absolute path.
428See
429.Xr environ 7
430for more information.
431.El
432.Sh FILES
433.Bl -tag -width /var/log/messages -compact
434.It Pa /etc/localtime
435Time zone information file for default system time zone.
436May be omitted, in which case the default time zone is UTC.
437.It Pa /usr/share/zoneinfo
438Directory containing time zone information files.
439.It Pa /var/log/messages
440Record of the user setting the time.
441.It Pa /var/log/utx.log
442Record of date resets and time changes.
443.El
444.Sh EXIT STATUS
445The
446.Nm
447utility exits 0 on success, 1 if unable to set the date, and 2
448if able to set the local date, but unable to set it globally.
449.Sh EXAMPLES
450The command:
451.Pp
452.Dl "date ""+DATE: %Y-%m-%d%nTIME: %H:%M:%S"""
453.Pp
454will display:
455.Bd -literal -offset indent
456DATE: 1987-11-21
457TIME: 13:36:16
458.Ed
459.Pp
460In the Europe/London timezone, the command:
461.Pp
462.Dl "date -v1m -v+1y"
463.Pp
464will display:
465.Pp
466.Dl "Sun Jan  4 04:15:24 GMT 1998"
467.Pp
468where it is currently
469.Li "Mon Aug  4 04:15:24 BST 1997" .
470.Pp
471The command:
472.Pp
473.Dl "date -v1d -v3m -v0y -v-1d"
474.Pp
475will display the last day of February in the year 2000:
476.Pp
477.Dl "Tue Feb 29 03:18:00 GMT 2000"
478.Pp
479So will the command:
480.Pp
481.Dl "date -v3m -v30d -v0y -v-1m"
482.Pp
483because there is no such date as the 30th of February.
484.Pp
485The command:
486.Pp
487.Dl "date -v1d -v+1m -v-1d -v-fri"
488.Pp
489will display the last Friday of the month:
490.Pp
491.Dl "Fri Aug 29 04:31:11 BST 1997"
492.Pp
493where it is currently
494.Li "Mon Aug  4 04:31:11 BST 1997" .
495.Pp
496The command:
497.Pp
498.Dl "date 8506131627"
499.Pp
500sets the date to
501.Dq Li "June 13, 1985, 4:27 PM" .
502.Pp
503.Dl "date ""+%Y%m%d%H%M.%S"""
504.Pp
505may be used on one machine to print out the date
506suitable for setting on another.
507.Qq ( Li "+%m%d%H%M%Y.%S"
508for use on
509.Tn Linux . )
510.Pp
511The command:
512.Pp
513.Dl "date 1432"
514.Pp
515sets the time to
516.Li "2:32 PM" ,
517without modifying the date.
518.Pp
519The command
520.Pp
521.Dl "TZ=America/Los_Angeles date -Iseconds -r 1533415339"
522.Pp
523will display
524.Pp
525.Dl "2018-08-04T13:42:19-07:00"
526.Pp
527The command:
528.Pp
529.Dl "env LC_ALL=C date -j -f ""%a %b %d %T %Z %Y"" ""`env LC_ALL=C date`"" ""+%s"""
530.Pp
531can be used to parse the output from
532.Nm
533and express it in Epoch time.
534.Pp
535Finally the command
536.Pp
537.Dl "TZ=America/Los_Angeles date -z Europe/Paris -j 0900"
538.Pp
539will print the time in the "Europe/Paris" timezone when it is 9:00 in The
540America/Los_Angeles timezone.
541.Sh DIAGNOSTICS
542It is invalid to combine the
543.Fl I
544flag with either
545.Fl R
546or an output format
547.Dq ( + Ns ... )
548operand.
549If this occurs,
550.Nm
551prints:
552.Ql multiple output formats specified
553and exits with status 1.
554.Sh SEE ALSO
555.Xr locale 1 ,
556.Xr gettimeofday 2 ,
557.Xr getutxent 3 ,
558.Xr strftime 3 ,
559.Xr strptime 3 ,
560.Xr tzset 3 ,
561.Xr adjkerntz 8 ,
562.Xr ntpd 8 ,
563.Xr tzsetup 8
564.Rs
565.%T "TSP: The Time Synchronization Protocol for UNIX 4.3BSD"
566.%A R. Gusella
567.%A S. Zatti
568.Re
569.Rs
570.%U https://iana.org/time-zones
571.%T Time Zone Database
572.Re
573.Sh STANDARDS
574The
575.Nm
576utility is expected to be compatible with
577.St -p1003.2 .
578With the exception of the
579.Fl u
580option, all options are extensions to the standard.
581.Pp
582The format selected by the
583.Fl I
584flag is compatible with
585.St -iso8601 .
586.Sh HISTORY
587A
588.Nm
589command appeared in
590.At v1 .
591.Pp
592A number of options were added and then removed again, including the
593.Fl d
594(set DST flag) and
595.Fl t
596(set negative time zone offset).
597Time zones are now handled by code bundled with
598.Sy tzdata .
599.Pp
600The
601.Fl I
602flag was added in
603.Fx 12.0 .
604