Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Converting datetime string to valid localtime value (seconds)

by Anonymous Monk
on Oct 29, 2018 at 20:03 UTC ( #1224875=note: print w/replies, xml ) Need Help??


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";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1224875]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2020-07-06 21:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?