Difference between revisions of "Linux datetime"

From Noah.org
Jump to navigationJump to search
m
Line 18: Line 18:
 
$ echo $EPOCH
 
$ echo $EPOCH
 
1266033437
 
1266033437
$ date +"%F %T %z" -d "1970-01-01 UTC $EPOCH seconds"
+
$ date +"%F %T %z" -d "1970-01-01 UTC + $EPOCH seconds"
 
2010-02-12 19:57:17 -0800
 
2010-02-12 19:57:17 -0800
 
</pre>
 
</pre>
Line 47: Line 47:
 
$ date +"%F %T %z" -r $EPOCH
 
$ date +"%F %T %z" -r $EPOCH
 
2010-02-12 19:57:17 -0800
 
2010-02-12 19:57:17 -0800
 +
</pre>
 +
 +
== date calculations withe the Linux `date` command ==
 +
 +
The 'seconds' keyword may be written as 'seconds', 'second', 'secs', and 'sec'.
 +
 +
The `date` command can format a not just the current date, but any given date with the '-d' option. You can also express calendrical calculations from a given date or from the '''now''' time.
 +
 +
<pre>
 +
# date; date -d "- 1 week"
 +
Fri May 28 13:43:36 PDT 2010
 +
Fri May 21 13:43:36 PDT 2010
 +
</pre>
 +
 +
On Linux, the file '/proc/uptime' will give you the number of seconds the system has been running. From there you can use `date` to figure out what date the system booted.
 +
<pre>
 +
date +"%F %T %z" -d "now UTC - $(cat /proc/uptime | cut -f 1 -d ' ' ) seconds"
 +
2010-05-14 09:31:05 -0700
 +
</pre>
 +
 +
This is how the Epoch time conversion works. You are asking for the date N number of seconds from
 +
=== Current time minus a number of seconds ===
 +
 +
If you run `cat /proc/uptime`
 +
<pre>
 +
# uptime
 +
13:35:57 up 14 days,  4:27,  4 users,  load average: 0.00, 0.01, 0.04
 +
</pre>
 +
So, what date was 14 days, 4 hours, and 27 minutes ago?
 +
 +
<pre>
 +
# date -d "-14 days -4 hours -27 minutes"
 +
Fri May 14 09:11:19 PDT 2010
 
</pre>
 
</pre>
  

Revision as of 16:04, 28 May 2010


This article covers date and time issues on Linux including epoch time, timezones, hardware and system clock sync, ntpdate, and NTP.

Epoch time

UNIX Epoch Time is the number of seconds since 00:00:00 1970-01-01 UTC. This is useful for datetime stamps or generating unique names.

The epoch time can be retrieved using the `date` command:

$ date "+%s"
1266033437

Unfortunately, it is not trivial to convert epoch times to a human-readable format in a portable way. The following will work on most Linux systems and is probably the most portable:

$ EPOCH=`date "+%s"`
$ echo $EPOCH
1266033437
$ date +"%F %T %z" -d "1970-01-01 UTC + $EPOCH seconds"
2010-02-12 19:57:17 -0800

The `date` command in versions of GNU coreutils since 5.3.0 support the @ feature:

$ EPOCH=`date "+%s"`
$ echo $EPOCH
1266033437
$ date +"%F %T %z" -d @$EPOCH
2010-02-12 19:57:17 -0800

If you have GNU Awk installed then you can use the following command (Ubuntu/Debian systems do not have the 'gawk' package installed by default):

$ EPOCH=`date "+%s"`
$ echo $EPOCH
1266033437
$ echo $EPOCH | awk '{print strftime("%F %T %z",$1)}'
2010-02-12 19:57:17 -0800

Supposedly, the following will work on BSD systems; unfortunately, not Mac OS X. And it is not portable with the GNU `date` command.

$ EPOCH=`date "+%s"`
$ echo $EPOCH
1266033437
$ date +"%F %T %z" -r $EPOCH
2010-02-12 19:57:17 -0800

date calculations withe the Linux `date` command

The 'seconds' keyword may be written as 'seconds', 'second', 'secs', and 'sec'.

