xref: /freebsd/bin/date/date.1 (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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.Dd September 10, 2024
33.Dt DATE 1
34.Os
35.Sh NAME
36.Nm date
37.Nd display or set date and time
38.Sh SYNOPSIS
39.\" Display time.
40.Nm
41.Op Fl nRu
42.Op Fl z Ar output_zone
43.Op Fl I Ns Op Ar FMT
44.Op Fl r Ar filename
45.Op Fl r Ar seconds
46.Oo
47.Sm off
48.Fl v
49.Op Cm + | -
50.Ar val Op Cm y | m | w | d | H | M | S
51.Sm on
52.Oc
53.Op Cm + Ns Ar output_fmt
54.\" Set time with the default input format.
55.Nm
56.Op Fl jnRu
57.Op Fl z Ar output_zone
58.Op Fl I Ns Op Ar FMT
59.Oo
60.Sm off
61.Fl v
62.Op Cm + | -
63.Ar val Op Cm y | m | w | d | H | M | S
64.Sm on
65.Oc
66.Sm off
67.Oo Oo Oo Oo Oo
68.Ar cc Oc
69.Ar yy Oc
70.Ar mm Oc
71.Ar dd Oc
72.Ar HH
73.Oc Ar MM Op Cm \&. Ar SS
74.Sm on
75.Op Cm + Ns Ar output_fmt
76.\" Set time with the user-provided input format.
77.Nm
78.Op Fl jnRu
79.Op Fl z Ar output_zone
80.Op Fl I Ns Op Ar FMT
81.Oo
82.Sm off
83.Fl v
84.Op Cm + | -
85.Ar val Op Cm y | m | w | d | H | M | S
86.Sm on
87.Oc
88.Fl f Ar input_fmt
89.Ar new_date
90.Op Cm + Ns Ar output_fmt
91.Sh DESCRIPTION
92When invoked without arguments, the
93.Nm
94utility displays the current date and time.
95Otherwise, depending on the options specified,
96.Nm
97will set the date and time or print it in a user-defined way.
98.Pp
99The
100.Nm
101utility displays the date and time read from the kernel clock.
102When used to set the date and time,
103both the kernel clock and the hardware clock are updated.
104.Pp
105Only the superuser may set the date,
106and if the system securelevel (see
107.Xr securelevel 7 )
108is greater than 1,
109the time may not be changed by more than 1 second.
110.Pp
111The options are as follows:
112.Bl -tag -width Ds
113.It Fl f Ar input_fmt
114Use
115.Ar input_fmt
116as the format string to parse the
117.Ar new_date
118provided rather than using the default
119.Sm off
120.Oo Oo Oo Oo Oo
121.Ar cc Oc
122.Ar yy Oc
123.Ar mm Oc
124.Ar dd Oc
125.Ar HH
126.Oc Ar MM Op Cm \&. Ar SS
127.Sm on
128format.
129Parsing is done using
130.Xr strptime 3 .
131.It Fl I Ns Op Ar FMT
132Use
133.St -iso8601
134output format.
135.Ar FMT
136may be omitted, in which case the default is
137.Cm date .
138Valid
139.Ar FMT
140values are
141.Cm date ,
142.Cm hours ,
143.Cm minutes ,
144.Cm seconds ,
145and
146.Cm ns No Pq for nanoseconds .
147The date and time is formatted to the specified precision.
148When
149.Ar FMT
150is
151.Cm hours
152.Po or the more precise
153.Cm minutes ,
154.Cm seconds ,
155or
156.Cm ns Pc ,
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 by a plus or minus sign,
229the date is adjusted forward or backward 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 forward 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 and
331.Ql %N
332for nanoseconds, as well as any arbitrary text.
333A newline
334.Pq Ql \en
335character is always output after the characters specified by
336the format string.
337The format string for the default display is
338.Dq +%+ .
339.Pp
340If an operand does not have a leading plus sign, it is interpreted as
341a value for setting the system's notion of the current date and time.
342The canonical representation for setting the date and time is:
343.Pp
344.Bl -tag -width Ds -compact -offset indent
345.It Ar cc
346Century
347(either 19 or 20)
348prepended to the abbreviated year.
349.It Ar yy
350Year in abbreviated form
351(e.g., 89 for 1989, 06 for 2006).
352.It Ar mm
353Numeric month, a number from 1 to 12.
354.It Ar dd
355Day, a number from 1 to 31.
356.It Ar HH
357Hour, a number from 0 to 23.
358.It Ar MM
359Minutes, a number from 0 to 59.
360.It Ar SS
361Seconds, a number from 0 to 60
362(59 plus a potential leap second).
363.El
364.Pp
365Everything but the minutes is optional.
366.Pp
367.Nm
368understands the time zone definitions from the IANA Time Zone Database,
369.Sy tzdata ,
370located in
371.Pa /usr/share/zoneinfo .
372Time changes for Daylight Saving Time, standard time, leap seconds
373and leap years are handled automatically.
374.Pp
375There are two ways to specify the time zone:
376.Pp
377If the file or symlink
378.Pa /etc/localtime
379exists, it is interpreted as a time zone definition file, usually in
380the directory hierarchy
381.Pa /usr/share/zoneinfo ,
382which contains the time zone definitions from
383.Sy tzdata .
384.Pp
385If the environment variable
386.Ev TZ
387is set, its value is interpreted as the name of a time zone definition
388file, either an absolute path or a relative path to a time zone
389definition in
390.Pa /usr/share/zoneinfo .
391The
392.Ev TZ
393variable overrides
394.Pa /etc/localtime .
395.Pp
396If the time zone definition file is invalid,
397.Nm
398silently reverts to UTC.
399.Pp
400Previous versions of
401.Nm
402included the
403.Fl d
404(set daylight saving time flag) and
405.Fl t
406(set negative time zone offset) options, but these details are now
407handled automatically by
408.Sy tzdata .
409Modern offsets are positive for time zones ahead of UTC and negative
410for time zones behind UTC, but like the obsolete
411.Fl t
412option, the
413.Sy tzdata
414files in the subdirectory
415.Pa /usr/share/zoneinfo/Etc
416still use an older convention where times ahead of UTC are considered
417negative.
418.Sh ENVIRONMENT
419The following environment variable affects the execution of
420.Nm :
421.Bl -tag -width Ds
422.It Ev TZ
423The timezone to use when displaying dates.
424The normal format is a pathname relative to
425.Pa /usr/share/zoneinfo .
426For example, the command
427.Dq TZ=America/Los_Angeles date
428displays the current time in California.
429The variable can also specify an absolute path.
430See
431.Xr environ 7
432for more information.
433.El
434.Sh FILES
435.Bl -tag -width /var/log/messages -compact
436.It Pa /etc/localtime
437Time zone information file for default system time zone.
438May be omitted, in which case the default time zone is UTC.
439.It Pa /usr/share/zoneinfo
440Directory containing time zone information files.
441.It Pa /var/log/messages
442Record of the user setting the time.
443.It Pa /var/log/utx.log
444Record of date resets and time changes.
445.El
446.Sh EXIT STATUS
447The
448.Nm
449utility exits 0 on success, 1 if unable to set the date, and 2
450if able to set the local date, but unable to set it globally.
451.Sh EXAMPLES
452The command:
453.Pp
454.Dl "date ""+DATE: %Y-%m-%d%nTIME: %H:%M:%S"""
455.Pp
456will display:
457.Bd -literal -offset indent
458DATE: 1987-11-21
459TIME: 13:36:16
460.Ed
461.Pp
462In the Europe/London timezone, the command:
463.Pp
464.Dl "date -v1m -v+1y"
465.Pp
466will display:
467.Pp
468.Dl "Sun Jan  4 04:15:24 GMT 1998"
469.Pp
470where it is currently
471.Li "Mon Aug  4 04:15:24 BST 1997" .
472.Pp
473The command:
474.Pp
475.Dl "date -v1d -v3m -v0y -v-1d"
476.Pp
477will display the last day of February in the year 2000:
478.Pp
479.Dl "Tue Feb 29 03:18:00 GMT 2000"
480.Pp
481So will the command:
482.Pp
483.Dl "date -v3m -v30d -v0y -v-1m"
484.Pp
485because there is no such date as the 30th of February.
486.Pp
487The command:
488.Pp
489.Dl "date -v1d -v+1m -v-1d -v-fri"
490.Pp
491will display the last Friday of the month:
492.Pp
493.Dl "Fri Aug 29 04:31:11 BST 1997"
494.Pp
495where it is currently
496.Li "Mon Aug  4 04:31:11 BST 1997" .
497.Pp
498The command:
499.Pp
500.Dl "date 8506131627"
501.Pp
502sets the date to
503.Dq Li "June 13, 1985, 4:27 PM" .
504.Pp
505.Dl "date ""+%Y%m%d%H%M.%S"""
506.Pp
507may be used on one machine to print out the date
508suitable for setting on another.
509.Qq ( Li "+%m%d%H%M%Y.%S"
510for use on
511.Tn Linux . )
512.Pp
513The command:
514.Pp
515.Dl "date 1432"
516.Pp
517sets the time to
518.Li "2:32 PM" ,
519without modifying the date.
520.Pp
521The command
522.Pp
523.Dl "TZ=America/Los_Angeles date -Iseconds -r 1533415339"
524.Pp
525will display
526.Pp
527.Dl "2018-08-04T13:42:19-07:00"
528.Pp
529The command:
530.Pp
531.Dl "env LC_ALL=C date -j -f ""%a %b %d %T %Z %Y"" ""`env LC_ALL=C date`"" ""+%s"""
532.Pp
533can be used to parse the output from
534.Nm
535and express it in Epoch time.
536.Pp
537Finally the command
538.Pp
539.Dl "TZ=America/Los_Angeles date -z Europe/Paris -j 0900"
540.Pp
541will print the time in the
542.Dq Europe/Paris
543timezone when it is 9:00 in the
544.Dq America/Los_Angeles
545timezone.
546.Sh DIAGNOSTICS
547It is invalid to combine the
548.Fl I
549flag with either
550.Fl R
551or an output format
552.Dq ( + Ns ... )
553operand.
554If this occurs,
555.Nm
556prints:
557.Ql multiple output formats specified
558and exits with status 1.
559.Sh SEE ALSO
560.Xr locale 1 ,
561.Xr clock_gettime 2 ,
562.Xr gettimeofday 2 ,
563.Xr getutxent 3 ,
564.Xr strftime 3 ,
565.Xr strptime 3 ,
566.Xr tzset 3 ,
567.Xr adjkerntz 8 ,
568.Xr ntpd 8 ,
569.Xr tzsetup 8
570.Rs
571.%T "TSP: The Time Synchronization Protocol for UNIX 4.3BSD"
572.%A R. Gusella
573.%A S. Zatti
574.Re
575.Rs
576.%U https://iana.org/time-zones
577.%T Time Zone Database
578.Re
579.Sh STANDARDS
580The
581.Nm
582utility is expected to be compatible with
583.St -p1003.2 .
584With the exception of the
585.Fl u
586option, all options are extensions to the standard.
587.Pp
588The format selected by the
589.Fl I
590flag is compatible with
591.St -iso8601 .
592.Pp
593The
594.Ql %N
595conversion specification for nanoseconds is a non-standard extension.
596It is compatible with GNU date's
597.Ql %N .
598.Sh HISTORY
599A
600.Nm
601command appeared in
602.At v1 .
603.Pp
604A number of options were added and then removed again, including the
605.Fl d
606(set DST flag) and
607.Fl t
608(set negative time zone offset).
609Time zones are now handled by code bundled with
610.Sy tzdata .
611.Pp
612The
613.Fl I
614flag was added in
615.Fx 12.0 .
616.Pp
617The
618.Ql %N
619conversion specification was added in
620.Fx 14.1 .
621