Hi.
I wrote some functions to manipulate the date I hope it will useful for you too.
use Time::Local qw(timelocal); # This function will return the date in a second from epoch time # Example: # my $timeInSec = getTimeInSec('2005-03-28 12:00:00'); # BUT # actually function work with format '2005-03-28' if you need full dat +e/time you have to make some changes. # If you will have problem with it please contact me. sub getTimeInSec { my $time = shift; my ( $year,$month,$day ); if ( $time =~ /^ (\d\d\d\d) # Parsing Year [-\x20.\/]? # Delimiter (1[0-2]|0?[1-9]) # Parsing Month [-\x20.\/]? # Delimiter (3[0-1]|[1-2][0-9]|0?[1-9]) # Parsing Day $ /x or die "Date format '$time' is wrong" ) { ( $year,$month,$day ) = ( $1,$2,$3 ); die "Date format '$time' is wrong" unless &checkDateFormat( $ +year,$month,$day ); } return timelocal(00,00,00,$day,$month-=1,$year-=1900); } # This function check if the given string is a correct date. sub checkDateFormat { my ( $year,$month,$day ) = @_; my %month_day = ('01' => 31 ,'03' => 31 ,'02' => &checkLeapYear($year) ,'04' => 30 ,'05' => 31 ,'06' => 30 ,'07' => 31 ,'08' => 31 ,'09' => 30 ,'10' => 31 ,'11' => 30 ,'12' => 31); $month = '0' . $month if length( $month ) == 1; return 1 if ($day <= $month_day{ $month }); return 0; } sub checkLeapYear { return 29 unless $_[0] % 400; return 28 unless $_[0] % 100; return 29 unless $_[0] % 4; return 28; } # To use this function you can write # printf "%04d-%02d-%02d %02d:%02d:%02d", &timenow(); # or # my $dateTime = sprintf "%04d-%02d-%02d %02d:%02d:%02d", &timenow(get +TimeInSec('2005-03-28 12:00:00')); sub timenow { my ($sec,$min,$hour,$day,$month,$year) = localtime(defined $_[0] ? + $_[0] : time); return $year+1900,$month+1,$day,$hour,$min,$sec; }

Vlad.

In reply to Re: Converting to epoch time. by VladSu
in thread Converting to epoch time. by TASdvlper

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.