### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

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

#### JLWest

• Hero Member
• Posts: 989
##### UTC Time question
« on: August 06, 2020, 09:11:27 pm »

Is there a way to get the UTC time of say Berlin.

I have the local time in Phoenix and I know the current time offset Berlin.

I can't see a way to determine the UTC time in Berlin.

Thanks.

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 #1 on: August 06, 2020, 09:29:19 pm »
Hello JLWest,

Is there a way to get the UTC time of say Berlin.
Yes, by looking up the timezone (UTF) offset of Berlin.

Quote
I have the local time in Phoenix and I know the current time offset Berlin.
Depends on what you mean exactly by time offset. is that he UTF time offset ?

Quote
I can't see a way to determine the UTC time in Berlin. Usually provided by means of a database, or more correctly using a lang/long map/database of the world

UTC Time location 1 = Local Time Location 1 + UTC time offset location 1
UTC Time location 2 = Local Time Location 2 + UTC time offset location 2

Absolute UTC time duration = UTC Time location 2 - UTC Time location 1

Assuming location 1 is source and location 2 is destination.

edit: oops, corrected an error for the duration.
« Last Edit: August 06, 2020, 09:38:58 pm by TRon »

#### JLWest

• Hero Member
• Posts: 989
##### Re: UTC Time question
« Reply #2 on: August 06, 2020, 09:48:22 pm »
Hi TRon

Yes,
I have a file with 24,000 airports with their Lat/Lon of each airport. Also on the record of each airport I have their Current timezone and timezone offest via an API call to TimezoneDB.com.

So for Berlin right now it's CEST 7200,1
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 #3 on: August 06, 2020, 09:54:11 pm »
So for Berlin right now it's CEST 7200,1
That is problematic because that would mean you need to use yet another timezone offset, namely cest, which is UTC + 2.

There are about a few hundred different named timezones, some or  not including daylight saving time, which makes things unnecessary complicated.

Are you not able to retrieve the UTC time offset of Berlin from that database ?

edit: oh, stupid me. it is 7200 seconds. Usually the offset is provided in minutes, afaik.
« Last Edit: August 06, 2020, 09:57:23 pm by TRon »

#### winni

• Hero Member
• Posts: 2121
##### Re: UTC Time question
« Reply #4 on: August 06, 2020, 09:57:21 pm »

Is there a way to get the UTC time of say Berlin.

I have the local time in Phoenix and I know the current time offset Berlin.

I can't see a way to determine the UTC time in Berlin.

Thanks.

Hi!

The UTC time of Berlin is the same as the UTC time of Phoenix/Arizona.
Or Timbuktu.

That is the definition of UTC time.

To get the UTC from Lazarus do this:

Code: Pascal  [Select][+][-]
1. uses ......, lazsysutils;
2.
3. .....
4. ShowMessage (DateTimeToStr(NowUTC));
5. ....
6.

Winni

#### JLWest

• Hero Member
• Posts: 989
##### Re: UTC Time question
« Reply #5 on: August 06, 2020, 10:05:15 pm »
UTC Time location 1 = Local Time Location 1 + UTC time offset location 1
UTC Time location 2 = Local Time Location 2 + UTC time offset location 2

Absolute UTC time duration = UTC Time location 2 - UTC Time location 1

This is confusing.

Location 1
TDT := Now;     = TDateTime                    Not UTC
MyInt64 := DateTimeToUnix(TDT, True );    Just a EPOCH time

I don't see anything to convert a TDT to UTC

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

#### winni

• Hero Member
• Posts: 2121
##### Re: UTC Time question
« Reply #6 on: August 06, 2020, 10:13:14 pm »

Berlin is now 2 hours before UTC (daylight saving)

BerlinTime :=  NowUtC + 2.0/24.0 ;

Arizona is 7 hours behind UTC.
No daylight saving execpt the Navajo Nation (6 hours)

PhoenixTime := NowUTC - 7.0/24.0

That's the whole secret.

Winni

#### JLWest

• Hero Member
• Posts: 989
##### Re: UTC Time question
« Reply #7 on: August 06, 2020, 10:13:26 pm »
Hi Winni

Maybe i'm not saying this right.

It's 1308 in Phoenix. What is the local time in Berlin (1008).

All I have is the time code and offset of Berlin. CEST and 7200,1.
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 #8 on: August 06, 2020, 10:18:42 pm »
UTC Time location 1 = Local Time Location 1 + UTC time offset location 1
UTC Time location 2 = Local Time Location 2 + UTC time offset location 2

