11

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.

Alex B
  • 24,678
  • 14
  • 64
  • 87
Patrick Cuff
  • 28,540
  • 12
  • 67
  • 94

8 Answers8

12

It is a 1-line thing in python:

time.strftime('%H:%M:%S', time.strptime(x, '%I:%M %p'))

Example:

>>> time.strftime('%H:%M:%S', time.strptime('08:01 AM', '%I:%M %p'))
'08:01:00'
>>> time.strftime('%H:%M:%S', time.strptime('12:01 AM', '%I:%M %p'))
'00:01:00'
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Biswanath
  • 121
  • 1
  • 2
12

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.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • @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 Jan 13 '09 at 19:34
  • 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 Jan 14 '09 at 11:55
  • 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 Jan 14 '09 at 14:36
  • You could also use \d\d? instead of \d{1,2}, which is shorter :D – Jonathan Leffler Jan 14 '09 at 14:38
  • 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 Jan 14 '09 at 15:24
  • I think the parentheses in the "simple" method are incorrect. Should be `$hh = ($1 % 12) + ($2 eq 'AM' ? 0 : 12);` – Mark Eirich Apr 16 '13 at 16:23
  • `$hh = ($1 % 12) + (($2 eq 'AM') ? 0 : 12);` solution worked ffor me. – Sandeep May 16 '16 at 13:15
4

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 Thurau
  • 7,564
  • 7
  • 43
  • 80
2

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
  • 743
  • 1
  • 5
  • 11
0

In Python: Converting 12hr time to 24hr time

import re
time1=input().strip().split(':')
m=re.search('(..)(..)',time1[2])
sec=m.group(1)
tz=m.group(2)  
if(tz='PM'):
     time[0]=int(time1[0])+12
     if(time1[0]=24):
            time1[0]-=12
     time[2]=sec         
else:
     if(int(time1[0])=12):
            time1[0]-=12
     time[2]=sec


print(time1[0]+':'+time1[1]+':'+time1[2])
Harsh Seth
  • 5
  • 1
  • 1
  • 3
0

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 :)

  • It sounds moderately complex to me. Not wrong - just complex. – Jonathan Leffler Jan 13 '09 at 18:52
  • 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 Jan 13 '09 at 19:13
  • 1
    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 :) –  Jan 14 '09 at 02:56
0

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 
-1

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
  • 29,277
  • 14
  • 101
  • 140