SPUG: localtime() returning hour in wrong range?

Tim Maher tim at consultix-inc.com
Sun Apr 8 14:03:34 PDT 2007


On Sun, Apr 08, 2007 at 01:23:42PM -0700, Jim Ludwig wrote:
> Tim Maher wrote:
> 
> I think it is possible you're overlooking
> something.  Perhaps what you're overlooking was
> *hinted* by Eric Wilhelm, who said:
> >> Add coffee, check back at 24:01.

Indeed, I missed that hint. T'was too sly for me!
 
My mistake was in considering the $hour values as similar to
$month, which needs a 0-based to 1-based conversion to match
April with 4. My lack of military experience might have been
another factor 8-}

So here's my take on a program to convert military to "civilian" time;
comments welcome!

#! /usr/bin/perl -wl
# Tim Maher, tim at TeachMePerl.com

defined $ENV{DEBUG} and system 'date';  # show for reference
(undef, $minutes, $hour)=localtime ;    # leave seconds undefined

# Convert military time to civilian time, with AM/PM added
$am_pm='AM';
$hour >= 12  and  $am_pm='PM';     # hours 12-23 are afternoon
$hour >  12  and  $hour=$hour-12;  # 13-23 ==> 1-11 PM
$hour ==  0  and  $hour=12;        # rename day's first hour

$minutes < 10 and $minutes="0$minutes"; # convert "5" to "05", etc.

print "The time is $hour:$minutes $am_pm.";

__DATA__
00 1 2 3 4 5 6 7 8 9 10 11 / 12 13 14 15 16 17 18 19 20 21 22 23 <MIL
12 1 2 3 4 5 6 7 8 9 10 11 / 12 01 02 03 04 05 06 07 08 09 10 11 <STD


More information about the spug-list mailing list