Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!jarthur!elroy.jpl.nasa.gov!cit-vax!ktl From: ktl@wag240.caltech.edu (Kian-Tat Lim) Newsgroups: comp.lang.perl Subject: Re: A head start (was Re: reverse of localtime()) Message-ID: <14036@cit-vax.Caltech.Edu> Date: 1 Mar 90 01:05:01 GMT References: <1176@frankland-river.aaii.oz.au> <1990Feb28.160935.28518@iwarp.intel.com> Sender: news@cit-vax.Caltech.Edu Reply-To: ktl@wag240.caltech.edu (Kian-Tat Lim) Organization: California Institute of Technology, Pasadena, CA Lines: 68 In-reply-to: merlyn@iwarp.intel.com (Randal Schwartz) Here's something I put together for a perl-based backup script. It only takes a specific date format, but it'll work until 2070 if your Unix uses an unsigned long for times. Things also get hairy around standard/daylight changeover time; I haven't looked to see if it works properly there. ===== CUT HERE ===== sub date2secs { ;# Convert output of "date" to seconds since Jan 1 1970 00:00:00 ;# Derived from GNU tar (and others) getdate.y, though not nearly as ;# smart about differing formats ;# Call like this: ;# do "date2secs.pl"; ;# chop($date = `date`); ;# $secs = &date2secs($date); ;# Days before first of each month local(%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); ;# Hours added to each zone to get GMT; single letters are military local(%zone) = ("nst",3.5, "ast",4, "adt",3, "est",5, "edt",4, "cst",6, "cdt",5, "mst",7, "mdt",6, "pst",8, "pdt",7, "yst",9, "ydt",8, "hst",10, "hdt",9, "gmt",0, "bst",-1, "eet",0, "eest",-1, "met",-1, "mest",-2, "wet",-2, "west",-3, "jst",-9, "aest",-10, "aesst",-11, "acst",-9.5, "acsst",-10.5, "awst",-8, "a",1, "b",2, "c",3, "d",4, "e",5, "f",6, "g",7, "h",8, "i",9, "k",10, "l",11, "m",12, "n",-1, "o",-2, "p",-3, "q",-4, "r",-5, "s",-6, "t",-7, "u",-8, "v",-9, "w",-10,"x",-11,"y",-12, "z",0 ); local($wday,$mm,$dd,$tm,$zz,$yy) = split(/ +/,$_[0]); $zz =~ tr/A-Z/a-z/; local($hr,$min,$sec) = split(/[:]/,$tm); ;# Note that the next line will fail in 2070 and the one after will ;# fail in 2101 -- but Unix's signed long for times fails in 2038! ;# We also fail to take into account dates before the epoch (1/1/1970). $yy += 1900 if $yy < 100; $yy += 100 if $yy <= 69; local($jdate) = 365 * ($yy - 1970) + int(($yy - 1970 + 1) / 4); $jdate += $mdays{$mm} + $dd - 1; $jdate++ if $mm ne "Jan" && $mm ne "Feb" && $yy % 4 == 0 && ($yy % 100 != 0 || $yy % 400 == 0); $jdate *= 86400; $jdate += (($hr + $zone{$zz}) * 60 + $min) * 60 + $sec; } ;# Return 1 to indicate successful 'do' 1; ===== CUT HERE ===== -- Kian-Tat Lim (ktl@wagvax.caltech.edu, KTL @ CITCHEM.BITNET, GEnie: K.LIM1)