If all you need this for is sorting purposes, then it suffices to swap the figures in the date, and it will sort correctly, ie something like
my ($date, $time) = split / /, $datetime; $date = join '/', reverse split m[/], $date; $datetime = "$date $time";

Now the dates from your example data will look like 2003/09/23 14:30 and 2003/09/30 10:00 which is in correct sorting order.

For the record, though, the culprit is you're forgetting to split up your string between date and time. So the time portion ends up trailing the year when you split the date, and the date ends up in front of the hour when split the time.

If you put a few print statements in, you'll notice the problem right away:

my ($mon, $day, $year) = split(/\//, $_[0]); my ($hour, $min, $sec) = split(/:/, $_[0]); print "$mon - $day - $year\n"; print "$hour - $min - $sec\n";
The solution is half there in your code already. I wonder why you assign the first parameter to $date at the beginning of your function, then never use it? If you change this bit:
my ($date) = @_; my ($time);
to read like
my ($datetime) = @_; my ($date, $time) = split / /, $datetime;
the rest of your code should work as is.

Makeshifts last the longest.

Edited by castaway: added some [, ] as appropriate

In reply to Re: date time to unix by Aristotle
in thread date time to unix by britney

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.