Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Time Issues

by ImpalaSS (Monk)
on Dec 01, 2000 at 19:06 UTC ( #44362=perlquestion: print w/replies, xml ) Need Help??

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

Hellow Everyone,
I need help with a small problem. I have a perl script which created a table of data for the engineers here. Yesterday, my boss came to me and wanted certian parts ot the table to be links to other scripts within the system. Well, that was no problem, but a new bug presented itself today. Basically, the script in question has parameters as follows, Now, that prints a table, with about 30 colums and 1200 rows, and the last row, there is a link to another script, which looks like this

Now, the problem isnt creating the links, it has to do with the date paramter in the link. See what happens is, in the second file, the data has to be one day after the date that you would want the data to be called for. So if i want the data for the 30th of november, i would need to put the 1st of december in the script. So here was mty solution, to take the first 2 characters of the date, add one, and then put the date backtogether.. that works for everyday.. except the last day of any given month, (ie: today). My question is.. is there a way to logically take the date in the script ( it would be in the format of 30Nov00 and make it 01Dec00 chronogically instead of the way i am doing it now? Here is the way i do it currently. <code> $date2 = $date; if ($date ne "today"){ $newdate = substr($date2, 0, 2); $enddate = substr($date2, 2, 5); $newdate++; $date2 = $newdate.$enddate; }
Well, thank you very much once again in advance


Replies are listed 'Best First'.
Re: Time Issues
by chipmunk (Parson) on Dec 01, 2000 at 19:29 UTC
    Here's a basic solution using only core modules:
    use Time::Local; $date = '30Nov00'; my @months = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/; my $i = 0; my %months = map { $_ => $i++ } @months; my @dmy = $date =~ /(\d+)([A-Z]+)(\d+)/i; $dmy[1] = $months{$dmy[1]}; if ($dmy[2] < 60) { # have to handle two-digit $dmy[2] += 100; # year somehow } my $time = timelocal(0, 0, 12, @dmy); # noon, to avoid DST issues $time += 60 * 60 * 24; # advance one day my @dmy = (localtime($time))[3, 4, 5]; $dmy[1] = $months[$dmy[1]]; $dmy[2] %= 100; my $date2 = sprintf "%02d%s%02d", @dmy; print "$date2\n";

    Of course, you could have Date::Parse and Date::Manip do most of the work for you, if you wanted.

Re: Time Issues
by davorg (Chancellor) on Dec 01, 2000 at 19:24 UTC

    I think I'd do something like this:

    #!/usr/local/bin/perl -w use strict; use Time::Local; my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my $i = 0; my %months = map { $_ => $i++ } @months; my $date = '30Nov00'; # for example my ($d, $m, $y) = unpack('A2A3A2', $date); $m = $months{$m}; # Nasty hack because of the 2 digit year # Adjust cutoff to taste $y += 100 if $y < 50; my $when = timelocal(0, 0, 12, $d, $m, $y); $when += 86_400; # bug fixed here. thanks chipmunk ($d, $m, $y) = (localtime($when))[3 .. 5]; printf '%02d%s%02d', $d, $months[$m], $y % 100;

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: Time Issues
by kilinrax (Deacon) on Dec 01, 2000 at 19:31 UTC
    Use Time::Local and POSIX::strftime:
    #!/usr/bin/perl -w use strict; use POSIX; use Time::Local; my $time = '31Nov00'; my ($day, $mon, $year) = ($time =~ /^(\d+)([A-Za-z]+)(\d+)$/); my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); my $i; my $mon_num; for($i=0; $i<=$#months; $i++){ if ($mon eq $months[$i]) { $mon_num = $i; last; } } my $epochsecs = timelocal(0, 0, 0, $day-1, $mon_num, $year); my $tomorrowsecs = $epochsecs + 86400; my @tomorrow = localtime($tomorrowsecs); my $timestr = POSIX::strftime("%d%b%y", 0, 0, 0, @tomorrow[3..5]); print $timestr; exit;
Re: Time Issues
by Hrunting (Pilgrim) on Dec 01, 2000 at 20:05 UTC
    Just as a more general note:

    When we program using dates and times here in the office, we almost never deal in date strings except for output purposes. Internally, everything is done using "seconds since the epoch"-type timestamps (the value returned by time() is an example) and then common modules are used to convert from these timestamps into strings. Even if you can't install these modules (which, really, you should at least convince your boss to install either Time::ParseDate or Date::Manip), you can do most of the work yourself using localtime(). The only place that becomes hairy is when you're dealing with daylight savings time.

    Seriously consider ditching the whole 30Nov00 way of passing dates around in your script query string and replace it with the timestamp for that date. For one, '00' isn't Y2K compliant, and two, you'll have a common format you can use for addition, comparison, and conversion.

Re: Time Issues
by davemabe (Monk) on Dec 01, 2000 at 19:12 UTC
Re: Time Issues
by ImpalaSS (Monk) on Dec 01, 2000 at 19:17 UTC
    Hey again,
    Two things, i cant install any modules that don't come with perl.. for many reasons. and, i thought i put the code in <code> tags, but, i must have fouled something up.
    Here is the code i use
    $date2 = $date; if ($date ne "today"){ newdate = substr($date2, 0, 2); $enddate = substr($date2, 2, 5); $newdate++; $date2 = $newdate.$enddate; }


      Oh yes, you can do that if you have any write access on a machine. Let's say you want all the modules to go in your personal library directory /home/impalass/lib/perl, then you simply say during the normal install procedure:

      > perl Makefile.PL LIB=/home/impalass/lib/perl

      For using the modules, there exist several ways to get your personal library into the @INC library search path: the most convenient one is probably to set the PERL5LIB environment variable in your login profile. And yes, this works on Win32 systems, too.

      For more on this, read Effective Perl Programming, pages 163 to 175 (Chapter "Using Packages and Modules").

      Christian Lemburg
      Brainbench MVP for Perl

      Well if you're going to re-invent the wheel, at least take a look at the wheels people are recommending to you and see if you can copy bits and pieces for your purposes.
Re: Time Issues
by tame1 (Pilgrim) on Dec 01, 2000 at 21:18 UTC
    I you can't add Date::Calc or a module such as this, why not
    download it, keep it local, and use lib?

    What does this little button do . .<Click>; "USER HAS SIGNED OFF FOR THE DAY"

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2022-07-03 21:51 GMT
Find Nodes?
    Voting Booth?

    No recent polls found