I’ve tried pretty hard to find an example of this particular conversion (these date formats) using Perl regex, and to no avail. Can someone please help me convert dates between these formats?
Dec 26 2012 12:00AM ==> 201212126
The following was my initial attempt but it runs too slow (obviously, I used substr like 5 times which is ridiculous).
# Format the input time to yyyymmdd from 'Dec 26 2012 12:00AM' like format.
sub formatTime($)
{
#Get passed in value of format 'Dec 26 2012 12:00AM'.
my $col = shift;
if (substr($col, 4, 1) eq " "){
substr($col, 4, 1) = "0";
}
return substr($col, 7, 4).$months{substr($col, 0, 3)}.substr($col, 4, 2);
}
Note: This is for work, for converting input files to a very large DB ingestion, and unfortunately python is not supported on platform which is my language of choice for scripting. I tried making my own Perl regex, but I just don’t have time to read up and figure it out while doing other parts of this. I already wasted most of yesterday writing Perl scripts and learning on the fly for the rest of it, this conversion just it taking me too long.
I advice you using module DateTime + DateTime::Format::Strptime.
Output