Go Down

Topic: How do you calculate the date from a NTP time stamp? (Read 117 times) previous topic - next topic

boylesg

I have tried to calculate the day of the year from the time stamp and then use that to calculate the date.

It works for some date but I keep getting odd dates that are a day before or a day after the the true date due to rounding errors.

So it seems I cannot use this method reliably.

Does anyone know of alternative ways to get a date from a time stamp?

6v6gt

Use the time library.
From NTP you get a date in epoch time (based on 01.01.1900). Convert it first to epoch 1970 time :

Code: [Select]
time_t utcCalc = NTPstamp - 2208988800UL ;

Serial.print( day(utcCalc )) ;
Serial.print( hour(utcCalc )) ;
Serial.print( minute(utcCalc ) );
. . .
. . .

 






boylesg

Yeah I followed the example in the WiFiESP library and can calculate the time just fine.

But I would still like to know how you calculate the date reliably.

I suppose I could just look up the code that does it in the time library and adjust for my modified version of WiFiESP library.

6v6gt

Are you still having a problem ?
Time and Date are extracted in exactly the same manner.

Here is a full date:

Code: [Select]

time_t utcCalc = NTPstamp - 2208988800UL ;

Serial.print( day(utcCalc )) ;
Serial.print( monthutcCalc )) ;
Serial.print( year(utcCalc ) );



If you want an assessment of the code you have written, post it and maybe someone here can suggest an improvement.

boylesg

Are you still having a problem ?
Time and Date are extracted in exactly the same manner.

Here is a full date:

Code: [Select]

time_t utcCalc = NTPstamp - 2208988800UL ;

Serial.print( day(utcCalc )) ;
Serial.print( monthutcCalc )) ;
Serial.print( year(utcCalc ) );


Ohh screw it - I tried extracting the specific code from the Time library that does the breakdown down of the time stamp and calculates the date (as opposed to the time) but it was like pulling a weed.

There was always some extra bit of code that I needed to extract from the library to make it compile in my project.

In the end I just gave up and included the Time library and just used the existing functions - it does not appear to be a big library anyway.

Calculation of day of month, month and year seems to be similar to what I was trying to do.

But I have not gotten to grips with how it handles the fact that a year is 365.25 days with ending up with rounding errors that results in the day of month being sometimes one too less or one too many.



If you want an assessment of the code you have written, post it and maybe someone here can suggest an improvement.

6v6gt

OK. It is often better not to attempt to re-invent the wheel.
The rules for handling leap years are quite well known and I can imagine the person who wrote the 'time' library had at least a vague understanding of what he was doing and did some testing. At least, I have had no problems with it.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy