Shop OBEX P1 Docs P2 Docs Learn Events
NTP Time Stamp to ASCII Date — Parallax Forums

NTP Time Stamp to ASCII Date

Ryan RaffertyRyan Rafferty Posts: 13
edited 2013-01-30 15:02 in Propeller 1
I am looking for a way to convert an NTP Time Stamp (Number of seconds since 00:00:00 on January 1, 1900) to a useable ASCII Date. Or even a LONG variable for Month,Day,Year.

The problem that I am running into is that my most recent timestamp is 3,568,129,182 seconds since Jan 1, 1900. Spin will only accept positive integers up to 2,147,483,647 so when I evaluate the LONG (11010100101011010101010010011110), it returns a negative integer of -726838114.

Does anyone know of a better way to handle this?

Thanks in advance.

Ryan Rafferty

Comments

  • RaymanRayman Posts: 14,665
    edited 2013-01-25 12:14
  • Ryan RaffertyRyan Rafferty Posts: 13
    edited 2013-01-25 12:38
    Thanks, I'll take a look at that.
  • ersmithersmith Posts: 6,054
    edited 2013-01-25 17:20
    If you are open to using C it would be pretty trivial to convert an NTP timestamp into a C time_t, which for both PropGCC and Catalina is an offset from Jan. 1, 1970; the C standard library functions can then convert this to ASCII time or a broken down time structure.

    (BTW, just to be pedantic, the NTP timestamp is not a count of seconds since Jan. 1, 1900, just as the C time_t is not a count of seconds since Jan. 1, 1970; both of them ignore leap seconds. Which is convenient for some purposes, but a problem for others.)

    Eric
  • RaymanRayman Posts: 14,665
    edited 2013-01-25 17:55
    I have to agree with Eric... Very accurate time calculation is very complex and there are a lot of standards involved. So, using a standard library, like in C, is an easy way to get the right answer. This is mostly because it's been vetted by a lot of people.
    Mine has only been vetted by me. I probably chose to ignore leap seconds...
  • Ryan RaffertyRyan Rafferty Posts: 13
    edited 2013-01-28 12:13
    Rayman, I didn't see anything in your code to convert from number of seconds. It's probably there and I am just missing it.

    However, I did get it figured out. I figured if I used a starting point of Jan 01, 2000 instead of 1900, my "Seconds Since" would be a much smaller number. So if I could subtract 3,115,760,000 from the current time stamp, what was left over would be the seconds since Jan 01, 2000. Since I cannot work with such large numbers, what I ended up doing was taking my LONG, and shifting it left 2 bits then shifting it back right 2 bits. This effectively subtracted 3,221,225,472. Now adding 65,465,472 back resulted in a number that was in fact 3,115,760,000 less than my original NTP Timestamp. Now I could use the remaining 346,903,710 (a smaller number that SPIN likes just fine) to calculate the time based on seconds since Jan 01, 2000.

    I hope this helps somebody in the future...Sure made my head spin for a while.

    Ryan Rafferty
  • Ryan RaffertyRyan Rafferty Posts: 13
    edited 2013-01-28 12:16
    Eric, I am not familiar with C at all and my accuracy only needs to be with a day or so, so the leap seconds are a non-issue for me. Thanks to you and Rayman for the help.
  • RaymanRayman Posts: 14,665
    edited 2013-01-28 12:39
    Ryan, Looking at my code, it's not easy for me to figure out either :(

    To convert from a 32-bit number of seconds since Jan1, 1970 to a string you:
    1. Call LocalTime with the 32-bit number as the argument
    2. Call StrTime with string format as the argument

    You'd have to do some math if you want it from 1900.
    Probably have to worry about the Y2038 problem coming 70 years earlier for you:
    http://en.wikipedia.org/wiki/Year_2038_problem
  • Ryan RaffertyRyan Rafferty Posts: 13
    edited 2013-01-28 13:09
    The Year 2038 problem is not an issue either for my purposes. The date is never before 2012 so I can assume everything below 3.5 Billion is after 2038 and handle it appropriately in my code.

    One last question....does anyone know how to make a thread as Solved?

    Thanks,
    Ryan Rafferty
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2013-01-28 13:19
    Here is an SNTP demo that should get you most of the way there.... There is a slight bug if the current year happens to be a leap year and it is before Februrary 29th of that year, but other than that it should be /mostly/ correct.

    SNTP Demo:
    http://forums.parallax.com/showthread.php/129435-DEMO-Using-SNTP-to-sync-the-Spinneret-s-on-board-RTC?p=975401&viewfull=1#post975401

    There is also a Perl Script that I wrote to test the formula out before I ported it over to a Spinneret ... The Perl code was run from a Linux (Fedora) based platform.

    Perl Test Script:
    http://forums.parallax.com/showthread.php/129435-DEMO-Using-SNTP-to-sync-the-Spinneret-s-on-board-RTC?p=992018&viewfull=1#post992018
  • RaymanRayman Posts: 14,665
    edited 2013-01-29 02:42
    If you have it figured out, I've heard you can pick "solved" by editing the original post, pushing the "Go Advanced" button and looking for it there...
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2013-01-30 15:02
    Here is an update to the SNTP time server... There was a bug with the decoding algorithm that I believe I have found ...

    http://forums.parallax.com/showthread.php/145697-SNTP-Simple-Network-Time-Protocol-Update?p=1160782#post1160782
Sign In or Register to comment.