Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Need Advice: Date difference revisted

by emilford (Friar)
on Mar 10, 2004 at 02:44 UTC ( [id://335320]=perlquestion: print w/replies, xml ) Need Help??

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

Monks,

I know there are countless threads that discuss this. I know there are countless modules that do this. However, Monks, I still need your Perl wisdom.

Of the solutions I've found, all use various modules to accomplish finding the difference in days between two dates. Date::Cal and Time::Local, just to name a few. This brings me to problem one:

1) I am not able to install any module on the server in which this Perl script needs to run.

So, I searched futher on how to incorporate the module into my code. This is Perl, after all. There had to be a way. Of course, a thread with several solutions to this issue. This, however, brought me to my second problem:

2) The module has to be Perl 4.x compatible. Date::Calc, Time::Local, and others all require Perl 5.x or greater. In this case, Perl 4.x is all I have access to.

And now here I am. I'm not sure how to approach this and I'd like to avoid having to write my own solution as I'm sure it won't be nearly "complete" enough. Any suggestions on how to approach this?

Replies are listed 'Best First'.
Re: Need Advice: Date difference revisted
by kvale (Monsignor) on Mar 10, 2004 at 05:54 UTC
    Looking at Programming Perl, first edition (I can't believe I still have it!), there is a timelocal.pl in the perl4 standard library. So the code would go something like
    require "timelocal.pl"; @old_time = ($sec, ..., $isdst); $old_seconds = &timelocal( @old_time); $new_seconds = time(); if ( $new_seconds - $old_seconds > 3600 * 24 * 15) { print "Interval is greater than 15 days\n"; }

    -Mark

Re: Need Advice: Date difference revisted
by graff (Chancellor) on Mar 10, 2004 at 03:17 UTC
    How much trouble would it be to move to a different server (which would be more up-to-date and/or maintained better)? (:P) -- I mean, really, Perl 4.x? WHY??

    Anyway, could you giva a little more detail? What is the original form of the two dates being compared? (Seconds since the epoch? "Mnth Dy Year HH:MI:SS"? Something else?) I don't recall now whether Perl 4 has both the "localtime" and the "timelocal" functions -- if so, you might be able to work with those.

    update: OK, having seen your reply, one more question... (warning: heading for Klugeville here): I gather you don't need accuracy to the hour, minute or second -- you just need to count the days between two given dates. So, what sort of time-depth do you have to deal with? Any dates before 1900? Before 1970?

    If there is a definite threshold date, such that no computation will involve a date before that, you could establish that date as the start of your own "epoch". Then, you could use a modern version of perl on some other system (or use any tool of your choice) to create a look-up table of months and years: for each month/year, the table gives the number of days from the beginning of your epoch to the beginning of the month. Format this table in such a way that your Perl 4.x script can "require date_lookup.tbl", or simply read the table data into an array a hash.

    Given such a table, you can easily take any two date strings convert them to "number of days since start of the epoch" (use month/year to get the table index and add the day-of-month to that), and then just subtract to get the number of days between them.

      It'd be a huge amount of trouble, trust me. Have you ever had to deal with getting something approved by the government? The original format is a simple forward slash (or dash) delimited string, with the month, day, and year (i.e. - 12/31/1990). Perl 4 does have localtime, so I am able to get the current date information. Timelocal, however, is not available.
Re: Need Advice: Date difference revisted
by Anonymous Monk on Mar 10, 2004 at 07:30 UTC
    Ok... Here's an ugly hack that works in Perl 4. I don't know what if it's sufficient for your needs but I got curious and hacked away. It will fail in the year 2100, but you can work in the additional logic to handle the leap year exceptions.... God hopefully, this kludge won't be around in 96 years. Anyway, the main part is the function I called "diffTime" that returns the number of seconds since the supplied date and time. It returns a negative value for past dates and positive values for future dates. It only works with dates after 1970 and times must be in 24 hour format. Curious to know if this is at all useful to you.

    Synopsis: &diffTime(3/10/2004 02:28:00)

    -Aaron Greenberg
    ahg at pobox dot com

    #!/usr/local/bin/perl4 # Global config option for hours off of GMT. i.e. EST is -5. # NOTE: You may want to use something like 4.5 to average when Dayligh +t # savings time is in affect. $GMToffset = -5; # We Run from the commandline as sample program $diff = &diffTime($ARGV[0], $ARGV[1]); print "Diff Seconds: $diff\n"; $diffDays = $diff/(3600*24); print "Diff Days: $diffDays\n"; if ($diffDays < -16) { ### NOTE that it's negative for past days. print "\n*** Windows up for more than 16 days!\n"; } exit(0); # FUNCTION: diffTime (date time) # Usage &difftime( DD/MM/YYYY [HH:MM:SS] ) # A hyphen may be used as the date separator. only colons valid for ti +me # Returns seconds from current time. Negative for past events Posotiv +e for # future events. sub diffTime { local($rebootDate, $rebootTime, $rDmon, $rDday, $rDyear, $rDhour, $ +rDmin, $rDsec); local(@monthdays); local($sec, $leapCount, $i, $time); $rebootDate = $_[0]; # First Argument $rebootTime = $_[1]; # Second Argument unless (($rDmon, $rDday, $rDyear) = $rebootDate =~ /(\d{1,2})[\/-]{ +1}(\d{1,2})[\/-]{1}(\d{4})/) { print STDERR "Invalid Date\n\n"; exit(1); } @monthdays=(31,28,31,30,31,30,31,31,30,31,30,31); ### NOTE: This will break in the year 2100 if ($rDyear%4 == 0) { $monthdays[2]=29; } $sec = ($rDyear - 1970 ) * 365 * 24 * 60 * 60; $leapCount = int(($rDyear - 1972) / 4); $sec = $sec + ($leapCount * 24 * 60 * 60); for ( $i = $rDmon-2; $i >= 0; $i--) { $sec = $sec + ( $monthdays[$i] * 24 * 60 * 60); } $sec = $sec + ($rDday * 24 * 60 * 60); if ($rebootTime) { unless (($rDhour, $rDmin, $rDsec) = $rebootTime =~ /(\d{1,2})[:] +{1}(\d{1,2})[:]{1}(\d{1,2})/) { print STDERR "Invalid Time\n\n"; exit(2); } $sec = $sec + ($rDhour * 3600) + ($rDmin * 60) + $rDsec; } unless ($GMToffset) { $GMToffset = 0; } $time = time() + $GMToffset * 60 * 60; return $sec - $time; }

Re: Need Advice: Date difference revisted
by emilford (Friar) on Mar 10, 2004 at 03:44 UTC
    Okay, let me hopefully address this for everyone, since this seems to be the biggest concern. Our systems are VERY locked down because of the nature of my work. I do not have the ability to use Perl 5. There are VERY few select cases where Perl 5 is available and this, unfortunately, is not one. Perl 4 is my only option.

    The government is always out of date. This example is no different.

    Maybe I should approach this differently. What I need to do, is be able to write a script that checks to see if a Windows box has been rebooted w/in the last 15 days. I know this is stepping away from Perl specifically, but perhaps I've missed something w/ the oh-so-wonderful Windows OS.
      Where are you getting the date from? If it is a file modification date, you could just check like this:
      if (-M $filename > 15) { print("Reboot more than 15 days ago."); }
      (No, I haven't tested this, especially on perl4 on Windows.)
        The only way something like this would work is if there is a Windows file that is written to whenever the system is rebooted. Again, the systems are locked down, so I have few options for finding the system uptime on Windows. The only thing I've found so far that gives any indications is the 'net statistics' command. I know this is a pain in the a$$. When I can use Perl 5 I do. If I had the option, I'd be able to write this in no time. With Perl 4, however, I'm a bit stuck. I appreciate the responses guys. How difficult do you think it'd be to write my own date difference subroutines? I'd imagine it's quite involved since there's an entire module dedicated to this alone.
Re: Need Advice: Date difference revisted
by crabbdean (Pilgrim) on Mar 10, 2004 at 04:48 UTC
Re: Need Advice: Date difference revisted
by Fletch (Bishop) on Mar 10, 2004 at 03:21 UTC

    Abandon all hope now. Perl 4.0.36 is at least a decade old and didn't support modules. You'd be better served by getting a more reasonable sysadmin.

Re: Need Advice: Date difference revisted
by TomDLux (Vicar) on Mar 10, 2004 at 03:24 UTC

    Come on ... if they want scripts, they should seriously consider migrating to 21-st century software. If old scripts need Perl 4, Perl 5 could be installed as perl5.

    After all, we no longer have a man walk a hundred feet in front of a car, waving a red lantern to warn people of the approaching hazard. ..... Do we?

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

      After all, we no longer have a man walk a hundred feet in front of a car, waving a red lantern to warn people of the approaching hazard. ..... Do we?

      If it's the government agency with whom I used to contract, that is precisely what they'd do.

      --
      tbone1, YAPS (Yet Another Perl Schlub)
      And remember, if he succeeds, so what.
      - Chick McGee

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-23 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found