metaperl has asked for the wisdom of the Perl Monks concerning the following question:

How would you convert a DateTime instance to a unix timestamp?
  • Comment on How would you convert a DateTime instance to a unix timestamp?

Replies are listed 'Best First'.
Re: How would you convert a DateTime instance to a unix timestamp?
by moritz (Cardinal) on Mar 27, 2009 at 15:02 UTC
    By using the appropriate method $dt->epoch().
Re: How would you convert a DateTime instance to a unix timestamp?
by Anonymous Monk on Mar 27, 2009 at 15:19 UTC
    Do you mean a timestamp as seen on an ls ?

    If so just use the get methods.

    $dt->month_abbr() $dt->day() $dt->year()

    along with sprintf if you want to format it the same.
Re: How would you convert a DateTime instance to a unix timestamp?
by Marshall (Canon) on Mar 27, 2009 at 17:27 UTC
    I cobbled together a couple examples from previous code for you. I'm not sure where the date comes from that you want to convert to epoch. There is a Date/Time module, but sometimes you might wind up with something weird and have to do some conversions yourself or what you need is easy to do without the module.

    Update: Oh, I see you are already using the Date:Time module. That is a great one if you need multiple time zones, etc! dt->epoch uses functions in Time::Local. Almost all my work is with UTC and dates that are ok with Unix Epoch (usually don't even need the seconds). So just consider funcs below as an alternative if you don't need anything fancy.

    If you want the modification time from a file, etc. You need the stat function and use slice to get the value that you need, in this case 9. Example 1 shows how this works and one way to convert back to a string. To run the code, make a file called "testfile".

    If you already have numeric date values, then timegm() is a way, see example 2. Then I show 3 ways to convert back to a string. gmtime() returns a string in a scalar context and for lots of things that is just fine. When using the lower level functions, remember that months are numbered 0-11, not 1-12 and the year is based upon 1900 (1953 is 53). Hope this helps, have fun!

    #!/usr/bin/perl -w use strict; use Time::Local; use POSIX qw(strftime); ####### how to get mtime from a file ######### my @Month_text = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my $source_path = "./testfile"; (my $m_time) = (stat($source_path))[9] or die "stat failed"; my ($i_month, $day, $year, $hour, $minutes) = (localtime($m_time))[4,3 +,5,2,1]; $year += 1900; printf "%s %2d %4d %d:%d\n",$Month_text[$i_month],$day,$year,$hour,$mi +nutes,"\n"; #### parsing a date to put into timegm ###### my $test = "2004-09-03 23:58"; print "starting unparsed string=$test\n"; ( $year, my $month, my $days, my $hours, $minutes) = $test =~ m/\s*(\d+)-(\d+)-(\d+)\s*(\d+):(\d+)/; print "testing parsed vales =$year-$month-$days $hours:$minutes \n" +; ######### The key epoch second routine ##### #remember for timegm month[0-11] and year is delta from 1900 my $time = timegm(0,$minutes,$hours,$days,$month-1,$year-1900); print "epoch seconds $time\n"; print "running gmtime() to convert back to components\n"; (my $seconds, $minutes, $hours, $days, $month, $year) = gmtime($time); printf("the printf version=%04d-%02d-%02d %02d:%02d \n", $year+1900, $month+1, $days, $hours, $minutes); #note don't have to adjust as strftime is using C time structure print "now running strftime format".'=%Y-%m-%d %H:%M'."\n"; my $str = strftime( "strfttime version =%Y-%m-%d %H:%M ", 0, $minutes, $hours, $days, $month,$year); print "$str\n"; my $string = gmtime($time); print "string in scalar context from gmtime=$string\n"; __END__ Mar 27 2009 9:30 starting unparsed string=2004-09-03 23:58 testing parsed vales =2004-09-03 23:58 epoch seconds 1094255880 running gmtime() the printf version=2004-09-03 23:58 now running strftime format=%Y-%m-%d %H:%M strfttime version =2004-09-03 23:58 string in scalar context from gmtime=Fri Sep 3 23:58:00 2004