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

In reply to Re: How would you convert a DateTime instance to a unix timestamp? by Marshall
in thread How would you convert a DateTime instance to a unix timestamp? by metaperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.