SNTP Timestamp date and time

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How to correctly convert a timestamp from SNTP protocol to an accurate date and time?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

That’s a pretty broad question that’s covered by RFCs.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Its not documented in the RFC that I can see.

 

I have just figured out the Time must be in (UTC) Coordinated Universal Time and is giving accurate results.

 

I assume I'm working off the Gregorian Calendar, sounds to be standard. Do you need to calculate all leap years going back to 1 January 1900.

Assume you cant just use the average 365.2425 days in year? Must be a simple way of getting the year and day?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

    I have in mind this question since a while and I was going to ask the same thing at one point.

 

    Arduino has an example about this. You need to account for leap seconds as well.

 

    As a practical way I would calculate the seconds up to the beginning of 2019 and only deal with what is left. I think the future leap years are known in advance. Not the same for the leap seconds. So, I would have a constant for the total leap seconds and set it manually once they occur. For the leap years, just a switch construction.

 

    I would like also to hear other good ways to implement this.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

savman wrote:

365.2425 days in year

 

Leap years occur every 4 years, except when

the year is divisible by 100 without also being

divisible by 400:

bool is_leap_year = ((year % 4) == 0);

if ((year % 100) == 0) {
    is_leap_year = ((year % 400) == 0);
}

 

--Mike

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman, if it is in the RFC's can you share which one

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use the time.h library. There is one, I believe, built into avr-libc but, if not, just google avr time.h  Its on Savannah.nongnu.org as well as other places. EVERYTHING is there. Has provisions for geographical offset from UTC, as well as daylight saving time. It is derived from the standard time.h library.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Thu. Dec 27, 2018 - 08:15 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Got it sussed, I'm summing all days 365/366 of each year since 1900, until I reach the year in the timestamp, this gives the year. I use the remaining days to calculate the day and month.

Accounting for leap years(28/29 days in Feb).

 

I'm using the code above for testing for leap years.

 

I add a constant to the timestamp to adjust for NZ UTC time before doing anything.

 

As mentioned above I could add a pre calculated constant and start looping from say 2018 to improve performance.

 

Just have to figure out Daylight Savings Time adjustment now...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Save your energy (and time). Its all in time.h

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yep I see that, thanks.

 

I used that for checking

I was almost there, had to finish what I started laugh