Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > converting date/time to timestamp

Reply
Thread Tools

converting date/time to timestamp

 
 
Peter Pippinger
Guest
Posts: n/a
 
      10-21-2003
Hello NG,

i know, this might be also a FAQ, but i havn´t found this in the Perldoc. Maybe
someone can point me to the right place in the Handbook?

I have two variables:
$date="10/21/2003"
$time="18:30"

And i want to have the UNIX-timestamp for these variables in $timestamp.

How can i convert this?

Thanx for any hints and best regards!
Peter


 
Reply With Quote
 
 
 
 
David Oswald
Guest
Posts: n/a
 
      10-21-2003

"Peter Pippinger" <> wrote in message
news:bn2mhu$frp$03$...

> i know, this might be also a FAQ, but i havn´t found this in the Perldoc.

Maybe
> someone can point me to the right place in the Handbook?
>
> I have two variables:
> $date="10/21/2003"
> $time="18:30"
>
> And i want to have the UNIX-timestamp for these variables in $timestamp.


The process is to convert those two variables to seconds since epoc, and
then plug that into the 'localtime' function in scalar context. You can
look at perlfaq4 from the POD that came with your Perl installation for a
discussion of dates and times, and at perlfunc "localtime" and "time" for a
discussion of Perl's built-in time functions.

There is also a very convenient module on CPAN called Date::Manip. It, and
Date::Transform are both very good at deciphering date/time strings in many
different formats and converting them into a more standardized format. You
can find those modules along with their documentation at search.cpan.org.

Good luck.



 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      10-21-2003
Andrew Shitov wrote:
>
> use Time::Local;
>
> $date="10/21/2003";
> $time="18:30";
>
> my ($mon, $mday, $year) = split /\//, $date;
> my ($hour, $min) = split /:/, $time;
> print timelocal (0, $min, $hour, $mday, $mon, $year);


That prints the number of epoch seconds representing November, 21.
-----------------------------------------------------^^^^^^^^

The last line should rather be:

print scalar localtime timelocal(
0, $min, $hour, $mday, $mon - 1, $year);

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting datetime with tzinfo to unix timestamp Michael Torrie Python 0 03-12-2010 11:15 PM
converting Date() to unix timestamp format? marc@gaspdesign.co.uk Javascript 6 03-06-2007 10:54 PM
Convert to NTP Timestamp Manzoorul Hassan Perl 1 03-11-2005 05:37 PM
converting timestamp to jan 1 2003 3:40 PM Max Java 4 10-14-2003 04:16 AM
log timestamp format Bill F Cisco 3 07-24-2003 05:40 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57