The easiest way I have found to do this is to turn the date and time string into a number. Split the string into day,month,year,hours,minutes,seconds and then feed that into the timelocal function from Time::Local. This will give a number that is easily sorted.
Here is a bit of code that describes this somewhat ugly and perhaps not very efficient routine:
#!/usr/bin/perl -w
use strict;
use Time::Local;
my $test = "G: CCCCC-01 :ADD : ORDER PROCESSED : *** OK ***:08/30/2003
+:14:24:58";
my $datetemp = substr($test,index($test,"***:")+4,length($test)-index(
+$test,"***:")+4);
print "DATE & TIME: $datetemp\n";
my $date = substr($datetemp,0,index($datetemp,":"));
print "DATE ONLY: $date\n";
my $time = substr($datetemp,index($datetemp,":")+1,length($datetemp)-i
+ndex($datetemp,":"));
print "TIME ONLY: $time\n";
my @datearray = split(/\//,$date);
my @timearray = split(/:/,$time);
my $ticks = timelocal($timearray[2],$timearray[1],$timearray[0],$datea
+rray[1],$datearray[0]-1,$datearray[2]);
print "CLOCK TICKS SINCE THE EPOCH: $ticks\n";
#usage of the timelocal function
# my $time = timelocal($sec,$min,$hour,$mday,$month,$year);
Again, this is somewhat ugly but it gets the job done in manner that is easy to understand. There is really no need to assign the variables $date and $time, I just thought it would make it easier to understand the process.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.