JP Vossen on 16 Jul 2007 17:54:02 -0000


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

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


Date: Sun, 15 Jul 2007 14:54:26 -0400
From: Matthew Rosewarne <mukidohime@case.edu>
Subject: Re: [PLUG] Making sense of UNIX time_t ("epoch seconds")

On Sunday 15 July 2007, Antony P Joseph wrote:
There is a catch here epoch is in GMT.  While date will look into  local
time variable. So the correct will be date -d " local epoch  time()"

Right, forgot about timezones...

 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 don't know, call me old fashioned, but I don't find anything to be easier in perl. :>


"date" can give the correct output if you tack "UTC" on the end of the command, which would look like:

date -d "1970-01-01 $TIMESTAMP sec UTC"

<shameless, well, err, plug>

I devoted an entire chapter (11) to this topic in the _bash Cookbook_. You'd think computers would make doing date and time arithmetic easier, but it's not as easy as you might think. By far the easiest way to handle it is to convert to/from Epoch seconds, and then do simple math.

Chapter 11 covers this, explaining both the 'date' and Perl snippets mentioned in this thread, but also covering getting the date into Epoch time:

11.1 Formatting Dates for Display
11.2 Supplying a default Date
11.3 Automating Date Ranges
11.4 Converting Dates and Times to Epoch Seconds
	(Usually requires *GNU* date or Perl (or GNU awk))
11.5 Converting Epoch Seconds to Dates and Times
11.6 Getting Yesterday or Tomorrow with Perl
11.7 Figuring out Date and Time Arithmetic
11.8 Handling Time Zones, Daylight Saving Time, and Leap Years
	(Don't, let 'time' or some library do it for you)
11.9 Using date and cron to Run a Script on the Nth day


To get Epoch seconds for a time that is not "now" requires the non-standard -d switch, e.g.:
$ date -d '2005-11-05 12:00:00 +0000' '+%s'


</shameless plug>


You may also find the following native shell functions fascinating (or, they may give you a headache):
http://www.unixlabplus.com/unix-prog/date_function/Date_function.html
http://www.unixlabplus.com/unix-prog/date_function/



:-) JP ----------------------------|:::======|------------------------------- JP Vossen, CISSP |:::======| jp{at}jpsdomain{dot}org My Account, My Opinions |=========| http://www.jpsdomain.org/ ----------------------------|=========|------------------------------- Microsoft has single-handedly nullified Moore's Law. Innate design flaws of Windows make a personal firewall, anti-virus and anti-malware software mandatory. The resulting software arms race has effectively flattened Moore's Law on hardware running Windows. ___________________________________________________________________________ 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