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!