in reply to Convert date & time to UNIX time
The easy way in Perl is with regular expressions, like below. Not sure what else you need. \d{4}means exactly 4 digits, \d{2} exactly 2 digits, etc.
The low level "unix time" is a number of seconds from an arbitrary point, usually jan, 1, 1970 at midnight. Without more info, I'm not sure what you are doing or what you need. But yes there are ways to convert this string to a number of seconds since "epoch" time.#!/usr/bin/perl -w use strict; my $string = "20090719-074248"; my ($year,$month,$day,$hour,$min,$sec) = my @data = ($string=~ m/(\d{4})(\d{2})(\d{2})-(\d{2})(\d{2})(\d{2})/); print "data = @data\n"; #digits as a list print "year = $year\n", "month = $month\n", "day = $day\n", "hour = $hour\n", "min = $min\n", "sec = $sec\n"; __END__ PRINTS: data = 2009 07 19 07 42 48 year = 2009 month = 07 day = 19 hour = 07 min = 42 sec = 48
This #seconds as an integer format is useful when adding or subtracting dates ... conversion is from time like above to total #seconds for each date, then do arithmetic and then convert resulting number back to individual date components.
For sorting dates, you have the perfect format as a very simple sort will yield the right date order.
Update:As Ikegami points out, these are the 4 main time functions.
Careful reading of the man pages is required as there are some "tricks", like $mon ranges from 0..11, while $mday ranges from 1..31 and $year is actual year -1900.
Oh, if you aren't used to time zones, gmtime means in old terminology Greenwich Mean Time, the time at the "prime meridian" in Greenwich, England. This is called UTC now, a French acronym, which means in English Universal Coordinated Time.use Time::Local; #These are the inverse functions of localtime() and gmtime() #which are Perl "built-in functions".. (no "use" statement needed) $time = timelocal($sec,$min,$hour,$mday,$mon,$year); $time = timegm($sec,$min,$hour,$mday,$mon,$year);
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Convert date & time to UNIX time
by ikegami (Patriarch) on Jul 30, 2009 at 23:36 UTC | |
Re^2: Convert date & time to UNIX time
by ikegami (Patriarch) on Aug 04, 2009 at 03:54 UTC | |
by Marshall (Canon) on Aug 04, 2009 at 20:51 UTC |
In Section
Seekers of Perl Wisdom