KBD

Keith Devens .com

Saturday, November 22, 2008 Flag waving
Darwin made it possible to be an intellectually-fulfilled atheist. – Richard Dawkins (The Blind Watchmaker)
← How Pixar Adds a New School of Thought to Disney - New York Timesreform - Third millenium syntax for Perl 5 OOP →

Daily link icon Thursday, February 2, 2006

How to programatically convert stupid 12 hour timestamps to 24 hours

Every time I have to manually parse a 12 hour timestamp (with "am" and "pm") into a 24 hour format I have to think about it to get the logic right. Now I won't have to again:

def twelveto24(hr, per):
    if hr == 12 and per == 'a': return 0
    if hr != 12 and per == 'p': return hr+12
    return hr

hours = range(1,13)
hours = zip(hours,['a']*12)+zip(hours,['p']*12)

for hr, per in hours:
    print twelveto24(hr, per),

#results
1 2 3 4 5 6 7 8 9 10 11 0 13 14 15 16 17 18 19 20 21 22 23 12

Update: For the hell of it, here's the Perl code I wrote to do the thing I actually had to do:

sub getTimestamp{
    #02/01/2006,05:06:04am
    $_[0] =~ m@\[(\d+)/(\d+)/(\d+),(\d+):(\d+):(\d+)([aApP])[mM]\]@;
    return "$3-$1-$2 ".sprintf("%02d",twelveto24($4,lc($7))).":$5:$6";
}

sub twelveto24{
    ($hr, $per) = @_;
    return 0 if($hr == 12 and $per eq 'a');
    return $hr+12 if($hr != 12 and $per eq 'p');
    return $hr;
}

print getTimestamp("[02/01/2006,12:06:04am]");
#2006-02-01 00:06:04
← How Pixar Adds a New School of Thought to Disney - New York Timesreform - Third millenium syntax for Perl 5 OOP →

Comments XML gif

Joseph Scott (http://joseph.randomnetworks.com/) wrote:

Will strtotime() work for this?

∴ Joseph Scott | 2-Feb-2006 12:21pm est | http://joseph.randomnetworks.com/ | #9088

Keith (http://keithdevens.com/) wrote:

Yeah, it would. And it even worked fine on the date format I just had to parse:

$ php -r "echo date('c',strtotime('02/01/2006,12:06:04am'));"
2006-02-01T00:06:04-06:00

(hey, central time zone Smiley winking)

But, I was working in Perl, and there are no built-in date parsing libraries in Perl.

Keith | 2-Feb-2006 3:08pm est | http://keithdevens.com/ | #9089

Joseph Scott (http://joseph.randomnetworks.com/) wrote:

I couldn't find a strtotime() for Perl, but CPAN has Date::Manip and Time::Format that should do it (I think).

∴ Joseph Scott | 2-Feb-2006 7:07pm est | http://joseph.randomnetworks.com/ | #9090

Keith (http://keithdevens.com/) wrote:

Yeah, CPAN has some things, but unfortunately there's nothing standard.

Keith | 2-Feb-2006 7:54pm est | http://keithdevens.com/ | #9091

Aidan Kehoe (http://www.parhasard.net/) wrote:

use Date::Parse qw/str2time/; for the next time that comes up ...

∴ Aidan Kehoe | 5-Mar-2006 7:21am est | http://www.parhasard.net/ | #9250

Keith (http://keithdevens.com/) wrote:

Of course, but like I said, there's nothing standard:

C:\temp>cat test.pl
use Date::Parse qw/str2time/;

print "hello";
C:\temp>test.pl
Can't locate Date/Parse.pm in @INC (@INC contains:
   C:/Perl/lib C:/Perl/site/lib .) at C:\temp\test.pl line 1.
BEGIN failed--compilation aborted at C:\temp\test.pl line 1.

(wrapped so it doesn't break my layout) BTW, I don't think I ever linked this article on handling dates and times with Perl, but I think that was one of the things I looked through. Point was, I couldn't go installing modules on the server I was working on.

Keith | 5-Mar-2006 11:09am est | http://keithdevens.com/ | #9251

Aidan Kehoe (http://www.parhasard.net/) wrote:

Ah, right. Yeah, Perl's a signficantly suckier language when you can't install modules; I wrote this earlier today, for example, and I'm not proud of it:

    # We need an actual file to pass to the openssl command-line
    # tool. Create it using the Perl temporary file module.
    my ($temp_file_handle, $temp_file_name)
        = tempfile DIR => $tempdir;

    # Flush writes to the file that we're going to hash
    # automatically.
    $| = 1, select $_ for select $temp_file_handle;

    # Write the secret prefix and the content to the file, so the hash
    # operation takes place on the same string on both ends
    print { $temp_file_handle } SIGNING_PREFIX.$content;
    close $temp_file_handle;
    my $hash = `/usr/bin/openssl dgst -sha1 < $temp_file_name`;
    unlink $temp_file_name;
∴ Aidan Kehoe | 5-Mar-2006 11:44am est | http://www.parhasard.net/ | #9256

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

November 2008
SunMonTueWedThuFriSat
 1
2345678
9101112131415
16171819202122
23242526272829
30 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 5 posts

Recent comments XML

new⇒Ubuntu Nvidia install not working for me... could use a hand

Cant change xorg.conf!

I'm not​the owner of it, don't ask me​why
but it...

I)orogon: Nov 22, 5:41am

Calif. Supreme Court to take up gay marriage ban

I would argue the point is not​definitional.  While the word​marriage is su...

Justin: Nov 20, 4:37pm

Java join function

Meh, don't have null strings in​your string arrays imo, but you're​welcome ...

Keith: Nov 19, 7:51pm

Girls, please don't get breast implants

sorry but another thing i have to​make a comment on about you​men...the men...

happynow: Nov 17, 11:36pm

Books by Vincent Cheung

to all Cheung​fans:

read:

http://www.progin​osko.com/aquascum/cheung.h...

Zamir: Nov 16, 9:07am

Generated in about 0.231s.

(Used 8 db queries)

mobile phone