http://qs321.pair.com?node_id=1224875


in reply to Converting datetime string to valid localtime value (seconds)

For those who got here (like me) who want to reverse a *string* created by scalar(localtime(time())), the following example shows how to do it:
use Time::Local; @t = split(/[ :]/,scalar(localtime(time))); %mon2num = qw(jan 0 feb 1 mar 2 apr 3 may 4 jun 5 jul 6 aug 7 sep 8 oc +t 9 nov 10 dec 11); $mon = $mon2num{lc(substr($t[1],0,3))}; print(scalar(localtime(time())),"\n", scalar(localtime(timelocal($t[5],$t[4],$t[3],$t[2],$mon,$t[6]))) +,"\n");' Mon Oct 29 15:59:06 2018 Mon Oct 29 15:59:06 2018

Replies are listed 'Best First'.
Re^2: Converting datetime string to valid localtime value (seconds)
by hippo (Chancellor) on Oct 30, 2018 at 09:14 UTC

    Alternatively, don't parse the monthnames yourself and use the core module Time::Piece instead:

    #!/usr/bin/env perl use strict; use warnings; use Time::Piece; my $source = 'Mon Oct 29 15:59:06 2018'; my $time = Time::Piece->strptime ($source, '%a %b %d %T %Y'); # Now use it however you wish print $time->datetime . "\n"; print "Today is " . $time->fullday . "\n";