Linux: Timestamp conversion methods

26 06 2009

A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred. This data is usually presented in a consistent format, allowing for easy comparison of two different records and tracking progress over time.

Unix timestamp is defined as the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds. The Unix epoch is the time 00:00:00 UTC on January 1, 1970.

Some different ways to find timestamps:

# timestamp of a file (that means, file creation time in timestamp).
stat -c %Y filename.txt

# present date & time as timestamp.
date +%s

# convert timestamp to real time (readable format).
awk “BEGIN { print strftime(\”%c\”,$timestamp) }”

echo $timestamp|awk ‘{print strftime(“%c”,$1)}’

perl -e “print scalar(localtime($timestamp))”

perl -e “require ‘ctime.pl’; print &ctime($timestamp);”

# you can use date command if it has -d/–date option available.
date -d “1970-01-01 $timestamp sec GMT”

If these commands didn’t work when you copy&paste, you can download it from here.

~mohammed


Actions

Information

Leave a comment