On Mon, Aug 13, 2007 at 02:43:04PM +0800, Ow Mun Heng wrote:
> Been struggling with a new script I'm porting over from bash because
> perl's DBI is much more elegant than my previous usage of sqsh.
> 
> I'm having trouble converting from a datetime into a unix epoch
> timestamp.
> 
> under bash, this is done.
> 
> date = YYYY-MM-DD HH:MM:SS
> epoch_date = date -d "$date" +%s
> 
> $date
> Mon Aug 13 14:39:48 MYT 2007
> $date -d "2007-08-13 14:39:48" +%s
> 1186987188
> 
> Under perl, I have no idea how this can be achieved.
> Neither localtime, timelocal or AFAICT, DateTime can achieve this
> easily.

Maybe I'm confusing what you're looking for, but a look at 'perldoc
DateTime' shows that the perl DateTime module has a 'epoch' function:

         $epoch_time  = $dt->epoch;
         # may return undef if the datetime is outside the range that is
         # representable by your OS's epoch system.

So if you create your datetime object with the new() function, you can 
simply output the epoch time over.

As another OOT suggestion, check out Class::DBI as something even more 
elegant than DBI.  I've been using it for the last year or so and LOVE
it's simplicity and OO-ness compared to DBI.


-- 
Alan <[EMAIL PROTECTED]> - http://arcterex.net
--------------------------------------------------------------------
"Beware of computer programmers that carry screwdrivers." -- Unknown
-- 
[EMAIL PROTECTED] mailing list

Reply via email to