Shop OBEX P1 Docs P2 Docs Learn Events
SNTP V2.01 code problem today? — Parallax Forums

SNTP V2.01 code problem today?

Is anyone else having a problem with SNP V2.01 today? Mine has been working just foie for the past 6 months then this morning i tried syncing and got this as the "human time": 00/227/2016 214:242:255 (GMT -5:00).

This is the "human time" SNTP code in V2.01:

OBJ
{{
******************************************************************
* SNTP Simple Network Time Protocol                       v2.01  *
* Author: Beau Schwabe                                           *
*                                                                *
* Recognition: Benjamin Yaroch, A.G.Schmidt                      *
*                                                                *
* Copyright (c) 2011 Parallax                                    *
* See end of file for terms of use.                              *
******************************************************************


Revision History:
v1      04-07-2011              - File created

v1.01   09-08-2011              - Minor code update to correct days in Month rendering
                                - and replace bytefill with bytemove for the 'ref-id' string                               

v2      01-29-2013              - Fixed an illusive bug that caused problems around the first of the year

v2.01   02-02-2013              - Logic order error with previous bug fix

Buffer bytes 0-3 contain the IP address of thre SNTP server
                           1                   2                   3      Buffer bytes
       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9  0  1
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |LI | VN  |Mode |    Stratum    |     Poll      |   Precision    |  4-7
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                          Root  Delay                           |  8-11
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                       Root  Dispersion                         |  12-15
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                     Reference Identifier                       |  16-19
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                                                                |  20-23
      |                    Reference Timestamp (64)                    |
      |                                                                |  24-27
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                                                                |  28-31
      |                    Originate Timestamp (64)                    |
      |                                                                |  32-35
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                                                                |  36-39
      |                     Receive Timestamp (64)                     |
      |                                                                |  40-43
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                                                                |  48-51
      |                     Transmit Timestamp (64)                    |
      |                                                                |  52-55
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                 Key Identifier (optional) (32)                 |  
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                                                                |
      |                                                                |
      |                 Message Digest (optional) (128)                |
      |                                                                |
      |                                                                |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

}}
    
PUB HumanTime(Offset,TimeStampAddress)|i,Seconds,Days,Years,LYrs,DW,DD,HH,MM,SS,Month,Date,Year
' The NTP timestamp is GMT so add the UTC offset (-4 for EDT and -5 fro EST)
  Seconds := long[TimeStampAddress] + Offset * 3600  'Local epoch time in seconds
    Days    := ((Seconds >>= 7)/675) + 1 '<- Days since Jan 1, 1900 ... divide by 86,400 and add 1
' 1-Jan-1900 was a Monday (i.e. Day=1 is Monday, Day=2 is Tuesday, etc
' Days//7 is 0 for Sunday, 1 for Monday, 2 for Tuesday... 6 for Saturday.
'    DW      := (Days-1) // 7
     DW      := Days // 7         '<-- Day of the week ranges from 0 (Sunday) to 6 (Saturday)
'    DW      :=  ((Days+1) // 7 ) '<-- Changed addind one after // makes the day range 1-7
       
    Years := Days / 365         '   Number of Days THIS year and
    Days -= (Years * 365)       '   number of years since 1900.

    LYrs := Years / 4           '<- Leap years since 1900
    Year := Years + 1900        '<- Current Year                   

    Days -= LYrs                '<- Leap year Days correction
                                '   for THIS year
    repeat
      repeat i from 1 to 12     '<- Calculate number of days 
        Month := 30             '   in each month.  Stop if
         if i&1 <> (i&8)>>3     '   Month has been reached
           Month += 1
        if i == 2
           Month := 28 
        if Days =< Month        '<- When done, Days will contain
           quit                 '   the number of days so far this 
        if Days > Month         '   month.  In other words, the Date.
           Days -= Month     

