[NCLUG] perl datestring to timestamp conversion ?

Gabriel Somlo somlo at acns.colostate.edu
Mon Jan 10 12:48:35 MST 2005


On Mon, Jan 10, 2005 at 11:48:53AM -0700, dann frazier wrote:
> On Mon, Jan 10, 2005 at 11:24:34AM -0700, Gabriel Somlo wrote:
> > 
> > Right now, I'm doing it like so:
> > 
> >         my $utc = ( $is_utc_entry ) ? 'utc' : '';
> > 
> >         my $timestamp = qx/date -d "$YYYY-$MM-$DD $hh:$mm:$ss $utc" +%s/;
> > 
> > and let the 'date' program handle it. However, this script should be
> > portable, and run on weird things other than linux, where 'date'
> > doesn't have the %s option. Also, it would be nice if I didn't have to
> > ask various admins to install any extra perl modules :)

> 
> If this is the entire purpose of your script, you maybe able to just
> use the date command instead:
> 
> $ date -d "2005-12-01 12:12:12 utc"
> Thu Dec  1 05:12:12 MST 2005
> $ date -d "2005-12-01 12:12:12"
> Thu Dec  1 12:12:12 MST 2005
> 
> date supports format strings for specifying output style.


Thanks, I'm doing that -- see above :)

I need this stuff from within perl, and can't rely to always have
the nicey-nice GNU 'date' with the +%s (and -d for that matter)
options available. 

So I was more like trying to find out if I can wrap something like:

	{
		save localtime;
		set localtime to utc
		call mktime to get a timestamp
		restore saved localtime
	}

around mktime if it's a UTC thing, and not wrap anything around mktime
if it's not utc...

Thanks,
Gabriel



More information about the NCLUG mailing list