Return to Snippet

Revision: 1839
at November 10, 2006 02:59 by wolfie


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

Initial URL

                                

Initial Description

                                

Initial Title
Convert timestamp to Unixtime

Initial Tags
perl

Initial Language
Perl