datetime - How to convert unknown date/time format into normal using Perl? -


this question has answer here:

i have date/time this: 2015-07-31t13:30:00.000+01:00 , want convert normal date , time using perl , time::piece->strptime

here code:

sub changedateformat {   ($date, $fromformat, $toformat) = (@_);   return time::piece->strptime($date, $fromformat)->strftime($toformat); } 

the call:

print changedateformat($that_date, '%y-%m-%dt%h:%m:%s.%n+%z', '%y:%m:%d'); 

i think .000 nano seconds , +01.00 stands time zone. given code gives this: error parsing time @ /usr/lib64/perl5/time/piece.pm line 470

any appreciated.

there's couple of problems think.

%n isn't in strftime manpage. might not work.

and %z - i'm pretty sure +01:00 isn't valid.

  %z     +hhmm or -hhmm numeric timezone (that is, hour ,           minute offset utc). (su) 

this works though:

my $date = '2015-07-31t13:30:00+0100';  $fromformat = '%y-%m-%dt%h:%m:%s%z';  print time::piece->strptime($date, $fromformat); 

so i'd suggest - unless milliseconds important - strip via regex, , likewise timezone. (and important, don't think time::piece ms resolution anyway)

you can use regular expression 'correct' input date if inclined. i'm unsure if fits use case but:

$date =~ s/\+(\d{2}):(\d{2})$/+$1$2/; $date =~ s/\.\d{3}+/+/; 

Comments