FAQ
Hi all,

I have a Perl script that uses Date::Calc to calculate difference between
two datetime variables using Decode_Month and Delta_YMDHMS.

( $yy, $mm, $dd, $hh, $mi, $ss ) = Delta_YMDHMS( $logical_yy, $logical_mm,
$logical_dd,
$logical_hh,
$logical_mi, $logical_ss,
$primary_yy,
$primary_mm, $primary_dd,
$primary_hh,
$primary_mi, $primary_ss );
At the moment, the script works well if the datetime format is 01/Jan/2010
20:01:27, i.e. DD/MON/YYYY HH:MI:SS with the time in a 24-hour format.

Unfortunately, I suddenly have to to deal with an input file where the
datetime format is 02-Apr-2010 3:41:23 p.m., i.e. DD-MON-YYYY HH:MI:SS
a.m./p.m., so now my Delta_YMDHMS does not work as expected.

For example, my current script calculate time difference between 02-Apr-2010
3:41:23 p.m. and 02-Apr-2010 12:59:48 p.m. as 9 hours, 18 minutes and 25
seconds instead of 3 hours 18 minutes and 25 seconds.

Can anyone from the list advise if there is any way to convert the 12-hour
format to 24-hour fomat?

At the moment, am looking at having to follow as suggested in
http://stackoverflow.com/questions/440061/convert-12-hour-date-time-to-24-hour-date-time.
That is a bit complicated so am kinda hoping there is a quicker and easier
way.

I can't see anything from Date::Calc documentation that mention whether
converting between 12-hour to 24-hour format is possible or not.

Any quick response will be very much appreciated as I needed to get this
done quite urgently.

Thanks in advance.

Still Googling for the meantime ... :-)

Search Discussions

  • Thomas Bätzler at Apr 7, 2010 at 12:58 pm

    newbie01 perl asked:
    Unfortunately, I suddenly have to to deal with an input file where the
    datetime format is 02-Apr-2010 3:41:23 p.m., i.e. DD-MON-YYYY HH:MI:SS
    a.m./p.m., so now my Delta_YMDHMS does not work as expected.
    You can convert from a.m./p.m. to standard 24 hours using a simple RE:

    #!/usr/bin/perl -w

    use strict;

    while( my $line = <DATA> ){
    $line =~ s/(\d+)(:\d+:\d+)\s+(a|p)\.m\./($3 eq 'a')?($1%12).$2:($1%12+12).$2/e;
    print $line;
    }

    __DATA__
    02-Apr-2010 12:41:23 a.m.
    02-Apr-2010 12:41:23 p.m.
    02-Apr-2010 3:41:23 a.m.
    02-Apr-2010 3:41:23 p.m.

    HTH,
    Thomas

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedApr 7, '10 at 12:06p
activeApr 7, '10 at 12:58p
posts2
users2
websiteperl.org

2 users in discussion

Thomas Bätzler: 1 post Newbie01 perl: 1 post

People

Translate

site design / logo © 2022 Grokbase