#!/usr/bin/perl -w use strict; use HTTP::Date; use Time::HiRes; $\ ="\n"; my %month = (Jan => 0, Feb => 31, Mar => 59, Apr => 90, May => 120, Jun => 151, Jul => 181, Aug => 212, Sep => 243, Oct => 273, Nov => 304, Dec => 334); my $httpdate = 'Wed, 25 Aug 2010 09:56:38 GMT'; sub mytime { my $year = substr($_[0],12,4); my $days = ($year-1970)*365+ # days in full year passed sprintf('%d', ($year-1970)/4)+ # add leap days in years passed $month{substr($_[0],8,3)}+ # add days in full months passed (substr($_[0],5,2)-1)+ #is doing a -1 to day of month needed? (((($year)%4) == 0 && (($month{substr($_[0],8,3)}+substr($_[0],5,2)) > 59))?1:0); #have we passed the leap day in the current year? return $days*86400+(substr($_[0],17,2)*60*60)+(substr($_[0],20,2)*60)+substr($_[0],23,2); #hrs/mins/secs } sub mytime2 { my $year = substr($_[0],12,4); my $days = ($year-1970)*365+ # days in full year passed ((($year-1970) - (($year-1970) % 4))/4)+ # add leap days in years passed $month{substr($_[0],8,3)}+ # add days in full months passed (substr($_[0],5,2)-1)+ #is doing a -1 to day of month needed? (((($year)%4) == 0 && (($month{substr($_[0],8,3)}+substr($_[0],5,2)) > 59))?1:0); #have we passed the leap day in the current year? return $days*86400+(substr($_[0],17,2)*60*60)+(substr($_[0],20,2)*60)+substr($_[0],23,2); #hrs/mins/secs } my $t_a = Time::HiRes::time(); for (0 .. 100000) {mytime($httpdate);} print 'with sprintf int '.(Time::HiRes::time()-$t_a)."\n";; $t_a = Time::HiRes::time(); for (0 .. 100000) {mytime2($httpdate);} print 'with algebra int '.(Time::HiRes::time()-$t_a)."\n";; $t_a = Time::HiRes::time(); for (0 .. 100000) {str2time($httpdate);} print 'with HTTP date '.(Time::HiRes::time()-$t_a)."\n";; print "algorithm outputs"; print mytime($httpdate); print mytime2($httpdate); print str2time($httpdate);