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.
| [reply] [d/l] |
Re: Time Issues
by davorg (Chancellor) on Dec 01, 2000 at 19:24 UTC
|
#!/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;
--
<http://www.dave.org.uk>
"Perl makes the fun jobs fun
and the boring jobs bearable" - me
| [reply] [d/l] |
Re: Time Issues
by kilinrax (Deacon) on Dec 01, 2000 at 19:31 UTC
|
#!/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;
| [reply] [d/l] |
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. | [reply] |
Re: Time Issues
by davemabe (Monk) on Dec 01, 2000 at 19:12 UTC
|
| [reply] |
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;
}
Dipul | [reply] [d/l] [select] |
|
|
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
http://www.brainbench.com | [reply] [d/l] [select] |
|
|
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.
| [reply] |
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" | [reply] [d/l] |