How do you calculate the date from a NTP time stamp?

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?

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 :

time_t utcCalc = NTPstamp - 2208988800UL ; 

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

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.

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

Here is a full date:

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.

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

Here is a full date:

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.

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.

did this with epoch=1539525282

time_t utcCalc = epoch ;

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

but i received this:

2892018

today is 14/10/2018

is it a Timelib bug after last update?

is it a Timelib bug after last update?

What were you expecting?

1539525282UL = local unix date and time 14/10/2018 13:54:42

drakes1969:
. . .
but i received this:

2892018

today is 14/10/2018

. . .

289/2018 looks also like a day of year date format for 16th of October 2018.

Do not cross post.

jremington:
Do not cross post hijack/necro threads.