The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

jkann9 (5) [Avatar] Offline
#1
localtime => 24 hour time, conversion to 12 hour time.
On page 212, and again on page 215 I have issues with the line:
$hour >12 and $am_pm=’PM’ and $hour=$hour-12;

I see two issues:
1. the noon hour is considered as PM.
2. the midnight hour should be 12 not 0.

localtime returns hour in the range 0 to 23 inclusive.

I think we really need this:
$hour >=12 and $am_pm=’PM’;
$hour >=13 and $hour=$hour-12;
$hour == 0 and $hour=12;


Also the printing of the minutes is confusing as
5:05 PM gets printed out at 5:5 PM

Thus what we really need for the print statement is:
printf “The time is %d:%02d
”, $hour, $minutes, $am_pm;
tmaher (37) [Avatar] Offline
#2
Re: localtime => 24 hour time, conversion to 12 hour time.
Thanks for your thoughtful posting. I'll respond when I get a
chance to look into these matters.

-Tim
tmaher (37) [Avatar] Offline
#3
Re: localtime => 24 hour time, conversion to 12 hour time.
> On page 212, and again on page 215
> I see two issues:
> 1. the noon hour is considered as PM.
> 2. the midnight hour should be 12 not 0.
>
> I think we really need this:
> $hour >=12 and $am_pm=’PM’;
> $hour >=13 and $hour=$hour-12;
> $hour == 0 and $hour=12;

> lso the printing of the minutes is confusing as
> 5:05 PM gets printed out at 5:5 PM
>
> Thus what we really need for the print statement is:
> printf “The time is %d:%02d
”, $hour,
> $minutes, $am_pm;


I agree with your points, except for the use of printf, which adds
an unnecessary degree of complexity. Here's my rewrite of the mytime2
script:

#! /usr/bin/perl -wl

(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; # convert day's first hour

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

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

I'll update both mytime and mytime2 on the Errata page.

-Tim
tmaher (37) [Avatar] Offline
#4
Re: localtime =&gt; 24 hour time, conversion to 12 hour time.
For the benefit of those who wish to use the improved mytime
and mytime2 scripts, I've provided links to them on the http://MinimalPerl.com page.

-Tim
jkann9 (5) [Avatar] Offline
#5
Re: localtime =&gt; 24 hour time, conversion to 12 hour time.
The same issue occurs on page 386 with the mytime3 script.
tmaher (37) [Avatar] Offline
#6
Re: localtime =&gt; 24 hour time, conversion to 12 hour time.
> The same issue occurs on page 386 with the mytime3
> script.


Thanks for pointing this out; I've provided an improved version of mytime3
at http://MinimalPerl.com/#Software

-Tim
pthompson (1) [Avatar] Offline
#7
Re: localtime =&gt; 24 hour time, conversion to 12 hour time.
Tim,

You seem to have missed out a semicolon in line 16 of your mytime+ script at http://minimalperl.com/mytime%2b.plx

This is what you have at the moment:

$hour >= 12 and $am_pm='PM' # 12-23 are afternoon

I believe this will result in the script failing if run after noon, but it should work correctly if run in the morning.

Regards,
- Paul.