Lazarus

Free Pascal => Beginners => Topic started by: JLWest on June 01, 2020, 11:11:19 pm

Title: EPOC date/Time problem
Post by: JLWest 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;                                                .
Title: Re: EPOC date/Time problem
Post by: marcov 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 (https://www.freepascal.org/docs-html/current/rtl/dateutils/unixtodatetime.html)
Title: Re: EPOC date/Time problem
Post by: winni 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
Title: Re: EPOC date/Time problem
Post by: JLWest 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?

Title: Re: EPOC date/Time problem
Post by: winni 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


Title: Re: EPOC date/Time problem
Post by: JLWest 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.
Title: Re: EPOC date/Time problem
Post by: winni 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
Title: Re: EPOC date/Time problem
Post by: lucamar 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.
Title: Re: EPOC date/Time problem
Post by: JLWest 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.
Title: Re: EPOC date/Time problem
Post by: JLWest 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
Title: Re: EPOC date/Time problem
Post by: lucamar 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!
Title: Re: EPOC date/Time problem
Post by: JLWest on June 02, 2020, 08:54:51 am
"local time or a universal time"

Not sure, by Universal time do you GMT.
Title: Re: EPOC date/Time problem
Post by: lucamar 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.
Title: Re: EPOC date/Time problem
Post by: JLWest 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
Title: Re: EPOC date/Time problem
Post by: lucamar 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 :-[
Title: Re: EPOC date/Time problem
Post by: wp on June 02, 2020, 09:29:46 am
Turns out you can calculate time zones if you have the Latitude which I do.
No, otherwise the time zone borders would be straight lines, but they are not: https://en.wikipedia.org/wiki/Time_zone
Title: Re: EPOC date/Time problem
Post by: winni on June 02, 2020, 10:35:15 am
Hi!

The IANA timezone table info and download:

https://data.iana.org/time-zones/tz-link.html#tzdb (https://data.iana.org/time-zones/tz-link.html#tzdb)

Winni
Title: Re: EPOC date/Time problem
Post by: winni on June 02, 2020, 10:48:54 am
Hi!

The whole timezone discussion might be obsolet.

AFAIN the international flight data is allways UTC.
And then they are converted into the local time for the customers.

So read your manual to what time zone the data refers.

Winni
TinyPortal © 2005-2018