provides software and services that enable enterprises
Live Chat 1-888-673-6564

Open Source Software Technical Articles

  • Home
  • Search
  • Contact Us
  • Products and Support
  • Services
  • Enterprise OSS Blog
  • Wazi Technical Blog
  • About Wazi
  • Attributions and Licensing
  • Supply Chain Compliance
  • How to Contribute
  • Contributors
  • Resources Library
  • Cloud Services
  • Partners
  • Customers
  • Community
  • Company
  • Careers
  • News and Events

Subscribe to Wazi by Email

Your email:


Enterprise Developer Support 24 x 7, Get a Support Quote Now!


click-here-to-chat-with-an-online-representative

download-oss-discovery

Latest Posts

  • The secret to great reporting with Drupal 7
  • A more colorful LibreOffice unveiled
  • Toward a more colorful LibreOffice
  • Flexible administration with Puppet's Facter and templates
  • Knock for OpenSSH
  • Get more out of phpMyAdmin
  • Image annotation in GIMP, Dia, and OpenOffice Draw
  • Solr, Drupal 7, and faceted search
  • Using FreeNAS' new full disk encryption for ZFS
  • Create distributed storage with Gluster

Connect with Us!

Current Articles | RSS Feed RSS Feed

Telling the Time on Linux: It's Harder Than It Looks

Posted by Carla Schroder on Mon, Feb 27, 2012
  
Email This Email Article  
Tweet  
  

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

real 0m0.000s
user 0m0.000s
sys 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:




$ time sudo updatedb

real 0m0.140s
user 0m0.072s
sys 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.



If you want to know the time on Linux you have to use the date command:




$ date
Thu, 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:




$ date -R
Thu, 23 Feb 2012 10:53:49 -0800


Or display the UTC:




$ date -u
Thu 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:




$ date -d "next wednesday"
Wed Feb 29 00:00:00 PST 2012


Valid strings, as explained in the GNU coreutils manual, are:




    • calendar date items

    • time of day items

    • time zone items

    • combined date and time of day items

    • day of the week items

    • relative items

  • pure numbers


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.



Fun with 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 -l
drwxr-xr-x 3 root root4096 2012-02-17 19:02 acpi
drwxr-xr-x 6 root root4096 2011-10-12 08:10 apm


This example is from Fedora 16:




[carla@fedora etc]$ ls -l
drwxr-xr-x. 3 root root 4096 Nov 2 19:30 abrt
drwxr-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.


19a98812-f823-48dc-841e-bf029c63c6d7

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.



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:




$ ls -l --time-style=iso
drwxr-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-iso
drwxr-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.

Follow @openlogic
Follow @CloudSwing

This work is licensed under a Creative Commons Attribution 3.0 Unported License
Creative Commons License.Follow @openlogic
Follow @OSCloudServices

This work is licensed under a Creative Commons Attribution 3.0 Unported License
Creative Commons License.
Tags: CentOS, Technical, Tips & Tricks, System Administration

Comments

Currently, there are no comments. Be the first to post one!
Post Comment
Name
 *
Email
 *
Website (optional)
Comment
 *

Allowed tags: <a> link, <b> bold, <i> italics

Loading...
Error sending email
Email sent successfully

Email article
Email To : 
Your name : 
Message : (maximum 200 characters)
Home | Search | Contact Us | Products and Support | Services | Enterprise OSS Blog | Wazi Technical Blog | Resources Library | Cloud Services | Partners | Customers | Community | Company | Careers | News and Events
Products
OpenLogic Exchange (OLEX)
License Compliance Module
OSS Discovery
OSS Deep Discovery
OpenUpdate
Services
Open Source Support
CentOS Support
Scanning & Compliance
Open Source Training
Professional Services
Solutions
Support & Indemnification
Open Source Governance
Open Source Scanning
Open Source Provisioning
Consulting & Training
Contact Us
1-888-673-6564


© 2013 OpenLogic, Inc. All rights reserved.
Site Map  |  Privacy Policy