12 hour clock and offset problem

K

Kenetic

Sorry, yes.

$hours += 12 if $ampm eq 'pm';

Should, of course, read:

$hours += 12 if $ampm eq 'pm' xor $hours eq '12';

Hey thanks Brian. It's rather amusing that about 15 minutes ago I
solved it myself, with a lot of the help posted here, but not really
copy and paste--started from the beginning and persevered till the
finish. Then I looked over here again. Even more amusing is that my
code needed to round some minute fractions, and I used a small snippet
Anno posted in another thread (didn't think it was related to this
newsgroup, but it was).

Yours is much more compact then what I wrote, and I learned quite a
bit from it before and again after--never seen xor being used before,
that's neat!

my $offset = -3.5;
my $tempTime = "9:40 am";

sub myMod {
my ( $a, $b ) = @_;
my $div = $a / $b;
return $a - int( $div ) * $b;
}
sub rnd {
my ( $x, $factor) = @_;
$factor*sprintf '%.0f', $x/$factor;
}
sub getutc {
my ( $time, $GMTOffset ) = @_;
my ( $hrs, $mins, $apm ) = $time =~ /(\d+):(\d\d)\s+([ap]m)/;
$tempPM = $apm;

if (($apm eq "pm") && ($hrs == 12)) {
$tempMins = $hrs * 60;
} elsif (($apm eq "am") && ($hrs == 12)) {
$tempMins = 0;
} elsif ($apm eq "pm") {
$hrs+=12;
$tempMins = $hrs * 60;
} elsif ($apm eq "am") {
$tempMins = $hrs * 60;
}

$tempMins += $mins;

if (myMod($offset, int($offset)) == -0.5) {
$tempMins += 30;
}
$tempMins -= (int($offset) * 60);

$tempMins %= 1440;

if ($tempMins >= 720) {
$apm = "pm";
} else {
$apm = "am";
}

$hrF = ($tempMins / 60);
if (int($hrF > 12)) {
$hrs = int($hrF-12);
} else {
$hrs = int($hrF);
}
$minsF = ($hrF - int($hrF));
$mins = rnd(($minsF * 60),1/2);
$mins = sprintf("%02d", $mins);
return sprintf("%2d:%02d %s", $hrs == 0 ? 12 : $hrs, $mins, $apm);
}

my $theTime = getutc($tempTime, $offset );

print "\nCurrent time: $tempTime\n",
"Adjust $offset hours\n\n",
"Adjusted to: $theTime\n";

At this point it would be trivial to ask how could I optimize or
produce more efficient code, since it's basically already been
discussed.

Consider this issue solved. For now ;)

Thanks!
 
P

Peter J. Holzer

The "x mod y" operation can be consistently defined for all real x and
y through

x mod y = x - y*floor(x/y) (y != 0)
y mod 0 = x

Ex falso quodlibet?

But I think you mean:

x mod 0 = 0

hp
 
A

anno4000

Peter J. Holzer said:
Ex falso quodlibet?

It is very customary to fix definitions when the original one
doesn't apply. Mathematicians do it all the time, provided that some
fundamental law(s) can be preserved by the fix.
But I think you mean:

x mod 0 = 0

No, I (or rather, Knuth and other Authors) mean

y mod 0 = x

The law to preserve here is that x - (x mod y) is an integral multiple
of y. That works for y = 0 if we define x mod 0 = x because 0 is an
integral multiple of 0, but not if we set x mod 0 = 0 (x would have to
be a multiple of 0).

Anno
 
P

Peter J. Holzer

It is very customary to fix definitions when the original one
doesn't apply. Mathematicians do it all the time, provided that some
fundamental law(s) can be preserved by the fix.


No, I (or rather, Knuth and other Authors) mean

y mod 0 = x

What is x in this case? A random number? It doesn't appear on the left
side of the equation.

The law to preserve here is that x - (x mod y) is an integral multiple
of y. That works for y = 0 if we define x mod 0 = x

But you didn't. Please compare

x mod 0 = x

and

y mod 0 = x

They are not the same.
because 0 is an integral multiple of 0, but not if we set x mod 0 = 0
(x would have to be a multiple of 0).

x = inf * 0 (for any x).

Knuth's definition has a very ugly property:

For any x, y with x >= 0 and y > 0 it is true that 0 <= x mod y < y.
But for y = 0, this is not true!

My proposed "fix" preserves the property that 0 <= x mod y < y.

hp
 
A

anno4000

Peter J. Holzer said:
What is x in this case? A random number? It doesn't appear on the left
side of the equation.

Oh dear. That's nonsense which I mindlessly repeated. I meant to say

x mod 0 = x

which is also what can be found in Knuth and other sources.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top