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