Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Convert date & time to UNIX time

by Marshall (Prior)
on Jul 30, 2009 at 23:26 UTC ( #784768=note: print w/ replies, xml ) Need Help??


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.

#!/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.


Comment on Re: Convert date & time to UNIX time
Select or Download Code
Re^2: Convert date & time to UNIX time
by ikegami (Pope) on Jul 30, 2009 at 23:36 UTC

    But yes there are ways to convert this string to a number of seconds since "epoch" time.

    Time::Local's timegm and timelocal being rather convenient if that's all one wants to do. Any date and/or time manipulation module will do the trick too.

Re^2: Convert date & time to UNIX time
by ikegami (Pope) on Aug 04, 2009 at 03:54 UTC

    "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.

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://784768]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2014-07-31 02:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (244 votes), past polls