Convert from epoch seconds to local time.

This is a quick way to convert from epoch time to local time.

This can be used for example if running lsuser in AIX and converting the time_last_login field.

If lsuser gets back “time_last_login=1298298259“, then

perl -le 'print scalar localtime 1298298259'
Mon Feb 21 14:24:19 2011

Viola – thankyou google and Nicholas.

You can also do the following in bash for example if you want to check the change time on a file…

file=/tmp/file
ctime=`stat $file | grep Change`
filetime=${ctime#Change\:*}
epochtime=`date -d "$filetime" +%s`      # Convert from a datestamp to epoch secs
now=`date +%s`                           # Get current date/time in epoch secs
(( diffepoch = now - epochtime ))
if [[ $diffepoch -gt 60 ]];then
  echo "$file is newer than 2 mins."
else
  echo "$file is older than 2 mins."
fi

About

I'm a freelance UNIX engineer working in the UK. I'm married to Tina and between us we have six children. I'm a bit of an Apple fan boy, and all the Windows machines in the house are a thing of the past now.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: