### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: UTC Time question  (Read 4803 times)

#### JLWest

• Hero Member
• Posts: 1013
##### Re: UTC Time question
« Reply #30 on: August 08, 2020, 12:57:27 am »
Thank You.

Just about have this figured out. I sure the clock will help a lot.
FPC 3.2.0, Lazarus IDE v2.0.4
Windows 10 Pro 32-GB
Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

#### dbannon

• Hero Member
• Posts: 1330
##### Re: UTC Time question
« Reply #31 on: August 08, 2020, 02:25:56 am »
> There are even ones of 15 minutes (politics).

The only countries I know are Nepal and the Chattham Island (belonging to New Zealand).

There is also a 15min (actually 45min) increment going up a little west of the centre of Australia. But only about 6 people live there and 2 of those don't have watches.

(sorry to interrupt J)

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

#### JLWest

• Hero Member
• Posts: 1013
##### Re: UTC Time question
« Reply #32 on: August 08, 2020, 03:12:57 am »
@dbannon - LOL

You know the 15, 30, and 45 min changes things a little.

I wont be able to use NowUTC +- Integer / 24 as the calculation but have to do something like this.
I have on the Airport records -420 (Phoenix)

So I divide - 420 by 60 = -7

Bit1 := ExtractWord(8,Airport,[8,['|']);          // -4200

Offset := (StrToInt(Bit1))  / 24;             <=== This would cause a runtime error
if it were 7.5 hours or -570
So

Bit1 := ExtractWord(8,Airport,[8,['|']);          // = '-4200'
Sign := Copy(Bit1,1,1);                               // positive or negative
Bit2 := Copy(Bit1,2,7);                                // Copy just the number
Bit2 ;= Trim(Bit2);
Offset := (StrToFloat(Bit2))  / 24;                  // Convert to a float and divide by 24
// This will handle the odd min

if Sign = '-' then  TDT := NowUTC - Offset
else if Sign = '+' then TDT := NowUTC + Offset;

FPC 3.2.0, Lazarus IDE v2.0.4
Windows 10 Pro 32-GB
Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

#### TRon

• Hero Member
• Posts: 536
##### Re: UTC Time question
« Reply #33 on: August 08, 2020, 04:40:32 am »
@dbannon
Thank you for making me laugh. btw are you one of those without watch ?

@JLWest
I understand you took your inspiration from wiini's post, but why so complicated ? You seem to adjust your code to such extend that it somewhat becomes awkward to read/understand

If you retrieve your offset in seconds then why not calculate with seconds ?
Code: Pascal  [Select][+][-]
1. program UTC_Time1;
2.
3. {\$MODE OBJFPC}{\$H+}
4.
5. Uses
6.   sysutils, dateutils, lazsysutils;
7.
8.
9. procedure TimmyTime;
10. var
11.   here_now                : TDateTime;
12.   here_UTCOffSet          : int64;  // UTC Offset in seconds
13.
14.   UTC_Now                 : TDateTime;
15.
16.   there_UTCOffsetAsString : string;
17.   there_UTCOffSet         : int64;  // UTC Offset in seconds
18.   there_Now               : TDateTime;
19. begin
20.   // 'simulate' retrieving local UTC offset from a database record, as a string
21.   // expressed in seconds.
22.   there_UTCOffsetAsString := '-4200';
23.
24.   // Convert the local UTC Offset string to an integer value
25.   there_UTCOffSet := there_UTCOffsetAsString.ToInt64;
26.
27.   // get local time for your current location (for comparison only).
28.   here_now := Now;
29.
30.   // Get the current UTC time
31.   UTC_Now := NowUTC;
32.
33.   // get local UTF offset in seconds
34.   here_UTCOffSet := SecondsBetween(UTC_Now, here_now);
35.
36.   // IncSecond will increase UTC_now with the offset but in case the offset is
37.   // negative it will actually subtract.
38.   // We use IncSecond because the actual timeoffset (as String) was provided
39.   // in seconds.
40.   there_NOW := IncSecond(UTC_Now,  there_UTCOffSet);
41.
42.   WriteLn('             ', 'Here':20, '   ', 'There');
43.   WriteLn('  DateTime : ', DateTimeToStr(here_now):20, '   ', DateTimeToStr(there_NOW));
44.   WriteLn('       UTC : ', DateTimeToStr(UTC_NOW):20 , '   ', DateTimeToStr(UTC_NOW));
45.   WriteLn('UTC Offset : ', here_UTCOffSet:20         , '   ', there_UTCOffSet);
46. end;
47.
48. begin
49.   TimmyTime;
50. end.
51.

Should be easy enough to adapt for use in your (Lazarus) project ?

#### JLWest

• Hero Member
• Posts: 1013
##### Re: UTC Time question
« Reply #34 on: August 08, 2020, 05:01:22 am »
"// Convert the local UTC Offset string to an integer value
there_UTCOffSet := there_UTCOffsetAsString.ToInt64;"

Wouldn't this have to be converted to a float to handle 15, 30 and 45 minuets?

But, yea I think yours is cleaner.

Thanks

Ah, no you would't
« Last Edit: August 08, 2020, 05:04:50 am by JLWest »
FPC 3.2.0, Lazarus IDE v2.0.4
Windows 10 Pro 32-GB
Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

#### TRon

• Hero Member
• Posts: 536
##### Re: UTC Time question
« Reply #35 on: August 08, 2020, 05:20:11 am »
"// Convert the local UTC Offset string to an integer value
there_UTCOffSet := there_UTCOffsetAsString.ToInt64;"

Wouldn't this have to be converted to a float to handle 15, 30 and 45 minuets?
15 minutes = 15 minutes * 60 seconds per minute = 900 seconds
30 minutes = 30 minutes * 60 seconds per minute = 1800 seconds
45 minutes = 45 minutes * 60 seconds per minute = 2700 seconds

Then we add (or when negative subtract) those seconds to the datetime value using the function incsecond()

I do hope there is not a UTC offset in the world that is smaller than a second (or even smaller) but, in case it does you could even choose to use milliseconds (as my code did in the other thread when we calculated the duration of a flight but that had another reason, namely epoch time which is expressed as an offset of a certain date and time in milliseconds).

In case there are UTC offsets that are smaller than a millisecond (or even smaller) then I actively refuse to take residence on such an insane world and i'll become an astronaut to volunteer for the first manned journey to mars (and I will put up a tent and roast my marsh-mallows there)

Quote
Thanks
No thanks required for providing some inspiration in case it helps.

Quote
Ah, no you would't
I wouldn't what exactly ?

#### TRon

• Hero Member
• Posts: 536
##### Re: UTC Time question
« Reply #36 on: August 08, 2020, 05:41:24 am »
@JLWest:
In case you missed it, I am a cheat

List of UTC time offsets: https://en.wikipedia.org/wiki/List_of_UTC_time_offsets

#### JLWest

• Hero Member
• Posts: 1013
##### Re: UTC Time question
« Reply #37 on: August 08, 2020, 05:47:58 am »
Quote

"    Ah, no you would't

I wouldn't what exactly ?"

On first read of you code I thought you would have to convert to Float. Then I read it again and realized you don't divide anywhere.  By staying in seconds all the way it handles the odd second's offset 15, 30, and 45.

I think I can convert this to a function and pass an index of the record which is carried in a listbox.  The function would pass back a string of there_UTCOffSet.

Am I correct in thinking that there_UTCoffset is the UTC time of the offset owner. i.e. Berlin.
FPC 3.2.0, Lazarus IDE v2.0.4
Windows 10 Pro 32-GB
Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

#### TRon

• Hero Member
• Posts: 536
##### Re: UTC Time question
« Reply #38 on: August 08, 2020, 05:59:16 am »
On first read of you code I thought you would have to convert to Float. Then I read it again and realized you don't divide anywhere.  By staying in seconds all the way it handles the odd second's offset 15, 30, and 45.
Ah ok. dumb me could have figured that out himself but somehow missed that you had seen "the light" during your post.

Yes, you understood correctly. We not ever use a smaller entity than seconds, and stay/calculate with those seconds, so there is no need for a float or single/double.

Quote
I think I can convert this to a function and pass an index of the record which is carried in a listbox.  The function would pass back a string of there_UTCOffSet.
Sure, that should be possible. In case that gives you troubles then please post some code and  state what you wish to do with what data exactly and for sure we can come up with a function that works for your case.

Quote
Am I correct in thinking that there_UTCoffset is the UTC time of the offset owner. i.e. Berlin.
That line of thought is absolutely correct.
« Last Edit: August 08, 2020, 06:00:56 am by TRon »

#### JLWest

• Hero Member
• Posts: 1013
##### Re: UTC Time question
« Reply #39 on: August 08, 2020, 06:34:12 am »
Actually your code is pretty slick.

Not saying I won't have any trouble but I think I have a good handle on this now, between you and Winni.

You know with the data I have 45,000 airports with Lat/Lon, timezone and timezone offset I could build a pretty big world wide clock. All I need is a clock graphic with  movable hands. A little code and my data. You know who is really slick with graphics is handako. I think I found my next project.

You and Winni better stand by. You have given me almost all of the code.

It would make a good tutorial and code example on  the Date and time functions in  FPC/Lazuras.

Thank you.
FPC 3.2.0, Lazarus IDE v2.0.4
Windows 10 Pro 32-GB
Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

#### TRon

• Hero Member
• Posts: 536
##### Re: UTC Time question
« Reply #40 on: August 08, 2020, 06:55:51 am »
Actually your code is pretty slick.
Thank you the compliment.

I'm sure you would have been able to (eventually) come up with something similar yourself.

Quote
Not saying I won't have any trouble but I think I have a good handle on this now, between you and Winni.
In case not then feel free to ask. I'm sure we're not the only ones who would be able to help you out in case of troubles.

Quote
You know with the data I have 45,000 airports with Lat/Lon, timezone and timezone offset I could build a pretty big world wide clock. All I need is a clock graphic with  movable hands. A little code and my data. You know who is really slick with graphics is handako. I think I found my next project.
That could indeed be a very fun project to do. These days there are plenty of websites that are able to do something similar but I find it usually much nicer to be able to do something like that using my own code.

In case you wish to know how to draw a graphical clock... there is a lazarus related blog-site that has a small tutorial on that. I will only post the link in case you are interested, because it could be that you would to try and figure out how to do that yourself first.

Just let me know.

Quote
It would make a good tutorial and code example on  the Date and time functions in  FPC/Lazuras.
For sure it would.

Have fun !

#### JLWest

• Hero Member
• Posts: 1013
##### Re: UTC Time question
« Reply #41 on: August 08, 2020, 07:35:57 am »
Yea, I'm interested, When I finish this project that's what we will do. We get handako to build the clock, he's a good guy.

I would have to supply the data records. A lot of them are unusable. You know landing strips on a farm or ranch in Texas. Then I'm using Timezone.com for Timezone information. \$5 s month for unlimited usage. We switch to TimezonePascal I think is the name.  Build it and post it on the Form.

Are we going to argue about credits. handako will have the hardest job building the graphics. Then I would have the most time culling 45.000 reccords.

You and Winni just have to write a few lines of code.

So how about "World Clock by handako , JLWest, TRon and winni"  or maybe
"World Clock by handako , JLWest, winni, TRon.

FPC 3.2.0, Lazarus IDE v2.0.4
Windows 10 Pro 32-GB
Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

#### TRon

• Hero Member
• Posts: 536
##### Re: UTC Time question
« Reply #42 on: August 08, 2020, 08:50:33 am »
@JLWest
A world-clock for the community made by the community

There are packages that already include an analogue/digital clock, such as for example Visual PlanIt which you can see here: https://wiki.lazarus.freepascal.org/Turbopower_Visual_PlanIt#TVpClock

And the tutorial about how to make an analogue clock can be found here: https://lazplanet.blogspot.com/2014/06/how-to-make-simple-analog-clock.html

I'm sure there are a lot of visual components packages (for Lazarus) that includes such a clock.

PS: Amazing how those website have the nerve to "offer" you to pay for publicly available information ...

#### Handoko

• Hero Member
• Posts: 4137
• My goal: build my own game engine using Lazarus
##### Re: UTC Time question
« Reply #43 on: August 08, 2020, 09:52:41 am »
I didn't follow this thread but I heard there will be a collaborative project and my skill may be needed, that's awesome. I'm in. I should start looking for some ideas on this weekend.

Have a good weekend!

#### JLWest

• Hero Member
• Posts: 1013
##### Re: UTC Time question
« Reply #44 on: August 08, 2020, 09:54:54 am »
WOW - That's interesting.

I  was thinking of a form with 4 to six clocks and the ability to set each clock to a different city.
FPC 3.2.0, Lazarus IDE v2.0.4
Windows 10 Pro 32-GB
Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB