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

Without using a module such as Date::Manip or Time::Calc, how could I convert the time "2004-06-14 13:34:54" to a unix timestamp? I looked at mktime but for this you need to know the weekday.

I'm stuck on how to do this so would appreciate a hand. Thanks a lot.

js.

Replies are listed 'Best First'.
Re: unix timestamp
by graff (Chancellor) on Jul 08, 2004 at 22:14 UTC
    If you are reluctant to use Date::Manip and Time::Calc because they need to be installed (and you have a problem with that), you could try using Time::Local instead -- this module is part of the core distribution. (update: Happy-the-monk pointed out that it's actually "Date::Calc", not "Time::Calc".)

    Time::Local requires a little more work on your part, because you have to split your date string up into its components in order to use the module's "timelocal" function:

    use strict; use Time::Local; my $time_str = "2004-06-14 13:34:54"; my ($yr, $mo, $dy, $hr, $mi, $sc) = split( /\D+/, $time_str ); $mo--; # January is supposed to be "month 0" my $time_unx = timelocal( $sc, $mi, $hr, $dy, $mo, $yr ); # the function "does the right thing" given a 4-digit year # (check the Time::Local man page for more details on that) # prove that it works: print "$time_str = $time_unx (" . scalar( localtime $time_unx ) . ")\n +";
    When I ran that, I got:

    2004-06-14 13:34:54 = 1087234494 (Mon Jun 14 13:34:54 2004)

Re: unix timestamp
by Thelonius (Priest) on Jul 09, 2004 at 02:21 UTC
    Actually, you can use mktime. Weekday is there because the arguments correspond to the "struct tm" structure. The same struct is used for both input and output, but on input to mktime, "the original values of the tm_wday and tm_yday components of the structure are ignored". See the Posix definition of mktime.
Re: unix timestamp
by duff (Parson) on Jul 08, 2004 at 22:18 UTC

    Why the restriction on modules? Do you mean without using any modules? Because with Time::Local (part of the standard perl distribution since forever) you can do this:

    my $date = "2004-06-14 13:34:54"; my ($year,$month,$day,$hour,$minute,$second) = $date =~ /^(\d{4})-(\d\d)-(\d\d)\s+(\d\d):(\d\d):(\d\d)$/ or die; $gmt = timelocal($second,$minute,$hour,$day,$month-1,$year);

    Chop up the date string however you please.

Re: unix timestamp
by sacked (Hermit) on Jul 08, 2004 at 22:47 UTC
    POSIX is another core module you can use. Its strftime function will do what you want:

    use POSIX 'strftime'; + my $timestamp= strftime( '%s' => localtime() );

    See perldoc -f localtime, POSIX::strftime, and strftime(3).

    --sacked
Re: unix timestamp
by grinder (Bishop) on Jul 08, 2004 at 22:35 UTC
    I'm stuck on how to do this so would appreciate a hand

    Why on Earth would you want to do this without a module? Life is too short. But hey, just for kicks I thought I'd knock something out quickly. But then it started to become a hassle. But here's what you could do.

    Firstly, as a gross simplification, you can say that a year is 365.25 days. At least that gets us off the hook of having to calculate leap years. As an approximation it works out pretty nicely. So... (2004-1970) * 365.25 * 86400 gives you an epoch of 1072958400. Adding in the number of days, hours, minutes and seconds in terms of seconds is trivial.

    But then you have to determine how many days have elapsed since the first of January for each month (0 for January, 31 for February, 59 for March, 89 for April...) and then it sounds like not quite as much fun.

    At this point you could build a lookup table to determine the value for you. And then to be correct, you should add one to the months beyond February if we're in a leap year (we are!). But hang on, if we're using a lookup table for the current year the Right Thing would be to also generate a lookup table to get the exact number of days year by year taking into account the leap years.

    But hang on, that time value you gave, is that local time or UTC time? Is summer time in effect? And what about those pesky leap seconds that get introduced from time to time to take into account the slowing of the Earth's rotation?

    How accurate do you want the answer to be? If you want it to be accurate, use a module that takes care of all these fussy details for you. Someone else has already sweated out the details. Otherwise just use an approximate calculation and be done with it.

    Using the quick and dirty formula:

    ((y-1970) * 365.25 + (365.25 * (mon-1)/12) + day-1) * 86400 + ((h*60 + min) * 60) + sec

    I get an epoch of 1087263294 which works out to be 2004-06-15 01:34:54 UTC. The true value is 1087220094. Is this error rate acceptable?

    - another intruder with the mooring of the heat of the Perl

Re: unix timestamp
by naChoZ (Curate) on Jul 08, 2004 at 21:41 UTC

    Date::Manip is quite capable.

    --
    People who want to share their religious views with you almost never want you to share yours with them.
    naChoZ