views:

11292

answers:

8

I have a tab delimited file where each record has a timestamp field in 12-hour format:

mm/dd/yyyy hh:mm:ss [AM|PM].

I need to quickly convert these fields to 24-hour time:

mm/dd/yyyy HH:mm:ss.

What would be the best way to do this? I'm running on a Windows platform, but I have access to sed, awk, perl, python, and tcl in addition to the usual Windows tools.

A: 

Since you have multiple languages, I'll suggest the following algorithm.

1 Check the timestamp for the existence of the "PM" string.

2a If PM does not exist, simply convert the timestamp to the datetime object and proceed.

2b If PM does exist, convert the timestamp to the datetime object, add 12 hours, and proceed.

Dillie-O
Not accurate...the computation outlined mishandles 12:01 AM and 12:01 PM.
Jonathan Leffler
Good call, thanks for the catch.
Dillie-O
+3  A: 

Use Pythons datetime module someway like this:

import datetime

infile = open('input.txt')
outfile = open('output.txt', 'w')
for line in infile.readlines():
  d = datetime.strptime(line, "input format string")
  outfile.write(d.strftime("output format string")

Untested code with no error checking. Also it reads the entire input file in memory before starting. (I know there is plenty of room for improvements like with statement...I make this a community wiki entry if anyone likes to add something)

Martin
+4  A: 

Using Perl and hand-crafted regexes instead of facilities like strptime:

#!/bin/perl -w
while (<>)
{
    # for date times that don't use leading zeroes, use this regex instead:
    # (?:\d{1,2}/\d{1,2}/\d{4} )(\d{1,2})(?::\d\d:\d\d) (AM|PM)
    while (m%(?:\d\d/\d\d/\d{4} )(\d\d)(?::\d\d:\d\d) (AM|PM)%)
    {
        my $hh = $1;
        $hh -= 12 if ($2 eq 'AM' && $hh == 12);
        $hh += 12 if ($2 eq 'PM' && $hh != 12);
        $hh = sprintf "%02d", $hh;
        # for date times that don't use leading zeroes, use this regex instead:
        # (\d{1,2}/\d{1,2}/\d{4} )(\d{1,2})(:\d\d:\d\d) (?:AM|PM)
        s%(\d\d/\d\d/\d{4} )(\d\d)(:\d\d:\d\d) (?:AM|PM)%$1$hh$3%;
    }
    print;
}

That's very fussy - but also converts possibly multiple timestamps per line.

Note that the transformation for AM/PM to 24-hour is not trivial.

  • 12:01 AM --> 00:01
  • 12:01 PM --> 12:01
  • 01:30 AM --> 01:30
  • 01:30 PM --> 13:30

Now tested:

perl ampm-24hr.pl <<!
12/24/2005 12:01:00 AM
09/22/1999 12:00:00 PM
12/12/2005 01:15:00 PM
01/01/2009 01:56:45 AM
12/30/2009 10:00:00 PM
12/30/2009 10:00:00 AM
!

12/24/2005 00:01:00
09/22/1999 12:00:00
12/12/2005 13:15:00
01/01/2009 01:56:45
12/30/2009 22:00:00
12/30/2009 10:00:00


Added:

In What is a Simple Way to Convert Between an AM/PM Time and 24 hour Time in JavaScript, an alternative algorithm is provided for the conversion:

$hh = ($1 % 12) + ($2 eq 'AM') ? 0 : 12;

Just one test...probably neater.

Jonathan Leffler
$hh = ($hh + 12) % 24; seems pretty trivial to me...
Dave Sherohman
@Dave Sherohman: Are you sure? It would have to be conditional on AM/PM being present, at minimum - you don't need to add 12 hours to an AM time such as 10:00 AM. It also doesn't seem to handle the problem that 12:59 AM is two minutes before 01:01 AM.
Jonathan Leffler
Thanks Jonathan, worked like a charm :) I had to change the regex to `(?:\d{1,2}/\d{1,2}/\d{4} )(\d{1,2})(?::\d\d:\d\d) (AM|PM)`, but other than that this was spot on.
Patrick Cuff
That was the "That's very fussy" part - I figured that you'd be able to deal with that if necessary. The tricky bit is the add/sub 12 hours gunk; there are a surprising number of false answers from people who've not thought through the necessary steps.
Jonathan Leffler
You could also use \d\d? instead of \d{1,2}, which is shorter :D
Jonathan Leffler
Thanks again. I am by no means a regex expert; I tend to hack my way through until I get something that works.
Patrick Cuff
A: 

This might be too simple thinking, but why not import it into excel, select the entire column and change the date format, then re-export as a tab delimited file? (I didn't test this, but it somehow sounds logical to me :)

Dave
It sounds moderately complex to me. Not wrong - just complex.
Jonathan Leffler
I have about 450,000+ lines in the file and we only have Office 2003 here at work, so Excel is limited to 65K lines.
Patrick Cuff
I wasn't familiar with Excel's limit, sorry. @Jonathan: Complex? Really? I guess our opinions are different on that. Writing Perl scripts and such feels more complex to me :)
Dave
A: 

Here i have converted 24 Hour system to 12 Hour system. Try to use this method for your problem.

    DateFormat fmt = new SimpleDateFormat("yyyyMMddHHssmm");

    try {
        Date date =fmt.parse("20090310232344");

        System.out.println(date.toString());
        fmt = new SimpleDateFormat("dd-MMMM-yyyy hh:mm:ss a ");
        String dateInString = fmt.format(date);
        System.out.println(dateInString);


    } catch (Exception e) {
        System.out.println(e.getMessage());
    } 

  RESULT:  
   Tue Mar 10 23:44:23 IST 2009   
   10-March-2009 11:44:23 PM
A: 

To just convert the hour field, in python:

def to12(hour24):
    return (hour24 % 12) if (hour24 % 12) > 0 else 12

def IsPM(hour24):
    return hour24 > 11

def to24(hour12, isPm):
    return (hour12 % 12) + (12 if isPm else 0)

def IsPmString(pm):
    return "PM" if pm else "AM"

def TestTo12():    
    for x in range(24):
        print x, to12(x), IsPmString(IsPM(x))

def TestTo24():
    for pm in [False, True]:
        print 12, IsPmString(pm), to24(12, pm)
        for x in range(1, 12):
            print x, IsPmString(pm), to24(x, pm)
Erik
A: 

select from 1440 ordered pairs

katz
A: 

below code is perfect! Thanks $hh = ($1 % 12) + ($2 eq 'AM') ? 0 : 12;

Yasir