Converting between Local and Unix time problems

Discussion in 'Perl Misc' started by pj_sammie@hotmail.com, Jan 5, 2006.

  1. Guest

    I am trying to convert a localtime generated in a log file, to unix
    time for storage in a db. In the script below, it properly converts if
    I generate a localtime within the script. But, it won't properly
    convert when using a sample line from the log file. I am stumped...

    Thanks for any help.

    CODE
    ---------
    use Time::Local;

    # Doesn't convert propely
    $thetimel = "Tue Sep 28 17:12:13 2004";
    print "$thetimel\n";
    &convert_time;

    # Does convert properly
    $currenttime = time();
    $thetimel = localtime($currenttime);
    print "$thetimel\n";
    &convert_time;

    sub convert_time {
    if ($thetimel =~ /\w+\W+(\w+)\W+(\d+)\W+(\d+):(\d+):(\d+)\W+(\d+)/)
    {
    $mon = $1;
    $mday = $2;
    $hours = $3;
    $min = $4;
    $sec = $5;
    $year = $6;
    }

    $thetimes = timelocal($sec, $min, $hours, $mday, $mon, $year);

    print "$sec, $min, $hours, $mday, $mon, $year\n";
    print "$thetimel => $thetimes\n";
    print "$thetimes => " . localtime($thetimes) . "\n\n";
    }
    , Jan 5, 2006
    #1
    1. Advertising

  2. Brian Wakem Guest

    wrote:

    > I am trying to convert a localtime generated in a log file, to unix
    > time for storage in a db. In the script below, it properly converts if
    > I generate a localtime within the script. But, it won't properly
    > convert when using a sample line from the log file. I am stumped...
    >
    > Thanks for any help.
    >
    > CODE
    > ---------
    > use Time::Local;
    >
    > # Doesn't convert propely
    > $thetimel = "Tue Sep 28 17:12:13 2004";
    > print "$thetimel\n";
    > &convert_time;
    >
    > # Does convert properly
    > $currenttime = time();
    > $thetimel = localtime($currenttime);
    > print "$thetimel\n";
    > &convert_time;
    >
    > sub convert_time {
    > if ($thetimel =~ /\w+\W+(\w+)\W+(\d+)\W+(\d+):(\d+):(\d+)\W+(\d+)/)
    > {
    > $mon = $1;
    > $mday = $2;
    > $hours = $3;
    > $min = $4;
    > $sec = $5;
    > $year = $6;
    > }
    >
    > $thetimes = timelocal($sec, $min, $hours, $mday, $mon, $year);
    >
    > print "$sec, $min, $hours, $mday, $mon, $year\n";
    > print "$thetimel => $thetimes\n";
    > print "$thetimes => " . localtime($thetimes) . "\n\n";
    > }



    timelocal() expects $mon to be in the range 0..11, but you are giving it
    'Sep', which is numerically equal to 0, so you end up with a date in Jan.
    It seems to work for the current date as it is January. It wont work next
    month.



    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
    Brian Wakem, Jan 5, 2006
    #2
    1. Advertising

  3. Guest

    wrote:
    > I am trying to convert a localtime generated in a log file, to unix
    > time for storage in a db.


    You're going to a lot of trouble there, my friend. Have you considered
    something like this instead? (use "%o" instead of "%s" if you want
    localtime instead of UTC):

    #!/usr/bin/perl

    use Date::Manip;
    print UnixDate("Tue Sep 28 17:12:13 2004", "%s");

    __END__

    --
    http://DavidFilmer.com
    , Jan 5, 2006
    #3
  4. Guest

    Thanks for the quick replies. I did not know about Date::Manip, and
    yeah that's a lot cleaner, simpler, easier etc. I think I'll go with
    that :)
    , Jan 5, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    777
    Paul Boddie
    Mar 11, 2007
  2. Replies:
    2
    Views:
    503
  3. Jonathan Wood
    Replies:
    1
    Views:
    2,184
    Jonathan Wood
    Nov 1, 2009
  4. Tharinda
    Replies:
    3
    Views:
    1,203
    peter koch
    Aug 25, 2010
  5. David Joseph Bonnici
    Replies:
    1
    Views:
    207
    Gunnar Hjalmarsson
    May 15, 2005
Loading...

Share This Page