18 October 2010

Epoch Time

Have you ever needed a way to determine UNIX (Epoch) Time, or perhaps its
counterpart in 24 hour format?

via perl:

        server [0] perl -e 'print scalar time;' ; echo
        1154020678

        To use UNIX time to determine it's correlative in 24 hour format:
            server [0] perl -e 'print scalar localtime 1154020678 ;' ; echo
            Thu Jul 27 13:17:58 2006

via gnu awk:
        server [0] gawk 'BEGIN{print systime()}'
        1154020897

        To use UNIX time to determine it's correlative in 24 hour format:
            server [0] awk 'BEGIN{print strftime("%d %b %Y,  %H:%M:%S", '1154020897')}'
            27 Jul 2006,  13:21:37

via gnu date or /bin/date on FreeBSD:
        bsdhost [0] date +%s
        1154021261

        To use UNIX time to determine it's correlative in 24 hour format:
            bsdhost [0] date -r 1154021261
            Thu Jul 27 13:27:41 EDT 2006

Update:  I've posted a utility, timetrans.pl, that specifically handles time
    translations between epoch time and normalized date/time formats.
    The utility accepts either TIME as a parameter or as piped input and
    allows for input modifiers to further adjust the resulting time output.
    Have a look at the link above for examples.
  -- troy (04/2012)

2 comments:

JT said...

Hi Troy,

Is there a way to take epoch time in Solaris and format it as a date? I've tried the awk command you have under the via gnu awk example and the Solaris 10 machine I am using (test box) does not accept the command. It doesn't define the error well, so I have no idea where the breakdown is. gawk does not work on this machine, so awk (possibly nawk?) seems to be the only possibility. Do you have any suggestions that will do what your awk command will do, but in a Solaris environment?

Thanks for the page, this has been very helpful!

troy said...

JT,

You should have perl installed with Solaris 10 (even a base install), can you use it? The example I've posted was written showing output from a Solaris host with '1154020678' as epoch time. Unfortunately, without perl (or php, or java, etc.), writing some C code, or installing something like gnu awk or gnu date, I'm not sure of any other ways to do the translation that you are looking for. If you come across something, I would be keen to hear about it.

-troy