Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

How Many Days in the Past?

by Anonymous Monk
on Apr 23, 2001 at 20:54 UTC ( [id://74771]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am getting dates in a YYMMDD format. I need to find out if they are more than X days in the past. I want to install the Date::Calc module but it requires an ANSI C compiler. The boss says I can use anything that I can build with straight perl or nmake.exe (Windows systems), but cannot install an ANSI C compiler. Since we don't currently use it, he doesn't want it on production servers.

Are there other modules that would allow this? Here's a snippet of what I tried, but it didn't work:

use Time::Local; use constant MAX_AGE => 20; # Other code here sub older_than_MAX_AGE { my $date = _convert_date( $_[0] ); # convert yymmdd t +o epoch seconds my $max_epoch_seconds = MAX_AGE * 60 * 60 * 24; # MAX_AGE should b +e days. This converts it to seconds. my ( $day, $month, $year ) = (localtime)[3..5]; $year += 1900; my $today = timelocal( 0,0,0, $day, $month, $year ); my $cutoff_date = $today - $max_epoch_seconds; return $date < $cutoff_date ? 1 : 0 ; } sub _convert_date { # This takes a date in YYMMDD format and converts it to epoch seco +nds (assumes 12:00:00 midnight) my $date = shift; my ( $year, $month, $day ) = ( $date =~ /(\d\d)(\d\d)(\d\d)/ ); $year += 2000; timelocal( 0,0,0,$day,$month,$year ); }
When I pass "older_than_MAX_AGE" the date of 010329, I get the following data:

$cutoff_date '986281200'
$date '988527600'
$max_epoch_seconds 1728000
$_[0] '010329'
$today '988009200'

As you can see, it says that the date that I am passing in is *newer* than today: 988527600 > 988009200, even though today is 010423 and the date passed in is 010329. Can anyone see what I'm doing wrong?

Thanks, J. Atzger

Replies are listed 'Best First'.
Re: How Many Days in the Past?
by Tuna (Friar) on Apr 23, 2001 at 21:05 UTC
    Check out Date::Manip

    Some examples from the docs:

    2. Compare two dates
    $date1=&ParseDate($string1); $date2=&ParseDate($string2); $flag=&Date_Cmp($date1,$date2); if ($flag<0) { # date1 is earlier } elsif ($flag==0) { # the two dates are identical } else { # date2 is earlier }
    4. The amount of time between two dates.
    $date1=&ParseDate($string1); $date2=&ParseDate($string2); $delta=&DateCalc($date1,$date2,\$err); => 0:0:WK:DD:HH:MM:SS the weeks, days, hours, minutes, and seconds between the two $delta=&DateCalc($date1,$date2,\$err,1); => YY:MM:WK:DD:HH:MM:SS the years, months, etc. between the two


    There are many more examples/"recipes" contained in the docs.

    HTH

      As a followup, try the code snippet in this node.. it requires Date::Manip to be installed, and allows the specification of parameters, such as minute, hour and day.. ie: you can check to see the epoch time in seconds, 2 hours ago from the present time..

      I don't know if you're running this in Win32 or Unix, but if its Win32,Linux or Solaris and if you have ActivePerl, you don't need a C compiler to install Date::Manip, because the PPM repository has it compiled and ready to install..
      HTH

Re: How Many Days in the Past?
by suaveant (Parson) on Apr 23, 2001 at 21:02 UTC
    a) it looks like you are converting today's date into year month day, then using timelocal... you realize time() returns the epoch seconds?

    b) month from localtime is actually 0 based, i.e. january is month 0. So you need to add one or it will be wrong in your code... that may very well be your problem.

    Update point a... or are you using that to get midnight, of today? in any case you need to add one to the month :)
                    - Ant

Re: How Many Days in the Past?
by arturo (Vicar) on Apr 23, 2001 at 21:15 UTC

    Lemme confirm what suaveant is saying: the month field (5th arg to timelocal) goes from 0 to 11. This is, I note, to maintain parity with what gmtime and localtime return.

    So what you're seeing is that APRIL (not March) 29th is later than April 23rd. You have to subtract one from the $month you pass to timelocal (and, correspondingly, add 1 to the month you get back from localtime when it matters; here it doesn't, but ...)

    Writing at 988046639 ... =)

Re: How Many Days in the Past?
by dws (Chancellor) on Apr 23, 2001 at 21:49 UTC
    You don't need a C compiler on Win32 if you're running the ActiveState Perl distribution. PPM (ActiveState's Perl Package Manager) will install Date::Calc without needing to compile anything.
    C:\> ppm install Date-Calc
    PPM is worth playing around with. ActiveState had repackaged quite a few CPAN packages into PPM format.

Re: How Many Days in the Past?
by lachoy (Parson) on Apr 23, 2001 at 21:42 UTC

    Something else to think about: Date::Calc (which IMO is more appropriate) is available through PPM if you're using ActivePerl on Win32 systems. No C compiler required :-)

    Chris
    M-x auto-bs-mode

Re: How Many Days in the Past?
by mr.nick (Chaplain) on Apr 23, 2001 at 21:52 UTC
    As others have noted, you need to modify $month to reflect that timelocal expects it to be 0 based (0 = january, 11 = december). Also, your older_than_MAX_AGE is overkill. Here is how I would implement it:
    #!/usr/bin/perl -w use strict; use Time::Local; use constant MAX_AGE => 20; sub _convert_date { # This takes a date in YYMMDD format and converts it to epoch # seconds (assumes 12:00:00 midnight) my $date = shift; my ( $year, $month, $day ) = ( $date =~ /(\d\d)(\d\d)(\d\d)/ ); $year += 2000; $month--; timelocal( 0,0,0,$day,$month,$year ); } sub older_than_MAX_AGE { my $date=shift; time-(MAX_AGE*86400) > $date; } print older_than_MAX_AGE(_convert_date("010401")),"\n"; print older_than_MAX_AGE(_convert_date("010411")),"\n";
    Note that this doesn't require any additional modules other than Time::Local which you apparently already have installed and are using.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://74771]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found