From owner-freebsd-questions Mon Nov 19 12: 4:21 2001 Delivered-To: freebsd-questions@freebsd.org Received: from ralf.artlogix.com (sense-mcglk-240.oz.net [216.39.168.240]) by hub.freebsd.org (Postfix) with ESMTP id 05B5737B419 for ; Mon, 19 Nov 2001 12:04:18 -0800 (PST) Received: by ralf.artlogix.com (Postfix, from userid 1000) id 179EE1B9C2E; Mon, 19 Nov 2001 12:04:17 -0800 (PST) To: Subject: Re: Epoch Time on Freebsd with perl References: From: Ken McGlothlen Date: 19 Nov 2001 12:04:16 -0800 In-Reply-To: Message-ID: <87n11i34u7.fsf@ralf.artlogix.com> Lines: 55 User-Agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.1 (Cuyahoga Valley) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG Someone whose name has been removed to protect the innocent writes: | > [Chris Aitken writes] | > $date1 = "2001-10-01 01:00:00"; | > $date2 = "2001-10-01 03:00:00"; | > | > What I want is to somehow calculate that there is 2 hours between these 2 | > dates (or 120 minutes, or 7200 seconds etc etc) [in Perl] | > Any help on this would be appreciated. | | Depending on your database you could do this in SQL, I would recommend | RTFMing its docs. That deserves *some* kind of prize for possibly the least efficient solution to a problem ever posted on the list. There are several methods out there. One of them would be this: use Time::Local; sub parsetime { # Get the date from the parameter list. my( $datestring ) = shift; # Break up the date on all non-digit characters, and reverse # the order of the result. You'll now have [second, minute, # hour, day, month, year] stored in @date. my( @date ) = reverse( split( /\D+/, $datestring ) ); # Due to a quick in Time::Local, months range from 0..11, so # subtract one from the month. $date[4]--; # Now we let timelocal() (in Time::Local) do the work and # return the result. return( timelocal( @date ) ); } Christ, this assumes that your dates are going to be in the above format. Once you have this routine defined, you can find out the difference between any two dates by using this (assuming the $date1 and $date2 definitions you have above): $time1 = &parsetime( $date1 ); $time2 = &parsetime( $date2 ); $difference = $time2 - $time1; In fact, there are a bunch of Perl modules out there which are useful for this sort of thing. This is just a relatively efficient one, because you know the format of the dates you're parsing. For really weird ones, you can go as far as using Time::ParseDate, which is slow, but incredibly flexible. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message