Path: utzoo!attcan!uunet!snorkelwacker!apple!oliveb!orc!mipos3!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.perl Subject: A head start (was Re: reverse of localtime()) Message-ID: <1990Feb28.160935.28518@iwarp.intel.com> Date: 28 Feb 90 16:09:35 GMT References: <1176@frankland-river.aaii.oz.au> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 53 In-Reply-To: pem@frankland-river.aaii.oz.au (Paul E. Maisano) In article <1176@frankland-river.aaii.oz.au>, pem@frankland-river (Paul E. Maisano) writes: | I am writing a perl program at the moment which is doing a lot of | manipulation of dates and times. | | I am maintaining a text file which contains dates such as 24/04/89 and | to manipulate these dates easily I would like to convert that to "the | number of seconds since Jan 1970". This is the reverse of what localtime() | does. [I can't keep the number of seconds in the file rather than the | date either since the file may be modified by hand.] Well, as a headstart, here's what I did for 'newslat', which had to convert a Usenet-standard date into an integer: %offset = ( 'Jan89', 0, 'Feb89', 31, 'Mar89', 59, 'Apr89', 90, 'May89', 120, 'Jun89', 151, 'Jul89', 181, 'Aug89', 212, 'Sep89', 243, 'Oct89', 273, 'Nov89', 304, 'Dec89', 334, 'Jan90', 365, 'Feb90', 396, 'Mar90', 425, 'Apr90', 456, 'May90', 486, 'Jun90', 516 ); # that'll do for a while ### lotsa code deleted here ### ### we're in a loop named ARTICLE, $article is a filename, and $_ is the ### string: "Date: 24 June 90 11:23:45 GMT" for example unless (/\s(\d\d?)\s+(\w\w\w)\w*\s+(\d\d)\s+(\d\d?):(\d\d):(\d\d)\s+GMT/) { /(.*)/; warn "$article: unknown date format: $1"; next ARTICLE; } ($day,$monthname,$year,$hour,$minute,$second) = ($1,$2,$3,$4,$5,$6); unless (defined($offset{$monthname . $year})) { warn "$article: unknown month/year: $monthname/$year"; next ARTICLE; } $when = ($offset{$monthname . $year}+$day-1)*86400 + $hour*3600 + $minute * 60 + $second + 599616000; At this point, $when has the UNIX "seconds past Jan 1 1970 midnite GMT" integer in it. This routine is reasonably fast... the key is to precompute the month/year offsets (I did it by hand :-) so that Perl has only to perform a simple table lookup. If you get a generic routine working, please forward it to me (or the net). I can also help you by email, if you need further assistance. $\="hacker,";$,="another ";print"Just ","Perl "; -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/