Current Articles | RSS Feed
Does anyone really know what time it is? Getting the correct time on computers running Linux is more complicated than you might think, and formatting it can be even more challenging.
You might think that entering the time command would tell you the time:
time
$ timereal 0m0.000suser 0m0.000ssys 0m0.000s
Sorry, but the time command is a timer that measures a program's elapsed time, user CPU time, and system CPU time. To see it in action, try it with a simple command that runs for a short time and then stops, like the updatedb command:
updatedb
$ time sudo updatedbreal 0m0.140suser 0m0.072ssys 0m0.068s
That didn't take long. This example uses the Bash shell's built-in time command, which is a stripped-down version of /usr/bin/time. But let us not go off on tangents, no matter how fun and interesting; try reading man 1 time if you want to learn more, for we must get back to telling time on Linux.
man 1 time
If you want to know the time on Linux you have to use the date command:
date
$ dateThu, Feb 23 13:01:56 PST 2012
This looks the same on both my CentOS and Linux Mint systems. Hurrah, something that different distros do the same way – an increasingly rare event in Linux-land. The date command has many options; here are some you might want to use in real life for tasks like snagging log file entries or files by date.
You can see your offset from UTC, Coordinated Universal Time, the global time standard for regulating clocks and time, with the -R option:
-R
$ date -RThu, 23 Feb 2012 10:53:49 -0800
Or display the UTC:
$ date -uThu Feb 23 18:55:00 UTC 2012
You can choose from a giant assortment of formatting options to display the date and time any way you want to. The next example shows how to use the formatting options with spaces and commas to make the date look nice. You could also use hyphens and slashes:
$ date +"%A, %B %d, %Y, %T %p"Thursday, February 23, 2012, 11:38:39 AM
One little-known formatting option for the date command will wow you. The -d option has magic time-traveling powers. It accepts time and date requests as text strings. For instance, this example fetches the date for next Wednesday:
-d
$ date -d "next wednesday"Wed Feb 29 00:00:00 PST 2012
Valid strings, as explained in the GNU coreutils manual, are:
So strings like "next week," "next year," "last week," "last year," "last (any day of the week)," and "this (day of week)" are all valid. "last" means -1, "this" means 0, and "first" and "next" are +1. There is no "second" because second is already used as a measure of time, but you can use third, fourth, fifth, and so on up to twelfth to display dates in the future, like this:
$ date -d "twelfth saturday"Sat May 12 00:00:00 PDT 2012$ date -d "seventh month"Sun Sep 23 13:53:45 PDT 2012
To go back in time you must use numbers and "ago":
$ date -d "8 years ago"Mon Feb 23 12:54:46 PST 2004
Go nuts and combine strings:
$ date -d "5 years 6 months 22 days 2 hours 6 minutes"Thu Sep 14 16:09:11 PDT 2017
Suppose you're happy with the current time, but not your current place. To see the time in a different time zone, specify the UTC offset:
$ TZ=UTC8 date Thu Feb 23 13:19:13 UTC 2012
UTC offsets range from +12 to -13. You may also use time zone names, which have the advantage of displaying the correct daylight savings and standard times, which sadly are not just a US affliction, but also exist in other countries. This example shows Pacific Standard Time:
$ TZ=America/Vancouver date Thu Feb 23 13:23:13 PST 2012
How do you know the UTC offset or time zone? One good reference is the TWiki.org Date and Time Gateway.
ls -l
A chronic problem on Linux is inconsistent time stamp formatting with ls -l (list files, long listing). This varies by Linux distribution and release. This is how it looks on Linux Mint 12:
carla@mint /etc $ ls -ldrwxr-xr-x 3 root root4096 2012-02-17 19:02 acpidrwxr-xr-x 6 root root4096 2011-10-12 08:10 apm
This example is from Fedora 16:
[carla@fedora etc]$ ls -ldrwxr-xr-x. 3 root root 4096 Nov 2 19:30 abrtdrwxr-xr-x. 2 root root 4096 Feb 8 2011 cron.monthly
Mint displays the time and date the same way for all dates. Fedora's output is a mish-mash: it's different from Mint, it omits the current year from the date, and it omits the time from previous years. These inconsistencies will vex and annoy if you use any scripts that have to read ls -l output.
There are multitudes of ways to control how ls -l displays timestamps. We'll learn two simple ways that override whatever method your distro uses: set an environment variable, or create a Bash alias.
The environment variable that controls the way ls -l formats timestamps is TIME_STYLE, and the ls -l command can be aliased with the --time-style option. Systemwide environment variables are set in /etc/profile or /etc/profile.d/, depending on what your distro uses, and per-user setting are in ~/.bash_profile, .bashrc, or ~/.profile. You should check these for any TIME_STYLE entries before making any changes. Bash aliases are also entered in one of these files. A fast way to see your existing Bash aliases is to run the alias command with no options. You can have multiple aliases for the same command as long as each one has a different alias name, and you remember to use the right alias name when you want to run the command.
--time-style
alias
The Mint time style is called long-iso, and the Fedora style is iso. (See the Debian Reference Manual section 9.2.5 to learn more about time styles.) You can preview different styles on the command line without changing anything:
long-iso
iso
$ ls -l --time-style=isodrwxr-xr-x. 3 root root 4096 11-02 19:30 abrt-rw-r--r--. 1 root root 46 01-11 11:29 adjtime-rw-r--r--. 1 root root 1518 2011-08-16 aliases$ ls -l --time-style=long-isodrwxr-xr-x. 3 root root 4096 2011-11-02 19:30 abrt-rw-r--r--. 1 root root 46 2012-01-11 11:29 adjtime-rw-r--r--. 1 root root 1518 2011-08-16 06:13 aliases
Set your desired option permanently by setting the TIME_STYLE environment variable in your personal .profile or .bash_profile, or system-wide in /etc/profile, like this:
export TIME_STYLE=long-iso
Or you can create a Bash alias by adding a line like this to one of the same files:
alias lsl='ls -l --time-style=long-iso'
Reboot to load your change.
The ISO 8601 standard spells out how to display time and date on computers. A good starting point for digging into the standard is Wikipedia's ISO 8601 page. Another helpful resource is the GNU Coreutils manual, sections 10 and 21. As a last resort, put up a sundial, kick back, and don't worry about time at all.
Allowed tags: <a> link, <b> bold, <i> italics