get unix time for a given date

Discussion in 'Perl Misc' started by daveyand, Mar 14, 2006.

  1. daveyand

    daveyand Guest

    Hey guys,

    I'm wracking my brain trying to get this working but cant seem to
    fathom it out.

    I have a date like: 03/14/06 and want to get the unixtime from it.

    I have tried to use time local like so:

    my ($mon, $day, $year) = ($date =~ m!(\d\d)/(\d\d)/(\d\d)!);
    my $unixtime = timelocal(0, 0,0, $day, ($mon - 1), ($year - 1900));

    And i get the following output:

    Setting 03/14/06 to be unixtime
    Day too small - -717264 > -24856
    Sec too small - -717264 < 74752
    Cannot handle date (0, 0, 0, 14, 2, 6) at ./look_for_mod.pl line 156


    What am i doing wrong? I cant work it out....

    Cheers for any help.
     
    daveyand, Mar 14, 2006
    #1
    1. Advertisements

  2. $year is 6, so you are passing -1884 to the routine above.

    Sinan
     
    A. Sinan Unur, Mar 14, 2006
    #2
    1. Advertisements

  3. daveyand

    takarov2003 Guest

    Is your year in your input date really only two digits? If so, you may
    just want to adjust for epoch year by adding 100 instead of subtracting
    1900.
     
    takarov2003, Mar 14, 2006
    #3
  4. daveyand

    daveyand Guest

    damnit!!!!!!!!!!!!!!!!!!!!!!!!!!!


    Seems to have worked thanks.
     
    daveyand, Mar 14, 2006
    #4

  5. What are you passing to timelocal as the year value?

    6 - 1900 = -1894


    Passing a year value of -1894, instead of 106.
     
    Tad McClellan, Mar 14, 2006
    #5
  6. daveyand

    Dr.Ruud Guest

    daveyand schreef:
    Alternative:
    perl -MDate::parse -le "print str2time('03/14/06')"

    Beware: the returned value is in timezone +0000.


    Tests:
    perl -MDate::parse -le "print scalar gmtime(str2time('03/14/06'))"
    perl -MDate::parse -le "print scalar localtime(str2time('03/14/06'))"
     
    Dr.Ruud, Mar 14, 2006
    #6
  7. daveyand

    Big and Blue Guest

    Can you handle the 3rd day of the 14th month?
     
    Big and Blue, Mar 15, 2006
    #7
  8. No, no. That's the 6th day of the 14th month in the year 3.

    jue
     
    Jürgen Exner, Mar 15, 2006
    #8
  9. daveyand

    robic0 Guest

    Its March 14, 2006 in America. 3/14/6. Whats the date in your coutries?
    Sweeden basackwards..
     
    robic0, Mar 15, 2006
    #9
  10. Or instead of 06. Two digit years don't need to be adjusted before being
    passed to timelocal().

    perldoc Time::Local
     
    Gunnar Hjalmarsson, Mar 15, 2006
    #10
  11. Really? I will have to ask my Brazilian friend tomorrow.
    Well, the third day of the 14th month in the year 6.

    However, because this is a global NG and the OP didn't make any statement
    about which date format he was using it is only fair to assume that he was
    using ISO format which is year-month-day.

    jue
     
    Jürgen Exner, Mar 15, 2006
    #11
  12. daveyand

    robic0 Guest

    ISO? Well, blame it on the Catholic nuns when in gradeschool,
    mm/dd/yy
     
    robic0, Mar 15, 2006
    #12
  13. But he did.
    Not really.
     
    Gunnar Hjalmarsson, Mar 15, 2006
    #13
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.