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