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.

In reply to Re: Convert date & time to UNIX time by Marshall
in thread Convert date & time to UNIX time by ewhitt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.