cjacksonjr has asked for the wisdom of the Perl Monks concerning the following question:

Currently my Date Time is given as: Oct 30 2001 7:32AM. I need to convert it to Unix Time and I'm having problems...
#! /usr/local/bin/perl # A perl program to read the notifAlarmLog Table use FileHandle; use IPC::Open2; use Symbol; # use Tk; use Sybase::CTlib; use Time::Local; use strict; my $dbAlarm=new Sybase::CTlib 'harmless','harmless','OPSYB1',''; $dbAlarm->ct_sql("use TomTest"); my $sql = "SELECT * FROM notifAlarmLog GROUP BY name, AlarmStart"; my (@records) = $dbAlarm->ct_sql($sql); my ($record); my ($t); my ($PSFaultCount) = 0; # Keeps count of Power Supply Faults foreach $record (@records) { $PSFaultCount++; print "$record->[0], $record->[1], $record->[2], $record->[3]\n"; } print "There are $PSFaultCount Valid PS Faults.\n";

Replies are listed 'Best First'.
Re: Converting from Date Time to Unix Time
by miyagawa (Chaplain) on Jul 08, 2002 at 16:01 UTC
Re: Converting from Date Time to Unix Time
by mpeppler (Vicar) on Jul 08, 2002 at 18:42 UTC
    Just to add my own wrinkle to this issue - you can convert to Unix time directly in your SQL query:
    select datediff(ss, 'jan 1 1970', datetime_column) from ...
    That will return a unix time value that assumes that the database server runs in the GMT (or UTC) timezone, which it probably doesn't.

    You can convert the $time value from this expression to the real Unix time value for your timezone with:

    use Time::Local; $time = timelocal(gmtime($time_from_database));
    OK - so it's not the simplest way to do this - however it's good to be aware of what you can do at the database level. :-)

    Michael

Re: Converting from Date Time to Unix Time
by Abigail-II (Bishop) on Jul 08, 2002 at 16:41 UTC
    Interesting, but what exactly is your problem? I don't see any place in your code where you are trying to convert times. In fact, I don't see any time handling at all in your code. What do you have, what do you want, what did you try, and what happened when you tried?

    Abigail

Re: Converting from Date Time to Unix Time
by rbc (Curate) on Jul 08, 2002 at 17:52 UTC
Re: Converting from Date Time to Unix Time
by Aristotle (Chancellor) on Jul 08, 2002 at 17:07 UTC

    Besides miyagawa's and Abigail-II's points which I second, some minor nits:

    You should SELECT specific columns rather than a broad sweeping *.

    print(join(", ", @$record[0..3]), "\n"); is easier to maintain.

    Makeshifts last the longest.