I have some dates in the format scalar localtime() which I want to convert to create a timestamp.

I thought this would be quite simple, as the scalar localtime() is automatic and so, I thought, would be the re-conversion. However much reading left me with the impression that I had to do a little more work to make this conversion.

I came up with the following script:

#!/usr/bin/perl -wT use strict; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use CGI ':standard'; use Time::Local; my $input = param('date'); print "Content-type: text/html\n\n"; my $ILLEGAL_CHARS = qr/[^\w:]/; if ($input =~ /$ILLEGAL_CHARS/;) { print "You do not have the right to enter that information"; } my @date = split (/ /, $input); my ($day,$mon,$mday,$hms,$year) = @date; my @date1 = split (/:/, $hms); my ($hour,$min,$sec) = @date1; my %mon_writ = ( Jul => '6', Aug => '7', Sept => '8', Oct => '9', ); $mon = $mon_writ{key}; ($sec, $min, $hour, $mday, $mon, $year) =~ s/^0+//; $year = $year - 1900; my $TIME = timegm($sec, $min, $hour, $mday, $mon, $year); print $TIME;
which appears to give me the correct results. I just thought that I'd ask the question - did I miss something, is there a one or two liner which would do this job?

If there are improvements I could make to this I'd also be grateful to know of them


In reply to scalar localtime() to timestamp by jonnyfolk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.