#!/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);
####
C:\Documents and Settings\Owner\Desktop>perl myhttpdate2.pl
with sprintf int 0.413187026977539
with algebra int 0.321068048477173
with HTTP date 1.31241106987
algorithm outputs
1282730198
1282730198
1282730198
C:\Documents and Settings\Owner\Desktop>
####
#!/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 %year = (1901 => -25202, 1902 => -24837, 1903 => -24472, 1904 => -24107, 1905 => -23741, 1906 => -23376,
1907 => -23011, 1908 => -22646, 1909 => -22280, 1910 => -21915, 1911 => -21550, 1912 => -21185,
1913 => -20819, 1914 => -20454, 1915 => -20089, 1916 => -19724, 1917 => -19358, 1918 => -18993,
1919 => -18628, 1920 => -18263, 1921 => -17897, 1922 => -17532, 1923 => -17167, 1924 => -16802,
1925 => -16436, 1926 => -16071, 1927 => -15706, 1928 => -15341, 1929 => -14975, 1930 => -14610,
1931 => -14245, 1932 => -13880, 1933 => -13514, 1934 => -13149, 1935 => -12784, 1936 => -12419,
1937 => -12053, 1938 => -11688, 1939 => -11323, 1940 => -10958, 1941 => -10592, 1942 => -10227,
1943 => -9862, 1944 => -9497, 1945 => -9131, 1946 => -8766, 1947 => -8401, 1948 => -8036, 1949 => -7670,
1950 => -7305, 1951 => -6940, 1952 => -6575, 1953 => -6209, 1954 => -5844, 1955 => -5479, 1956 => -5114,
1957 => -4748, 1958 => -4383, 1959 => -4018, 1960 => -3653, 1961 => -3287, 1962 => -2922, 1963 => -2557,
1964 => -2192, 1965 => -1826, 1966 => -1461, 1967 => -1096, 1968 => -731, 1969 => -365, 1970 => 0,
1971 => 365, 1972 => 730, 1973 => 1096, 1974 => 1461, 1975 => 1826, 1976 => 2191, 1977 => 2557,
1978 => 2922, 1979 => 3287, 1980 => 3652, 1981 => 4018, 1982 => 4383, 1983 => 4748, 1984 => 5113,
1985 => 5479, 1986 => 5844, 1987 => 6209, 1988 => 6574, 1989 => 6940, 1990 => 7305, 1991 => 7670,
1992 => 8035, 1993 => 8401, 1994 => 8766, 1995 => 9131, 1996 => 9496, 1997 => 9862, 1998 => 10227,
1999 => 10592, 2000 => 10957, 2001 => 11323, 2002 => 11688, 2003 => 12053, 2004 => 12418, 2005 => 12784,
2006 => 13149, 2007 => 13514, 2008 => 13879, 2009 => 14245, 2010 => 14610, 2011 => 14975, 2012 => 15340,
2013 => 15706, 2014 => 16071, 2015 => 16436, 2016 => 16801, 2017 => 17167, 2018 => 17532, 2019 => 17897,
2020 => 18262, 2021 => 18628, 2022 => 18993, 2023 => 19358, 2024 => 19723, 2025 => 20089, 2026 => 20454,
2027 => 20819, 2028 => 21184, 2029 => 21550, 2030 => 21915, 2031 => 22280, 2032 => 22645, 2033 => 23011,
2034 => 23376, 2035 => 23741, 2036 => 24106, 2037 => 24472, 2038 => 24837);
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
}
sub mytime3 { #yr table
my $year = substr($_[0],12,4);
my $days = $year{$year}+ #use year day hash
$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 mytime4 { #yr table and full substr caching
my $year = substr($_[0],12,4);
my $month = substr($_[0],8,3);
my $day = substr($_[0],5,2);
my $days = $year{$year}+ #use year day hash
$month{$month}+ # add days in full months passed
($day-1)+ #is doing a -1 to day of month needed?
(((($year)%4) == 0 && (($month{$month}+$day) > 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 mytime5 { #year table and removed redundant year caching
my $days = $year{substr($_[0],12,4)}+ #use year day hash
$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?
((((substr($_[0],12,4))%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) {mytime3($httpdate);}
print 'with yr table '.(Time::HiRes::time()-$t_a)."\n";;
$t_a = Time::HiRes::time();
for (0 .. 100000) {mytime4($httpdate);}
print 'with yr table and full substr caching '.(Time::HiRes::time()-$t_a)."\n";;
$t_a = Time::HiRes::time();
for (0 .. 100000) {mytime5($httpdate);}
print 'with yr table no cache '.(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 mytime3($httpdate);
print mytime4($httpdate);
print mytime5($httpdate);
print str2time($httpdate);
####
C:\Documents and Settings\Owner\Desktop>perl myhttpdate2.pl
with sprintf int 0.343380928039551
with algebra int 0.312686204910278
with yr table 0.241981983184814
with yr table and full substr caching 0.273297071456909
with yr table no cache 0.235360145568848
with HTTP date 1.24872398376465
algorithm outputs
1282730198
1282730198
1282730198
1282730198
1282730198
1282730198
C:\Documents and Settings\Owner\Desktop>