Mark Dominus on 17 Jul 2007 22:32:36 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Making sense of UNIX time_t ("epoch seconds")


>  Another easier way is
> $perl -e ' print gmtime(1184521826)."\n"; '
> Sun Jul 15 17:50:26 2007
>  $perl -e ' print "".localtime(1184521826)."\n"; '
> Sun Jul 15 13:50:26 2007

I do this so often I have it packaged as a utility in ~mjd/bin:

        % localtime 1184711397
        Tue Jul 17 18:29:57 2007

I have a lot of log files and such where each line begins with an
epochtime timestamp, so localtime also reads stdin:

        % cat log
        1176248126 + foo
        1179014678 + bar
        1180554286 - baz
        % localtime < log
        Tue Apr 10 19:35:26 2007 + foo
        Sat May 12 20:04:38 2007 + bar
        Wed May 30 15:44:46 2007 - baz

Here it is:

        #!/usr/bin/perl

        if (@ARGV) {
          for (@ARGV) {
            print show_localtime($_), "\n";
          }
        } else {
          while (<>) {
            s/^(\d+)/show_localtime($1)/e;
            print;
          }
        }

        sub show_localtime {
          my $t = shift;
          scalar localtime $t;
        }

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug