SlashDevin / NeoGPS

NMEA and ublox GPS parser for Arduino, configurable to use as few as 10 bytes of RAM
GNU General Public License v3.0
707 stars 195 forks source link

Request: dateTime.timestamp #92

Closed sgtJohnny closed 6 years ago

sgtJohnny commented 6 years ago

I'm using you NeoGPS in my project. I'm logging date and time to a CSV file. Sadly some CSV importers have problems with dates.

It would be very helpful if there was a function to get the current date as timestamp (seconds since 1/1/1970) from the neo gps library, like:

  uint32_t timestamp = fix_data.dateTime.timestamp;

Could you make this? :)

SlashDevin commented 6 years ago

The class NeoGPS::time_t already supports this. The example NMEAtimezone.INO shows how to convert the fix.dateTime to a seconds count, offset it by the time zone shift (in seconds), then convert that seconds count back to a time structure (time_t). The "seconds count" type is called NeoGPS::clock_t. Casting a time_t to a clock_t does the conversion from a structure to a seconds count.

In your case, just do this:

uint32_t timestamp = (NeoGPS::clock_t) fix_data.dateTime;

You may also need to review NeoTime.h to confirm the EPOCH. It defaults to Y2K, so you can either

sgtJohnny commented 6 years ago

Hi, thanks for this tip, but what exactly is the "Y2K" Epoch? I did not find any nice info in the web...

My main problem is Trace : 3,2018-05-21 12:01:29.00,476691008,89832085,16007,1032,,3,1452,115,74437, Time Stamp 580219289

But when converting the time stamp back, i get the same date but 2019 instead of 2018?

SlashDevin commented 6 years ago

Y2K = Year 2000

3,2018-05-21 12:01:29.00,476691008,89832085,16007,1032,,3,1452,115,74437,
**Time Stamp ** 580219289

That is the correct value for an EPOCH of 01-01-2000 00:00:00 (confirmed here).

But when converting the time stamp back, i get the same date but 2019 instead of 2018?

Lets see the lines of code that convert it back. Or is it a spreadsheet function you are talking about?

This should work:

uint32_t timestamp = (NeoGPS::clock_t) fix_data.dateTime;
NeoGPS::time_t sameDateTime = (NeoGPS::clock_t) timestamp.

Did you offset the timestamp in your sketch (or spreadsheet) to the POSIX epoch? You would have to add 946,684,800 to a Y2K timestamp to get a POSIX timestamp (or subtract it go back).

sgtJohnny commented 6 years ago

Oh, so the Y2K EPOCH is basically exactly the same as UNIX, but with another date? Its basically "Seconds since 2000"?

Now i'm getting it, sorry!

It works now! :)