Absolute UTC time duration = UTC Time location 2 - UTC Time location 1

This is confusing.
It is perhaps confusing but I wrote down the complete calculation because that way you can always calculate every value when you only have two values to work with.

You asked a similar question in the previous discussion we had, where i suggested to always work with UTC time.

Because as winni wrote, the UTC time is the same everywhere in the world.

The (time) values that differ between two random locations in the world is then their UTC offset.

By the looks of it, your timezone database seem to retrieve the UTC offset including the daylight saving time for Berlin (indicated by the 1 ?)

Quote
I don't see anything to convert a TDT to UTC

#### winni

• Hero Member
• Posts: 2121
##### Re: UTC Time question
« Reply #9 on: August 06, 2020, 10:26:34 pm »
Hi!

Assuming your PC runs with Phoenix time:

The complicated way:
Code: Pascal  [Select][+][-]
1. UTC := Now + 7.0/24.0;
2. BerlinTime := UTC -2.0/24.0;

The simple way:
Code: Pascal  [Select][+][-]
1. BerlinTime := NowUTC - 2.0/24.0;

Winn

#### winni

• Hero Member
• Posts: 2121
##### Re: UTC Time question
« Reply #10 on: August 06, 2020, 10:36:12 pm »
Hi!

Build  yourself a  clock for Phoenix and Berlin.
You only need a timer and a label.

Code: Pascal  [Select][+][-]
1. procedure TForm1.Timer1Timer(Sender:TObject);
2. begin
3. label1.caption := 'Phoenix: 'TimeToStr(NowUTC - 7/24)+ '  Berlin: '+TimeToStr(NowUTC +2/24);
4. end;

Winni

#### JLWest

• Hero Member
• Posts: 989
##### Re: UTC Time question
« Reply #11 on: August 06, 2020, 10:36:30 pm »
Berlin                     Phoenix
Offset       7200                      -25,200

7200/60/60 = 2          -25,200/60/60 =  -7

BT := NowUtC + 2.0/24.0    PT :=  NowUtC -7.0/24.0

So with offset I can get the UTC. Now I have to get to Local Berlin time.

Berlin is now 2 hours before UTC (daylight saving)

BerlinTime :=  NowUtC + 2.0/24.0 ;

Arizona is 7 hours behind UTC.
No daylight saving execpt the Navajo Nation (6 hours)

PhoenixTime := NowUTC - 7.0/24.0

That's the whole secret.

Winni
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

#### winni

• Hero Member
• Posts: 2121
##### Re: UTC Time question
« Reply #12 on: August 06, 2020, 10:44:43 pm »
Hi!

Ok - the offset to UTC again in epoch seconds - that was the problem!

And don't  be astonished  about timezones with half an hour:

That's still reality in India, Iran, Australia and others!!

Winni

#### TRon

• Hero Member
• Posts: 536
##### Re: UTC Time question
« Reply #13 on: August 06, 2020, 10:45:58 pm »
It's 1308 in Phoenix. What is the local time in Berlin (1008).

That is 22:08 Berlin time btw, not 10:08.

Quote
All I have is the time code and offset of Berlin. CEST and 7200,1.
Again, see winni's answer in case you wish to keep it simple.

Phoenix UTC offset = -7
Berlin UTC offset = +2

Therefor the time difference is 7 (from -7 to 0) + 2 = 9 hours.

13:08 + 9 = 22:08

My calculations come into play when you need to calculate the duration of a flight, which should again be done in UTC time because that is able to provide you with an absolute (duration) value (which is the actual duration of a flight).

You really should try to avoid using local times as much as possible as they do not mean anything. You could just as well state that it is ten pieces of pie later in Berlin then it is in Phoenix. The time difference only exist because humanity decided once that when the sun is at its highest point in the sky it is noonish and that should be the same everywhere in the world (well actually daylight saving time and some other technicalities and politics prevents that from actually being the case).

As of winni's suggestion to build yourself a clock, you can take a peek here: https://time.is/1308_6_Aug_2020_in_Phoenix/Berlin (you can change the places and other settings on the main page.

#### TRon

• Hero Member
• Posts: 536
##### Re: UTC Time question
« Reply #14 on: August 06, 2020, 10:47:47 pm »
And don't  be astonished  about timezones with half an hour:
There are even ones of 15 minutes (politics).

I stay out of the discussion now as you seem to have a much clearer head atm and your answers are far more clear

I only seem to confuse user JLWest even further