# Re: 12 hour clock and offset problem

*From*: Purl Gurl <purlgurl@xxxxxxxxxxxx>*Date*: Sun, 17 Jun 2007 07:23:39 -0700

Kenetic wrote:

Purl Gurl wrote:

(prior code example snipped)

Let me know if you find any glitches.

It looks like it works particularly with 12 am and 12 pm, and since

you have a handle on the script (there are some things in here I

haven't used extensively--just a novice at perl), I wonder if you can

modify it to include, .5 of an hour. The offset can be -3 or -3.5 for

3 hours and 30 minutes. Also to be considered is how :30 past the hour

would then wrap around to the next hour, which is easy enough

(although when wrapping past 12 am or pm might pose a challenge)

Rather than complete code for your task, I have opted for discussion.

You are, again, running into convention problems. Convention is to

adjust time by hours, only, to determine a local time. This is clearly

based on longitude location. Adding a problem is daylight savings time

which is used in some regions, not used in other regions.

Adjusting local time by minutes violates all conventions but does

not violate precision in time based on precise longitude location.

Use of minutes for time adjustment is truly stating degrees of

longitude in hours and minutes, which becomes rather complex, and

even more complex if adjustment is made including seconds.

My suggestion is you look at longitude and latitude scripts with

giving direct attention to longitude formulas. This is a serious

suggestion which you should follow; you will find lots of coding

which addresses your task of incorporating minutes and will include

methods to incorporate seconds.

Right off, if your minutes will always be " x.5 " notation, this

is, always 30 minutes, I would simply add 30 minutes to your original

minutes, then adjust if this new minutes amount exceeds 60 minutes.

if (index ($add_hours, ".5") > -1)

{ add 30 minutes and perform calculations here }

Following is example code for dealing with fractions. This example

does display working with a fractional hour and displays how precision

is lessened by use of the int() function within perl core. Use of int()

discards any fractional part and leaves only a whole number. Clearly

this reduces accuracy precision of results,

..5 equals 30 minutes

..33 equals 19.8 minutes (int return is 19)

However, if you are dealing with only tenths,

..1 .2 .3 .4 .5 .6 .7 .8 .9

there is no need for the int() function; returns are always whole numbers

without the int() function.

Although there is no need for int() in my example code, I have included

this function to demonstrate to readers how precision is reduced and to

demonstrate how code like this, how dealing with time, can become very

complex and very frustrating. You and other readers can use adjustment

times beyond tenths, 3.53 hours or 3.67 hours and such, to compare code

results with hand calculated results; a fractional minute will be created

without int() functions included.

Find in my example,

$add_hours = int ($adjust);

This is simply a quick and dirty method to extract "3"

from the 3.5 adjustment time for my $add_hours variable.

Find in my example,

$add_minutes = int ($hour_fraction * 60);

This is where precision is reduced for more complex

calculations. The int() function is NOT needed when

you deal only with tenths, 3.2 3.5 3.7 and such. This

int() can be removed,

$add_minutes = $hour_fraction * 60;

However, if you introduce the hundredth decimal place,

you either must use int() and reduce precision or add

more code to maintain higher precision.

Again, look at longitude code examples to learn how to

create very high precision in time adjustment. You are

truly dealing with longitude calculations expressed as

time; hours, minutes and seconds. Give good attention

to the International Date Line to note how time can

be radically altered when you meet or exceed 180 degrees

longitude or, in time, plus 24 hours exactly or more.

I am not serving you up code on a silver platter. This

example code provides you the basic concept of how to

deal with time adjustments which include a fractional

hour. You will learn more through figuring out how to

incorporate this example into whatever final code you

elect to use; learning is the point, not easy answers.

No "walk through" this time. No full and complete code.

You have been provided enough help by participants here,

have been given enough code examples. You will learn a lot

more by writing your own code. Do so.

If you encounter problems after writing your own code,

then return and ask for help.

#!perl

$time = "11:30 pm";

$adjust = 3.5;

$old_hour = substr ($time, 0, index ($time, ":"));

$old_minutes = substr ($time, -5, 2);

$add_hours = int ($adjust);

$hour_fraction = $adjust - substr ($adjust, 0, index ($adjust, "."));

$add_minutes = int ($hour_fraction * 60);

$new_minutes = $old_minutes + $add_minutes;

if ($new_minutes >= 60)

{

$new_minutes = $new_minutes - 60;

$add_hours++;

}

$new_hour = $old_hour + $add_hours;

print "New Hour is $new_hour";

print "\n\n";

print "New Minutes are $new_minutes";

PRINTED RESULTS:

New Hour is 15

New Minutes are 0

---

Thanks for all the help so far, it's been overwhelming.

I must introduce something off-topic, must masturbate my

ego through rant. Research will disclose almost all within

this discussion group, almost all within our Perl Community,

for years, claim me to be an blithering idiot of a troll,

and do so, almost on a daily basis.

This amuses me, greatly.

--

Purl Gurl

--

"Then again what can you expect from a fat-assed, champagne swilling,

half-breed just off the Rez?"

- Joe Kline

.

**Follow-Ups**:**Re: 12 hour clock and offset problem***From:*Randal L. Schwartz

**Re: 12 hour clock and offset problem***From:*Purl Gurl

**Re: 12 hour clock and offset problem***From:*Kenetic

**References**:**12 hour clock and offset problem***From:*Kenetic

**Re: 12 hour clock and offset problem***From:*Purl Gurl

**Re: 12 hour clock and offset problem***From:*Kenetic

- Prev by Date:
**Re: 12 hour clock and offset problem** - Next by Date:
**Re: perl and php** - Previous by thread:
**Re: 12 hour clock and offset problem** - Next by thread:
**Re: 12 hour clock and offset problem** - Index(es):