19 captures
04 Dec 2003 - 09 Aug 2009
Jan FEB Mar
03
2003 2004 2005
success
fail

About this capture

COLLECTED BY

Organization: Alexa Crawls

Starting in 1996, Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to the Wayback Machine after an embargo period.

Collection: alexa_dv

this data is currently not publicly accessible.
TIMESTAMPS

The Wayback Machine - http://web.archive.org/web/20040203054204/http://www.perl.com:80/cookbook/perlckbk2/solution.csp?day=4
 
www.perl.com
oreilly.comO'Reilly Networkperl.comXML.comONLamp.comONJava.comOpenP2P.com
 
 
  Downloads
  Documentation
  CPAN Mirror
  FAQs
  Training
  Resources
  Article Archive
  O'Reilly Books
  Search

  Columns

P5P Digest
P6P Digest
Off the Wall
Perl Success Stories


 O'Reilly Perl books

 

The Perl CD Bookshelf, Version 2.0

Advanced Perl Programming

Beginning Perl for Bioinformatics

CGI Programming with Perl, 2nd Ed

Developing Bioinformatics Computer Skills

Learning Perl on Win32 Systems

Learning Perl, 3rd Edition

Mastering Algorithms with Perl

Mastering Regular Expressions, 2nd Edition

mod_perl Pocket Reference

Perl Cookbook

Perl & LWP

Perl & XML

Perl in a Nutshell, 2nd Edition

Perl for System Administration

Perl for Web Site Management

Perl Pocket Reference, 4th Edition

Perl/Tk Pocket Reference

Programming Perl, 3rd Edition

Programming Web Graphics with Perl & GNU Software

Programming the Perl DBI

Writing Apache Modules with Perl and C

 


Atom Feed
RSS Feed

Traveling to a Tech Show?

Discount Hotels
Hotel Discounts
San Diego Hotels
Chicago Hotels
California Hotels
Canada Hotels
Toronto Hotels


   Print.Print
Email.Email article link
The Perl Cookbook, 2nd Edition (cover)

Perl Recipe of the Day

The following recipe is from Perl Cookbook, 2nd Edition, by Tom Christiansen and Nathan Torkington. All links in this recipe point to the online version of the book on the Safari Bookshelf.

Buy it now, or read it online on the Safari Bookshelf.


You need to find the number of days between two dates or times.

If your dates are in Epoch seconds and fall in the range Fri Dec 13 20:45:52 1901toTue Jan 19 03:14:07 2038 (inclusive), subtract one from the other and convert the seconds to days:

$seconds = $recent - $earlier;

If you have distinct DMYMHS values or are worried about the range limitations of Epoch seconds, use the Date::Calc module from CPAN. It can calculate the difference between dates:

use Date::Calc qw(Delta_Days);
$days = Delta_Days( $year1, $month1, $day1, $year2, $month2, $day2);

It also calculates the difference between a pair of dates and times:

use Date::Calc qw(Delta_DHMS);
($days, $hours, $minutes, $seconds) =
  Delta_DHMS( $year1, $month1, $day1, $hour1, $minute1, $seconds1,  # earlier
              $year2, $month2, $day2, $hour2, $minute2, $seconds2); # later

One problem with Epoch seconds is how to convert the large integers back to forms that people can read. The following example shows one way of converting an Epoch seconds value back to its component numbers of weeks, days, hours, minutes, and seconds:

$bree = 361535725;          # 16 Jun 1981, 4:35:25
$nat  =  96201950;          # 18 Jan 1973, 3:45:50

$difference = $bree - $nat;
print "There were $difference seconds between Nat and Bree\n";
There were 265333775 seconds between Nat and Bree

$seconds    =  $difference % 60;
$difference = ($difference - $seconds) / 60;
$minutes    =  $difference % 60;
$difference = ($difference - $minutes) / 60;
$hours      =  $difference % 24;
$difference = ($difference - $hours)   / 24;
$days       =  $difference % 7;
$weeks      = ($difference - $days)    /  7;

print "($weeks weeks, $days days, $hours:$minutes:$seconds)\n";
(438 weeks, 4 days, 23:49:35)

Date::Calc's functions can ease these calculations. The Delta_Days function returns the number of days between two dates. It takes the two dates as a list: year, month, day. The dates are given chronologically—earliest first.

use Date::Calc qw(Delta_Days);
@bree = (1981, 6, 16);      # 16 Jun 1981
@nat  = (1973, 1, 18);      # 18 Jan 1973
$difference = Delta_Days(@nat, @bree);
print "There were $difference days between Nat and Bree\n";
There were 3071 days between Nat and Bree

The Delta_DHMS function returns a four-element list corresponding to the number of days, hours, minutes, and seconds between the two dates you give it.

use Date::Calc qw(Delta_DHMS);
@bree = (1981, 6, 16, 4, 35, 25);   # 16 Jun 1981, 4:35:25
@nat  = (1973, 1, 18, 3, 45, 50);   # 18 Jan 1973, 3:45:50
@diff = Delta_DHMS(@nat, @bree);
print "Bree came $diff[0] days, $diff[1]:$diff[2]:$diff[3] after Nat\n";
Bree came 3071 days, 0:49:35 after Nat

The documentation for the CPAN module Date::Calc


View the past week's recipes: Today | Yesterday | 3 days ago | 4 days ago | 5 days ago | 6 days ago | A week ago


About perl.comContact UsPrivacy PolicyWrite for us
Compilation Copyright © 1998-2004 O'Reilly Media, Inc. All Rights Reserved.
All trademarks and registered trademarks appearing on perl.com are the property of their respective owners.
For problems or assistance with this site, email