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.
Here i have converted 24 Hour system to 12 Hour system. Try to use this method for your problem.
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.
In Python: Converting 12hr time to 24hr time
Use Pythons datetime module someway like this:
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)
Using Perl and hand-crafted regexes instead of facilities like strptime:
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.
Now tested:
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:
Just one test...probably neater.
To just convert the hour field, in python: