Skip to main content

You're viewing an archived page. It is no longer being updated.

Converting UNIX Timestamps

The Test Traffic Measurement Service (TTM) was shut down on 1 July 2014. This information is available for historical reference.

The following script converts a Unix timestamp into a UTC timestamp:

# localtime - print local and GMT time
# -- Rene 19990114

for i in $@
echo "Clock: $i"
unset TZ
echo "Local Time: " `perl5 -e "print scalar localtime($i)"`
export TZ=GMT0GMT0
echo "GMT/UTC: " `perl5 -e "print scalar localtime($i)"`

Copy the script into a file, save and make executable. The script reads all arguments and transforms them into a normal timestamp, e.g:

$ localtime 1 31536000
Clock: 1
Local Time: Thu Jan 1 01:00:01 1970
GMT/UTC: Thu Jan 1 00:00:01 1970

Clock: 31536000
Local Time: Fri Jan 1 01:00:00 1971
GMT/UTC: Fri Jan 1 00:00:00 1971

Since UNIX time starts on 1/1/1970, "localtime 1" should be 1 second past midnight. 1 year is equal to 365 days * 24 hours * 3600 seconds, or 31536000, so "localtime 31536000" should return 1/1/1971. We are in the European timezone, and that is 1 hour ahead of UTC/GMT.

This script won't win any prizes for nice programming. That wasn't intended either, we just needed something to do the job.