Unix Epoch time to RTC

Hello

I have two questions :

1- Is it possible set Unix Epoch time from NTP (Ethernet Module) to RTC DS1307 or DS3231 without delay ?

please send sample code.

I try search for more library but more of them using (y,m,d,h,m,s) variable.

I think it's cased delay and wrong time.

2- how long should wait for stable NTP UDP packet?

I need to receive right data in first try but some time hung or receive wrong data. then I try to get more in loop.

so I need to know who long shout wait for each try?

every time i receive wrong data.

Thanks
Armen

Answers:

  1. Yes, why not? Most SNTP sketches obtain the time in NTP format, then convert to Epoch time. That is the logical approach, since both keep time in units of integer seconds. Some go on to do further conversion to YYMMDD etc., but you can ignore that if you don't want to use it.

  2. It's not a general question, you have to decide what maximum latency to allow.

More generally, what specific hardware/software do you have, and what specific problem are you having with it? If you are getting "wrong data" then it is likely that either your network connection or your sketch is broken. Typically, you should get an NTP response in about 100-200 ms.

Hi
So NTP format = Unix Epoch format is it right?
If it's possible please introduce some example for this convert.
Thanks for reply
Armen

Rmen:
Hi
So NTP format = Unix Epoch format is it right?
If it's possible please introduce some example for this convert.
Thanks for reply
Armen

No they are not the same format. The conversion code is in the example sketch, please read it.

Thanks
but I still need to some example code for my question.

Rmen:
Thanks
but I still need to some example code for my question.

Here is some pseudocode:

t = received NTP time (converted to epoch)
RTC = t

In the sketch I posted a link to, NTP is converted and stored in the variable, 'epoch', which is printed. Just pass that value to the set method of the RTC library you're using.

1- Is it possible set Unix Epoch time from NTP (Ethernet Module) to RTC DS1307 or DS3231 without delay ?

please send sample code.

I try search for more library but more of them using (y,m,d,h,m,s) variable.

I think it's cased delay and wrong time.

There is no memory register within a ds3231 or ds1307 rtc module which stores Unix Epoch Time. The time stamp needs conversion to the BCD format of the rtc memory.

You have to use an RTC library to do the conversion or else convert yourself.

For example using the Michael Margolis DS1307RTC.h the command is

RTC.set(unixTimeStamp);

I try with this one:

rtc.adjust(DateTime(epoch));

but I am not sure it's OK with my DS1307

RTC.set(unixTimeStamp);

not work with my lib, I don't know why and what should I do :frowning:

when I check it with : https://www.epochconverter.com/

it's not correct after 2 or 3 hour :frowning:

its 2 sec faster than web counter

any suggestion?

Throw away the obsolete DS1307 and get a better RTC IC.

it's not correct after 2 or 3 hour its 2 sec faster than web counter

That is typical for the DS1307. The DS3231 keeps time accurate to 1 or 2 minutes per year.

Thanks
I will try with DS3231.
with "DS1307Z +1308A4" chip epoch time is +20 second in 12 hour !