Forum Moderators: coopster & phranque

Message Too Old, No Replies

Unfortunate time calculation problem

This Will Drive me Insane

         

adni18

3:18 am on May 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello everyone.

I am making a script that should calculate one's age, and it uses Net::ICal::Time. For some odd reason, Net::ICal::Time doesn't accept any year under 1000. Then if you put the "human" year, localtime decides the user is somewhere around 105! How can this be fixed?

#!/usr/bin/perl

use CGI;

print new CGI->header;

use Net::ICal::Time;

# Not yet implemented
$t = Net::ICal::Time->new(
second => 12,
minute => 5,
hour => 6,
day => 25,
month => 6,
year => 1992
);

my $newtime=$t->as_localtime;

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime((time-$newtime));

print "According to our system, you are $year year(s) old.";

lexipixel

11:15 am on May 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month


I usually post code in "long hand" so it's easier to read... you can optimize it once you know what it says.

#
# add this after call to
# localtime because it returns
# current year minus 1900
#
$year = $year + 1900;
#
# I hate reading "You are 1 year(s) old..."
# so, since you are calculating a result
# that can be '1' or higher replace following
#
# print "According to our system, you are $year year(s) old.";
#
# with these two lines
#
$multip = 's'; if ($year == 1) { $multip = ''};
printf "According to our system, you are $year year%s old.", $multip;
#
#

adni18

2:11 pm on May 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well I tried doing something similar to this, and it works; it says the user is 12 years old; but when you change the year to 1990, it still says they are 12 years old:


#!/usr/bin/perl

use CGI;

print new CGI->header;

use Net::ICal::Time;

# Not yet implemented
$t = Net::ICal::Time->new(
second => 12,
minute => 5,
hour => 6,
day => 25,
month => 6,
year => 1992
);

my $newtime=$t->as_localtime;

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime((time-$newtime));

print "According to our system, you are ".($year-93)." year(s) old.";

and when you change it to the following, the year stays the same!

#!/usr/bin/perl

use CGI;

print new CGI->header;

use Net::ICal::Time;

# Not yet implemented
$t = Net::ICal::Time->new(
second => 12,
minute => 5,
hour => 6,
day => 25,
month => 6,
year => 1990
);

my $newtime=$t->as_localtime;

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime((time-$newtime));

print "According to our system, you are ".($year-93)." year(s) old.";

lexipixel

6:52 am on May 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why are you using Net::ICal::Time?

If you explain a more of what you're trying to accomplish we can probably get it down to a few lines of code.