{
        if Days > Month         '<- When done, Days will contain
           Days -= Month        '   the number of days so far this 
        if Days =< Month        '   month.  In other words, the Date.
           quit     
}

    until Days =< Month
    Month := i                  '<- Current Month               
    Date  := Days               '<- Current Date


    SS := long[TimeStampAddress] + Offset * 3600
    SS := SS -(((Years*365)*675)<<7) '<- seconds this year
         
    MM := SS / 60                        '<- minutes this year
    SS := SS - (MM * 60)                 '<- current seconds

    HH := MM / 60                        '<- hours this year
    MM := MM - (HH * 60)                 '<- current minutes

    DD := HH / 24                        '<- days this year
    HH := HH - (DD * 24)                 '<- current hour

    DD -= LYrs                           '<- Leap year Days correction
                                         '   for THIS year

    long[TimeStampAddress][2] := Month<<24+Date<<16+Year
    long[TimeStampAddress][3] := DW<<24+HH<<16+MM<<8+SS                                     

'    DD is redundant but I included it for completion...
'    If you subtract the number of days so far this year from
'    DD and add one, you should get today's date.  This is calculated
'    from another angle above from Days
     

Comments

  • Ah ha! There is a V2.11 of this SNTP code wherein Mike Gebhard fixed the offending code in "HumanTime":

    This is his updated code dated 12-08-2013:
    PUB HumanTime(Offset,TimeStampAddress) | dow, seconds, t1, i,days,lastLeap,Month,day,Year,minutes, hours, daysSinceJan1,numberOfLeapYears,date,years, daysSinceEpoch, secondsSinceEpoch, secondsToday
    
    ' The returned SNTP timestamp is for UTC so add the local time zone offset to get local time (e.g. EST=-5 hours, EDT=-4 hours)
      seconds := long[TimeStampAddress] + Offset * 3600
    ' seconds is elapsed time since Monday, 1-Jan-1900 00:00:00 (12:00 AM)
    ' There are 86400 seconds in a day so dividing seconds by 86400 yields the number of whole days past since 1-Jan-1900 00:00:00.
    ' 86400=128*675 so shifting bits 7 places to the right is the same as dividing by 128.  This also taskes care of the sign problem.   
      days := ( seconds >> 7 ) / 675
    ' 01-Jan-1900 was a Monday (dw=1) so add one to days and divide by 7 and the remainder (0-6) represents the day of the week (0=Sun, 1=Mon... 6=Sat).    
      dow := (days+1) // 7
    'Calculate the number of leap years in days by dividing by (4*365+1) 
      numberOfLeapYears :=  days / DAYS_IN_FOUR_YEARS
    
      year := days
      year -= days/DAYS_IN_FOUR_YEARS   'Subtract the number of normal 365 day years from total
      years := year /= DAYS_IN_A_YEAR
      year += 1900
      
      lastLeap :=  days // DAYS_IN_FOUR_YEARS
    
      if(IsLeapYear(year) AND  lastLeap < (31+29))
        numberOfLeapYears--  
    
    
      daysSinceJan1 :=  lastLeap // DAYS_IN_A_YEAR
      daysSinceEpoch := (years * DAYS_IN_A_YEAR) + numberOfLeapYears + daysSinceJan1
    
      
      'Calc Day and Month
      daysSinceJan1++
      if(IsLeapYear(year) AND daysSinceJan1 < 31+29)
         daysSinceJan1++
         
      repeat i from 0 to  MONTHS_IN_A_YEAR - 1
        t1 := daysInMonth[i]
        if(IsLeapYear(year) AND i == 1)
          t1++
          
        if(t1 => daysSinceJan1)
          day := daysSinceJan1
          month := i+1
          quit
        else
          daysSinceJan1 -= t1
     
    
      'Calc hours, seconds, minutes
      secondsSinceEpoch :=   daysSinceEpoch*SECONDS_IN_ONE_DAY
      secondsToday :=  seconds - secondsSinceEpoch
      minutes :=  secondsToday/60
      seconds :=  secondsToday - minutes*60
      hours := minutes/60
      minutes := minutes // 60
      year -= 2000
    
    
      long[TimeStampAddress][2] := Month<<24+day<<16+year
      long[TimeStampAddress][3] := dow<<24+hours<<16+minutes<<8+seconds                                     
    
    
Sign In or Register to comment.