However if you are as determined to reinvent the wheel as I am on occasion then I will state my concerns. the hour field returned from localtime() is in the range 0..23 yet you add 1 to it before beginning, now perhaps you are doing this to adjust for a timezone, but if you think it through it doesnt work. After the addition the hour field is in the range 1..24, so when exactly will we see 12:30AM? (Never.) UPDATE: Oops. Im not entirely correct here. A more complicated AMPM logic does work, but id still say to follow the advice at the bottom.
Anyway, perhaps the below will help
Now this will work for the current time zone, adjusting it for a different time zone simply requires adding the appropriate number of seconds to the value returned by time() when calling localtime()my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, isdst) = loca +ltime(time); my $ampm=$hour>11 ? 'PM' : 'AM'; # day or night $hour%=12; # put it in the range 0..11 $hour=12 unless $hour; # convert 0 to 12 print "$hour$ampm"; # print it out
BTW, you might try writing a loop that goes from 0..23 and then print out the results of your solution for each value.my $hour_secs = 60*60; my @timeparts = localtime(time()+$hour_secs);
Yves / DeMerphq
--
When to use Prototypes?
In reply to Re: Morning or Night?
by demerphq
in thread Morning or Night?
by n4mation
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |