Walker News

GNU Date Generates Consistent UNIX Epoch Time Between Time Zones

GNU date is easier for Linux administrators who write shell script that involves Unix Epoch time or Julian time (Julian time is the common term we used to refer this UNIX time in our IT office).

However, the GNU date program might produces “different” set of date-to-Julian or Julian-to-date conversion for the same input but having it executed at different time zones. It might not happen so if the appropriate GNU date option switch is used for the “Unix Epoch time and date” conversion.

Here are some GNU date examples that perform date-to-Julian and Julian-to-date conversion in two different time zones, e.g. a server at London with BST time zone (British Summer Time) and another server at Utah with MDT time zone (Mountain Daylight Time).

Take note of the GNU date output that appears in “blue color” text (will be explained later):

GNU Date: Convert date to Unix Epoch time / Julian time

UK, London, BST time zone at this time of writing:
walker@walkernews.net # date -d "2008-08-08 20:08:08" +%s
1218222488
walker@walkernews.net # date -u -d "2008-08-08 20:08:08" +%s
1218226088
walker@walkernews.net # date -d "2008-08-08 20:08:08 UTC" +%s
1218226088

US, Utah, MTD time zone at this time of writing:
walker@walkernews.net # date -d "2008-08-08 20:08:08" +%s
1218247688
walker@walkernews.net # date -u -d "2008-08-08 20:08:08" +%s
1218226088
walker@walkernews.net # date -d "2008-08-08 20:08:08 UTC" +%s
1218226088

GNU Date: Convert Unix Epoch time / Julian time to date

UK, London, BST time zone at this time of writing:
walker@walkernews.net # date -u -d "1970-01-01 1218226088 secs" 
Fri Aug  8 20:08:08 UTC 2008
walker@walkernews.net # date -d "UTC 1970-01-01 1218226088 secs"
Fri Aug  8 21:08:08 BST 2008
walker@walkernews.net # date -d "UTC 1970-01-01 1218226088 secs" +"%Y-%m-%d %H:%M:%S %z"
2008-08-08 21:08:08 +0100
walker@walkernews.net # date -d "1970-01-01 1218226088 secs"
Fri Aug  8 20:08:08 BST 2008
walker@walkernews.net # date -d "1970-01-01 1218222488 secs"
Fri Aug  8 19:08:08 BST 2008
walker@walkernews.net # date -u -d "1970-01-01 1218222488 secs"
Fri Aug  8 19:08:08 UTC 2008

US, Utah, with MTD time zone at this time of writing:
walker@walkernews.net # date -u -d "1970-01-01 1218226088 secs"
Fri Aug  8 20:08:08 UTC 2008
walker@walkernews.net # date -d "UTC 1970-01-01 1218226088 secs"
Fri Aug  8 14:08:08 MDT 2008
walker@walkernews.net # date -d "UTC 1970-01-01 1218226088 secs" +"%Y-%m-%d %H:%M:%S %z"
2008-08-08 14:08:08 -0600
walker@walkernews.net # date -d "1970-01-01 1218226088 secs"
Fri Aug  8 21:08:08 MDT 2008
walker@walkernews.net # date -d "1970-01-01 1218247688 secs"
Sat Aug  9 03:08:08 MDT 2008
walker@walkernews.net # date -u -d "1970-01-01 1218247688 secs"
Sat Aug  9 02:08:08 UTC 2008

Have the GNU date to display/output consistent Unix Epoch time at different time zones

So, the question now is about how to have GNU date directly output a portable and consistent Julian-to-date or date-to-Julian conversion, especially when date calculation shell script is running on multiple servers that distributed at different time zones?

As you can see from the GNU date examples with “blue text” of output, the -u can directly produces a portable and consistent result of:
  • Unix Epoch time/Julian time of the given date “8 Aug 2008 8:08PM and 8 seconds”
  • Date converted from the Julian time of “8 Aug 2008 8:08PM and 8 seconds”

Although specify UTC in the date string given to -d option switch produces the same Unix Epoch time/Julian time, the Julian-to-date conversion results are not consistent.

There is nothing wrong with GNU date. It’s not a GNU date bug that causes it to produce different result at different time zones. Indeed, it’s the behaviour of the GNU date option switches!

For example, when the -u is not used, GNU date produces output that requires additional conversion from the given time zones offset.

So, the output of
date -d "UTC 1970-01-01 1218226088 secs" +"%Y-%m-%d %H:%M:%S %z"

indicates the converted time is displayed as “21:08:08 +0100”, meaning that the “correct” time should be “21:08:08 minus 1 hour”.
In Linux command prompt, type info date and browse to the GNU date examples section, the difference between -u and UTC is mentioned.

You can also access to these examples at the official GNU Coreutils online documentation.

Verdict

Specify the -u option switch when executing GNU date for “date to Unix Epoch time” or “Unix Epoch time to date” conversion, e.g.
date -u -d "2008-12-25 12:25:08" +%s
date -u -d "1970-01-01 1230207908 secs"

These two GNU date commands generate same output in UTC time, regardless of the local time zone where the GNU date is executed.

Custom Search

  1. greetingsfrompoland 06-03-09@07:15

    Hello to all ! Greetings From Poland. very Good Page !

2017  •  Privacy Policy