jonnyfolk has asked for the wisdom of the Perl Monks concerning the following question:
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:
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?#!/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;
If there are improvements I could make to this I'd also be grateful to know of them
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: scalar localtime() to timestamp
by Zaxo (Archbishop) on Oct 09, 2003 at 04:56 UTC | |
by jonnyfolk (Vicar) on Oct 09, 2003 at 05:14 UTC | |
|
Re: scalar localtime() to timestamp
by EdwardG (Vicar) on Oct 09, 2003 at 07:11 UTC | |
|
Re: scalar localtime() to timestamp
by graff (Chancellor) on Oct 09, 2003 at 05:25 UTC | |
|
Re: scalar localtime() to timestamp
by Aragorn (Curate) on Oct 09, 2003 at 06:50 UTC | |
by jonnyfolk (Vicar) on Oct 09, 2003 at 07:47 UTC | |
by Aragorn (Curate) on Oct 09, 2003 at 10:07 UTC | |
by Anonymous Monk on Oct 09, 2003 at 07:23 UTC | |
by Aragorn (Curate) on Oct 09, 2003 at 10:53 UTC | |
|
Re: scalar localtime() to timestamp
by jeffa (Bishop) on Oct 09, 2003 at 21:33 UTC |