Jump to content

User:GelvinM/Date (Unix command)

From Wikipedia, the free encyclopedia

The date command outputs the current or specified date and time and can also set the current system time. While available as a command line utility it is also used in shell scripts to perform automated actions such as calculating the difference between different times, converting times between timezones, reformating timstamps in texts to fit different date presentation requirements and adding versioning information to backup and other files.[1][2]

Option flags

[edit]

In the POSIX standard only the + and -u flags are listed as mandatory.[3]

  • +"date format codes" Alters the output using the specified date conversion codes which are prefaced with at % sign.[4] For POSIX compatibility the format codes correspond to the same format codes recognized by C's strftime() function. Not surprisingly many Unix implementations use this function in their implementation of date.[3]

date +"Today is day %j of the year %Y and it’s a %A"

Output: Today is day 078 of the year 2025 and it’s a Wednesday

  • -d "datestring" or --date="datestring" Displays the quoted date. Can be used with the + flag. Date strings consist of calendar date items which express day month and year, and time of day items which express hour:minute:second. Either item group is optional and they may appear in either order.[5] Times and dates can be adjusted using relative item expressions such as + or - time unit as well as keywords such as now, yesterday, tomorrow, previous, next, first and last.[6]

Examples:

date -d "Jan 14, 2026 4:33 PM"

date --date="17:51:02 22/12/2020"

date -d "next Tuesday +2 years -8 hours + 3 days"


  • -f filename or --file=filename where the referenced file contains one of more date strings on separate lines which are converted either to the default date format or the format specified by the + flag. Incompatible with the -d flag.


  • -r or --reference=filename - outputs the last modification time of the file
  • -s or --set="datestring" sets the current date and time to the value of datestring. Requires an account with admin privilege.[7]
[edit]
  • LC_TIME - changes the language and default order and format in which the date is presented

export LC_TIME=fr_CA.utf8

The value corresponds assigned to LC_TIME refers to files found in /usr/share/i18/locale. The file name is usually of the form languageCode_countryCode where the codes are the two character ISO codes for List of ISO 639 language codes or List of ISO 3166 country codes6 [8]

It is also possible to change the date presentation format by setting the environment variables LANG or LC_ALL however these will also change other presentation formats such as LC_MESSAGE (command error messages) and LC_MONETARY which many not be desirable.[4]

  • TZ - Alters the output of the command to reflect the designated time zone.

export TZ=Africa/Nairobi

The directory /usr/share/zoneinfo contains a large group of timezone database files| files describing both current and historical definitions of local times to facilitate the correct reproduction and translation of time information from one time zone and era to another. The files keep track of the offset from Greenwich Mean Time as well as when seasonal adjustments have been scheduled to take place such as the transition to and from Daylight Saving Time (DST). [9]

Security

[edit]

The integrity of system timestamps are extremely important for fraud detection by keeping track of precisely when financial transactions and other changes occur. By tampering with the system clock it is possible to misrepresent sequences of events.[10] With that in mind both BSD and GNU based Unix systems log both the before and after times when the clock is reset, including by the date command, in the utmp file.[11][12]

OpenBSD Unix systems can be set to highly secure mode preventing the system clock from being set back in time, blocking accounts with root privilege from maliciously backdating files, thereby enabling trustworthy date based forensics.[13][14][15]

Implementations in Other OS Environments

[edit]

CP/M 3

[edit]

Version 3 of the CP/M operating system introduced a simple date command which printed and set the date and time in a fixed format of MM/DD/YY HH:MM:SS. The command DATE C would display the date continuously whereas DATE SET would prompt for a new date and time. The documentation does not show any internationalization features.[16]

DR DOS, MS-DOS, Windows Command Line Shell

[edit]

All 3 utilities have highly similar behavior. The date and time commands present the current date and time separately and prompt the user for a new value. There is no command combining the two. Time can be specified up to 100ths of a second. The order of the date fields defaults to mm-dd-yy but can be set differently with definitions in the country.sys file for both DR DOS and MS-DOS. The Windows Control Panel is used to alter the default format settings for dates and time in the Windows command shell. MS-DOS allows one to specify time in 12 or 24 hour format as does the Windows command shell. DR-DOS only permitted 24 hour military notation.[17][18] [19][20]

Multics

[edit]

Multics was a multi-user operating system from Honeywell that was a major inspiration for Unix. It had the following time and date related active functions which could be entered either directly on the command line or embedded in other commands using square brackets: date, time, date_time, year, month (number), month_name, day (number), day_name, hour, minute.[21]

MPE

[edit]

MPE was an operating system that ran on HP 1000 and 3000 minicomputers. The command to display the date and time were SHOWTIME and SHOWCLOCK. The latter command added the time zone offset from GMT and a "correction" factor reflecting previous manual adjustments due to clock drift. Neither had any options.[22][23]

To reset both values SETCLOCK had named parameters DATE=mm/dd/yy[yy] , TIME=hh:mm[:ss] and CORRECTION=offset in seconds. and TIMEZONE. Two additional parameters NOW or GRADUAL indicated either the change should be immediate or that the system clock should either speed up or slow down to reach the specified new time. [24]

Windows PowerShell

[edit]

The get-date cmdlet provides capabilities similar to the Unix date command. The command's -date switch[a] can be used to create a date object representing any date and time. Relative time expressions as found in Unix are not part of the specification but the date can be calculated using either TimeSpans or by switches that set individual fields.

