Posted By

wolfie on 11/10/06


Tagged

perl Unixtime


Versions (?)

Convert timestamp to Unixtime


 / Published in: Perl
 

  1. sub convertTime {
  2. #expects time in yyyy-mm-dd hh:mm:ss format
  3. my( $time ) = shift;
  4. my( $year, $month, $day, $hour, $minute, $sec ) = split( /\W/, $time );
  5. my $oneday = 24 * 3600; #for convenience
  6. my $utime = $sec + ($minute * 60) + ($hour * 3600); ## time in seconds on the day in question
  7. $year -= 1970;
  8. my @months = (31,28,31,30,31,30,31,31,30,31,30,31);
  9. for (my $i=0; $i < ($month-1); $i++ ) {
  10. $utime += ($months[$i] * $oneday);
  11. }
  12. $utime += ((($year - ($year%4))/4) * $oneday); ## take leap years into account
  13. if( ($year%4)==0 && $month < 3 ) { $utime -= $oneday; }
  14. $utime += (($day-1) * $oneday);
  15. $utime += ($year * 365 * $oneday);
  16. return $utime;
  17. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: amosshapira on January 13, 2008

Date::Parse will do this for you too.

You need to login to post a comment.