Recent

Author Topic: EPOC date/Time problem  (Read 2508 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
EPOC date/Time problem
« on: June 01, 2020, 11:11:19 pm »
Doing an API Call and getting a return of 1591029676 for the date. This is Live Flight Data, aircraft in the air at the time you make the call.
The 1591029676 is supposedly an EPOC date/time.

Using the following function I always get a return of 12/31/9999. Something is amiss. My code or this is not a EPOC Date/time.

Can someone point out the problem.

Code: Pascal  [Select][+][-]
  1.  function TForm1.EPOCTimeTString(AEPOC : LongInt) : String;
  2.   var DT       : TDateTime;
  3.    RawUNIXDate : Int64;
  4.    ASTR        : string;
  5.  begin
  6.   DT :=AEPOC;
  7.   RawUNIXDate := DateTimeToUnix(DT);
  8.   DT := UnixToDateTime(RawUNIXDate);
  9.   ASTR := DateTimeToStr(DT);
  10.   Result := ASTR;
  11.  end;                                                .
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

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11352
  • FPC developer.
Re: EPOC date/Time problem
« Reply #1 on: June 01, 2020, 11:38:07 pm »
A TDateTime is a floating point value based on a Julian, not a EPOCH (aka Unix time) value.

Julian is days after a certain date, with time a fraction. Most used in computing are around 1 jan 1900, but because Excel programmers forgot to account for leap-years it is a few days off.

You can convert epoch time to Julian using dateutils.UnixToDateTime

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: EPOC date/Time problem
« Reply #2 on: June 01, 2020, 11:38:40 pm »
Hi!

Basics about Unix epoch time:

* It is counted in seconds - not days
* The offset date is  1.1.1970

So it is just one line of code:

Code: Pascal  [Select][+][-]
  1. Result := DateTimeToStr((1591029676.0/24/60/60) +EncodeDate(1970,1,1));  

Winni

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: EPOC date/Time problem
« Reply #3 on: June 02, 2020, 12:18:54 am »
So I take 1591037279 / 24 / 60 /60 = 18,414.78332175926

What is that The current day of 06-01-2020 1841 and 4.7932175926 micro secords.

So as a date and time it would be 2020-06-01 06:41:04.

Is that right?

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: 3197
Re: EPOC date/Time problem
« Reply #4 on: June 02, 2020, 12:32:31 am »
Hi!

1591037279 / 24 / 60 /60 is the number   of days since 1.1.1970

TDateTime counts days in the integer part. The time is in the fractional part .

So you give the days since 1.1.1970  plus the offset date into DateTimeToStr.

Btw: 24*60*60 is somewhere (in sysutils? ) as the const SecsPerDay

Yes, your result is right.

Winni


« Last Edit: June 02, 2020, 12:35:00 am by winni »

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: EPOC date/Time problem
« Reply #5 on: June 02, 2020, 01:50:04 am »
@Winni

Coded it and for 1591053392 it returned '6/1/2020 23:16:32'. The API call was made at 16:32 Scottsdale, AZ time.

The aircraft is a B748 and was at Lat 10.99 and Lon 39.93 at  6/1/2020 23:16:32. It  departed GRU (Sao Paulo, Brazil) en-route to FRA (Hessen, Germany)

Trying to answer the question when did the aircraft depart GRU and when will it land at FRA.

Any suggestions on how to do this.
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: 3197
Re: EPOC date/Time problem
« Reply #6 on: June 02, 2020, 02:21:26 am »
@JLWest

Flight Number?

And don't take a freight liner - the Airports dont put them on the official schedule.
And I don't  know how to get the schedules of UPS/DPD/CargoLux .....

(And don't take the Herkules C130 which is flying all day and night between Isanbul and Misrata/Lybia.  Whatever they transport).

Winni

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: EPOC date/Time problem
« Reply #7 on: June 02, 2020, 03:00:01 am »
Use UnixToDateTime() to convert the number to a TDateTime and then use LocalTimeToUniversal() and/or UniversalTimeToLocal() to convert to/from UTC.

Both of them accept a TZOffset parameter which allows you to convert UTC to any local date/time, so you can get the local time at both the origin and destination airports with UniversalToLocalTime, whether the original data is UTC or converted with LocalTimeToUniversal.
« Last Edit: June 02, 2020, 03:02:49 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: EPOC date/Time problem
« Reply #8 on: June 02, 2020, 04:09:23 am »
@JLWest

Flight Number?

And don't take a freight liner - the Airports dont put them on the official schedule.
And I don't  know how to get the schedules of UPS/DPD/CargoLux .....

(And don't take the Herkules C130 which is flying all day and night between Isanbul and Misrata/Lybia.  Whatever they transport).

Winni

In the API Call i get the Cargo, UPS,FEX, I don't get military. There is a site where you can get the military flights.

I can maybe find it if your interested.
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

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: EPOC date/Time problem
« Reply #9 on: June 02, 2020, 04:54:53 am »
@lucimar I think I sort of understand that.

I get a time and a lat/lon position with the time.
Therefore I can get a position where the aircraft was at that time.

I don't know how to get a timezone for that position.

I have a file with the Lat/Lon of airports around the world. With that and the aircraft's position and the haversine formula I could in theory determine the takeoff and arrival time (except for the timezone of the initial fix) of the flight.   

So I need to The times supplied comes with a lat/lonat is
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

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: EPOC date/Time problem
« Reply #10 on: June 02, 2020, 08:48:12 am »
I get a time and a lat/lon position with the time.
Therefore I can get a position where the aircraft was at that time.

What is important to know is whether the time is expressed as a local time or a universal time. It is logical to think it's the later but if it's the former then ...

Quote
I don't know how to get a timezone for that position.

[...]

So I need to The times supplied comes with a lat/lonat is

I don't know exactly how to get that but googling for "timezone from latitude and longitude" gives plenty of hits from which you can, at least, get some pointers as to how it's done. Good luck!
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: EPOC date/Time problem
« Reply #11 on: June 02, 2020, 08:54:51 am »
"local time or a universal time"

Not sure, by Universal time do you GMT.
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

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: EPOC date/Time problem
« Reply #12 on: June 02, 2020, 09:05:11 am »
"local time or a universal time"

Not sure, by Universal time do you GMT.

Yes, UTC is equivalent to what used to be called GMT.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: EPOC date/Time problem
« Reply #13 on: June 02, 2020, 09:09:46 am »
Turns out you can calculate time zones if you have the Latitude which I do.

"To find the time zone in hours of a particular location, you can take the longitude -- in degrees -- and divide it by 15. So, for example, 75° E would be 75/15 which equals 5. That translates to the time zone being 5 hours ahead of UTC or GMT time, which can also be labeled as UTC+5."

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

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: EPOC date/Time problem
« Reply #14 on: June 02, 2020, 09:25:33 am »
That's the base calculation but note that the "official" timezone might differ. For example, we in Spain should use GMT (most of the country lies in fact west of Greenwich) but instead, due to political considerations, use CET. Then you have the DST variations throughtout the year, etc.

That's why there are tables where you can consult which TZ offset a (portion of a) country uses at some specific date. But I don't remember from where you get them, sorry :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018