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

I have never converted date & time to UNIX time. I have a string I need to extract the date & time from. The format is:

20090719-074248 (year/month/day-hour/minute/second)

I was wondering what would be the easiest way to do this? I realize there Perl modules for this, but I was also wondering if there are any system variables/something I could use to simply this.

Replies are listed 'Best First'.
Re: Convert date & time to UNIX time
by Marshall (Canon) on Jul 30, 2009 at 23:26 UTC
    The easy way in Perl is with regular expressions, like below. Not sure what else you need. \d{4}means exactly 4 digits, \d{2} exactly 2 digits, etc.
    #!/usr/bin/perl -w use strict; my $string = "20090719-074248"; my ($year,$month,$day,$hour,$min,$sec) = my @data = ($string=~ m/(\d{4})(\d{2})(\d{2})-(\d{2})(\d{2})(\d{2})/); print "data = @data\n"; #digits as a list print "year = $year\n", "month = $month\n", "day = $day\n", "hour = $hour\n", "min = $min\n", "sec = $sec\n"; __END__ PRINTS: data = 2009 07 19 07 42 48 year = 2009 month = 07 day = 19 hour = 07 min = 42 sec = 48
    The low level "unix time" is a number of seconds from an arbitrary point, usually jan, 1, 1970 at midnight. Without more info, I'm not sure what you are doing or what you need. But yes there are ways to convert this string to a number of seconds since "epoch" time.

    This #seconds as an integer format is useful when adding or subtracting dates ... conversion is from time like above to total #seconds for each date, then do arithmetic and then convert resulting number back to individual date components.

    For sorting dates, you have the perfect format as a very simple sort will yield the right date order.

    Update:As Ikegami points out, these are the 4 main time functions.

    Careful reading of the man pages is required as there are some "tricks", like $mon ranges from 0..11, while $mday ranges from 1..31 and $year is actual year -1900.

    use Time::Local; #These are the inverse functions of localtime() and gmtime() #which are Perl "built-in functions".. (no "use" statement needed) $time = timelocal($sec,$min,$hour,$mday,$mon,$year); $time = timegm($sec,$min,$hour,$mday,$mon,$year);
    Oh, if you aren't used to time zones, gmtime means in old terminology Greenwich Mean Time, the time at the "prime meridian" in Greenwich, England. This is called UTC now, a French acronym, which means in English Universal Coordinated Time.

      But yes there are ways to convert this string to a number of seconds since "epoch" time.

      Time::Local's timegm and timelocal being rather convenient if that's all one wants to do. Any date and/or time manipulation module will do the trick too.

      "UTC" isn't French. It would be "TUC" in French. It's a compromise that uses the letters from both the French and English acronym while using neither.

      The metric unit system is known as "SI", the French acronym for "Système International d'Unités". Maybe you were thinking of that.

        I didn't know the politics of UTC. But makes sense, sometimes a good compromise is sometimes one that makes everybody a little bit unhappy!

        On other abbreviations, folks may come across "Z", acronym "Zulu". 13:15 Z would be "thirteen fifteen Zulu". Zulu is an abbreviation of an abbreviation! When handwritten, often a "-" is drawn through the Z to make it less confusable with the number "2". Z, GMT, UTC basically all mean the same thing.

Re: Convert date & time to UNIX time
by metaperl (Curate) on Jul 31, 2009 at 15:06 UTC
    DateTime is everyone's friend!
    use DateTime; use DateTime::Format::Strptime; my $parser = DateTime::Format::Strptime->new ( pattern => '%Y%m%d-%H%M%S' ); my $dt = $parser->parse_datetime( "20090719-074248" ) ; warn $dt->epoch;
Re: Convert date & time to UNIX time
by MidLifeXis (Monsignor) on Jul 31, 2009 at 13:17 UTC

    You have ambiguity in your timestamps at least once a year, depending on your time zone. If these are GMT only, you should be OK, but when DST stops, your timestamps will be ambiguous for a period of time. Not sure if this is an issue, but it may need to be taken into account, depending on your application.

    --MidLifeXis

    The tomes, scrolls etc are dusty because they reside in a dusty old house, not because they're unused. --hangon in this post

Re: Convert date & time to UNIX time
by doug (Pilgrim) on Jul 31, 2009 at 20:18 UTC
    I almost always use Date::Manip for date parsing. It might be overkill, but I sometimes think it is smarter than I am.