The -Uformat switch uses Unix % format codes to generate output. An alternate switch -Format uses .NET custom format strings[25] such as dddd (weekday name), MM (month number) and HH:mm for military times.[26]

The set-date command, like Unix's date -s command requires administrative privilege to run. The -date switch requires a date entered in a format compatible with the current locale. One can also compute a date by generating an offset from another date object or use the -Adjust switch to add or subtract a time interval. [27]

History

[edit]

David McKenzie is the author of the Linux version of date. The date command has been part of Unix since AT&T's original Version 1.[4][28]

  1. ^ Nemeth, Evi; Snyder, Garth; Hein, Trent; Whaley, Ben (2011). UNIX and Linux System Administration Handbook (4th ed.). New York: Prentice Hall. pp. 45, 398. ISBN 978-0131-48005-6.
  2. ^ "Formatting and Displaying Dates with Bash Scripts in Linux". www.squash.io. Vancouver BC: Squash Labs Inc. Oct 22, 2023. Retrieved Apr 28, 2025.
  3. ^ a b "IEEE and The Open Group Base Specifications Issue 8>3. Utilities>date". pub.opengroup.org. The Open Group. 2025. Retrieved Apr 27, 2025.
  4. ^ a b c Kerrisk, Michael (Feb 2, 2025). "date(1) — Linux manual page". man7.org. Retrieved Mar 20, 2025.
  5. ^ "Date input formats". gnu.org. Retrieved Mar 20, 2025.
  6. ^ "Relative items in date strings". gnu.org. Retrieved Mar 20, 2025.
  7. ^ Glass, Graham; King, Ables (2006). Linux for Programmers and Users. Upper Saddle River, NJ: Pearson Prentice Hall. p. 44. ISBN 978-0131-857483.
  8. ^ "Locale Environment Variables: Locale Names". www.gnu.org. gnu.org. Retrieved Mar 20, 2025.
  9. ^ Ward, Brian (April 19, 2021). "7.5.1". How Linux Works, 3rd Edition. No Starch Press. ISBN 978-1718-500402.
  10. ^ Vanini, Céline; Hargreave, Christopher; van Beek, Harm; Breinger, Frank (July 2024). "Was the clock correct? Exploring timestamp interpretation through time anchors for digital forensic event reconstruction". Elsevier. doi:10.1016/j.fsidi.2024.301759. Retrieved May 2, 2025.
  11. ^ "bsd man page for utmp". community.unix.com. Unix Linux Community. Nov 27, 1996. Retrieved April 27, 2025.
  12. ^ Kerrisk, Michael, ed. (2024-09-01). "utmp(5) — Linux manual page". man7.org. Retrieved April 27, 2025.
  13. ^ "DATE(1)". The OpenBSD Foundation. April 29, 2021. Retrieved May 3, 2025.
  14. ^ "GETTIMEOFDAY(2)". The OpenBSD Foundation. March 31, 2022. Retrieved May 3, 2025.
  15. ^ "SECURELEVEL(7)". The OpenBSD Foundation. April 29, 2025. Retrieved May 3, 2025.
  16. ^ "Digital Research : CP/M 3 Command Reference Manual" (PDF). Digital Research. March 1984. pp. 5–6. Retrieved May 3, 2025.
  17. ^ "DR DOS 6.0: Operating System for Personal Computers User Guide" (PDF). Digital Research. August 1991. pp. 182, 325–326, 365, 430. Retrieved May 1, 2025. {{cite web}}: Cite has empty unknown parameter: |2= (help); Text "edition: 2nd" ignored (help)
  18. ^ "Microsoft MS-DOS Operating System version 5.0 User's Guide and Reference". Microsoft. 1991. pp. 395, 575–576. {{cite web}}: |access-date= requires |url= (help); Check date values in: |access-date= (help); Missing or empty |url= (help); Missing pipe in: |access-date= (help)
  19. ^ "Windows Commands". Microsoft. Apr 13, 2018. pp. 242–243, 841–842. Retrieved May 3, 2025. {{cite web}}: Unknown parameter |other= ignored (|others= suggested) (help)
  20. ^ Murtaza, Fawad (September 13, 2023). "How to Change the Date and Time Format in Windows". Valnet Publishing Group. Retrieved May 3, 2025. {{cite web}}: Cite has empty unknown parameter: |1= (help)
  21. ^ "Multics Pocket Guide: Commands and Active Functions". www.bitsavers.org. Honeywelll. April 1980. Retrieved May 1, 2025.
  22. ^ "MPE/iX Commands Reference manual Volume I: SHOWCLOCK". Hewlett Packard. Retrieved May 1, 2025.
  23. ^ "MPE/iX Commands Reference manual Volume I: SHOWTIME". Hewlett Packard. Retrieved May 1, 2025.
  24. ^ "MPE/iX Commands Reference manual Volume I: SETTIME". Hewlett Packard. Retrieved May 1, 2025.
  25. ^ "Custom date and time format strings". Microsoft. December 3, 2022. Retrieved May 3, 2025.
  26. ^ "Get-Date". learn.microsoft.com. Microsoft. 2025. Retrieved May 3, 2025.
  27. ^ "Get-Date". learn.microsoft.com. Microsoft. 2025. Retrieved May 3, 2025.
  28. ^ "FreeBSD Manual Pages". man.freebsd.org. September 10, 2024. Retrieved April 1, 2025.


Cite error: There are <ref group=lower-alpha> tags or {{efn}} templates on this page, but the references will not show without a {{reflist|group=lower-alpha}} template or {{notelist}} template (see the help page).