The `date` command can format a not just the current date, but any given date with the '-d' option. You can also express calendrical calculations from a given date or from the now time.

# date; date -d "- 1 week"
Fri May 28 13:43:36 PDT 2010
Fri May 21 13:43:36 PDT 2010

On Linux, the file '/proc/uptime' will give you the number of seconds the system has been running. From there you can use `date` to figure out what date the system booted.

date +"%F %T %z" -d "now UTC - $(cat /proc/uptime | cut -f 1 -d ' ' ) seconds"
2010-05-14 09:31:05 -0700

This is how the Epoch time conversion works. You are asking for the date N number of seconds from

Current time minus a number of seconds

If you run `cat /proc/uptime`

# uptime
 13:35:57 up 14 days,  4:27,  4 users,  load average: 0.00, 0.01, 0.04

So, what date was 14 days, 4 hours, and 27 minutes ago?

# date -d "-14 days -4 hours -27 minutes"
Fri May 14 09:11:19 PDT 2010

Set timezone and clock on Linux

1. su to root

2. Find your timezone file under /usr/share/zoneinfo. For example:

   /usr/share/zoneinfo/US/Pacific

3. OPTIONAL: backup the current timezone configuration:

   cp /etc/localtime /etc/localtime.old

4. Create a symbolic link from the appropiate timezone to /etc/localtime.

   ln -sf /usr/share/zoneinfo/US/Pacific /etc/localtime

or

   ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime

5. Use rdate or ntpdate to set the system time (ntpdate is better):

   /usr/sbin/ntpdate time.nist.gov

or

   /usr/bin/rdate time.nist.gov

6. Set the hardware clock to the system time:

   /sbin/hwclock --systohc

Correct for clock drift

For where its not possible to run ntpd you may use adjtimex to correct for systematic drift.

aptitude install adjtimex

NIST

time.nist.gov

Pacific time
http://www.time.gov/timezone.cgi?Pacific/d/-8

Time Sync radio broadcasts

Most consumer "atomic" clocks use WWVB for radio sync. Time signals can also be received through GPS receivers which pickup the time signals broadcast by GPS satellites. Each GPS satellite carries its own atomic clock.

WWVB radio broadcasts of UTC time.

WWVB, Fort Collins, CO
60 kHz (binary carrier signal)
WWV, Fort Collins, CO
2500, 5000, 10000 and 15000 kHz (broadcasts voice)
WWVH, Kauai, HI
2500, 5000, 10000 and 15000 kHz (broadcasts voice)
CHU, Ottawa, Ontario, Canada
3330, 7335 and 14670 kHz (broadcasts voice)

UTC

UTC(GMT)EDTEST / CDTCST / MDTMST / PDTPST
00008 PM7 PM6 PM5 PM4 PM
01009 PM8 PM7 PM6 PM5 PM
020010 PM9 PM8 PM7 PM6 PM
030011 PM10 PM9 PM8 PM7 PM
0400MIDNIGHT11 PM10 PM9 PM8 PM
05001 AMMIDNIGHT11 PM10 PM9 PM
06002 AM1 AMMIDNIGHT11 PM10 PM
07003 AM2 AM1 AMMIDNIGHT11 PM
08004 AM3 AM2 AM1 AMMIDNIGHT
09005 AM4 AM3 AM2 AM1 AM
10006 AM5 AM4 AM3 AM2 AM
11007 AM6 AM5 AM4 AM3 AM
12008 AM7 AM6 AM5 AM4 AM
13009 AM8 AM7 AM6 AM5 AM
140010 AM9 AM8 AM7 AM6 AM
150011 AM10 AM9 AM8 AM7 AM
1600NOON11 AM10 AM9 AM8 AM
17001 PMNOON11 AM10 AM9 AM
18002 PM1 PMNOON11 AM10 AM
19003 PM2 PM1 PMNOON11 AM
20004 PM3 PM2 PM1 PMNOON
21005 PM4 PM3 PM2 PM1 PM
22006 PM5 PM4 PM3 PM2 PM
23007 PM6 PM5 PM4 PM3 PM