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.
#!/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
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. 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.
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);
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. | [reply] [d/l] [select] |
| [reply] [d/l] [select] |
"UTC" isn't French. It would be "TUC" in French. It's a compromise that uses the letters from both the French and English acronym while using neither.
The metric unit system is known as "SI", the French acronym for "Système International d'Unités". Maybe you were thinking of that.
| [reply] |
I didn't know the politics of UTC. But makes sense, sometimes a good compromise is sometimes one that makes everybody a little bit unhappy!
On other abbreviations, folks may come across "Z", acronym "Zulu". 13:15 Z would be "thirteen fifteen Zulu". Zulu is an abbreviation of an abbreviation! When handwritten, often a "-" is drawn through the Z to make it less confusable with the number "2". Z, GMT, UTC basically all mean the same thing.
| [reply] |
use DateTime;
use DateTime::Format::Strptime;
my $parser = DateTime::Format::Strptime->new
( pattern => '%Y%m%d-%H%M%S' );
my $dt = $parser->parse_datetime( "20090719-074248" ) ;
warn $dt->epoch;
| [reply] [d/l] |
You have ambiguity in your timestamps at least once a year, depending on your time zone. If these are GMT only, you should be OK, but when DST stops, your timestamps will be ambiguous for a period of time. Not sure if this is an issue, but it may need to be taken into account, depending on your application.
--MidLifeXis
The tomes, scrolls etc are dusty because they reside in a dusty old house, not because they're unused. --hangon in this post
| [reply] |
I almost always use Date::Manip for date parsing. It might be overkill, but I sometimes think it is smarter than I am. | [reply] |