Shop OBEX P1 Docs P2 Docs Learn Events
SNTP - Simple Network Time Protocol - BUG fix Update — Parallax Forums

SNTP - Simple Network Time Protocol - BUG fix Update

Beau SchwabeBeau Schwabe Posts: 6,557
edited 2013-02-01 23:05 in Accessories
I think I might have found and fixed the elusive bug in the time calculation that would always rear it's head around this time of year.

The DEMO program has not changed, but what you will want to update is the SNTP Simple Network Time Protocol v2.01.zip file.

I also updated the Perl script that I originally wrote to test the SNTP server before I migrated it to the Spinneret..

Please continue to let me know if there are any issues with this code. Thanks!

Perl Script: TimeTest.pl
#!/usr/bin/perl

 use IO::Socket::INET;
$MySocket=new IO::Socket::INET->new(PeerPort=>123,  #open socket
        Proto=>'udp',
        PeerAddr=>'132.163.4.101');

$data =     chr(227) . chr(0) . chr(0) . chr(148);  #LI/VN/Mode, Stratum, Poll, Precision
$data = $data . chr(0) . chr(0) . chr(0) . chr(0);  #Root Delay
$data = $data . chr(0) . chr(0) . chr(0) . chr(0);  #Root Dispersion
$data = $data . "LOCL";                             #Reference Identifier
$data = $data . chr(0) . chr(0) . chr(0) . chr(0);  #Reference Timestamp
$data = $data . chr(0) . chr(0) . chr(0) . chr(0);
$data = $data . chr(0) . chr(0) . chr(0) . chr(0);  #Originate Timestamp
$data = $data . chr(0) . chr(0) . chr(0) . chr(0);
$data = $data . chr(0) . chr(0) . chr(0) . chr(0);  #Receive Timestamp
$data = $data . chr(0) . chr(0) . chr(0) . chr(0);
$data = $data . chr(0) . chr(0) . chr(0) . chr(0);  #Transmit Timestamp
$data = $data . chr(0) . chr(0) . chr(0) . chr(0);

$MySocket->send($data);			            #Waiting to receive data		
$MySocket->recv($data,56);                          
$MySocket->close();                                 #received data close socket

@Chars = split("", $data);

$n1 = ord($Chars[40]);				    #Upper 32 bits of Transmit Timestamp
$n2 = ord($Chars[41]);
$n3 = ord($Chars[42]);
$n4 = ord($Chars[43]);

$Seconds = $n4 + $n3*256 + $n2*65536 + $n1*16777216; #Seconds since Jan1, 1900

$Zone =  -6;                                         #GMT - 6 time zone adjust
#$Zone =  -5;                                         #use GMT - 5 during daylight savings

$Seconds = $Seconds  + 3600*$Zone;

print "Seconds since Jan1, 1900 : $Seconds \n\r";

$Days = int((($Seconds >> 7)/675)+1);                #Days since Jan1, 1900     {divide seconds by 86,400 and add 1}
print "   Days since Jan1, 1900 : $Days \n\r";
          
$Years = int($Days / 365);                           #Years since 1900
print "  Years since Jan1, 1900 : $Years \n\r";

$Days = $Days % 365;                                 #Days this year

$LeapYears = int($Years / 4);			     #Leap Years since 1900
$CurrentYear = $Years + 1900 ;                       #Current Year

$Days = $Days - $LeapYears;                          #Leap Year Days Correction

print "   Days so far this year : $Days \n\r\n\r";

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


$count = 1;					     #Routine to determine
 do {                                                #  - The Current Month 
$Month = 30;                                         #  - The Current Date       
$c1 = ($count & 1);                                  #
$c2 = (($count & 8) >>3);                            # from the number of Days
if ($c1 != $c2) {                                    # passed in the current year 
    $Month = $Month +1;                              
   }
if ($count == 2) {
    $Month = 28; 
   }

print "In month $count of this year, there are $Month days. \n\r";

if ($Days <= $Month) {
   $CurrentMonth = $count;
   $CurrentDate  = $Days;  
   $count = 12;
   }

if ($Days > $Month) {
   $Days = $Days - $Month;
   }

$count++;
 } until ($count == 13);
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$Seconds = $Seconds - ((($Years * 365)*675) *128);   #Seconds this year
$SS = $Seconds;
$MM = int($SS / 60);                                 
$SS = $SS - ($MM*60);                                #Current Seconds
$HH = int($MM / 60);                                 
$MM = $MM - ($HH*60);                                #Current Minutes
$DD = int($HH / 24);                                 
$HH = $HH - ($DD*24);                                #Current Hour

print "\n\r";
print "Current Date: $CurrentMonth/$CurrentDate/$CurrentYear\n\r";
print "Current Time: $HH:$MM:$SS\n\r";


Example Perl output:
Seconds since Jan1, 1900 : 3568755733 
   Days since Jan1, 1900 : 41306 
  Years since Jan1, 1900 : 113 
   Days so far this year : 33 

In month 1 of this year, there are 31 days. 
In month 2 of this year, there are 28 days. 

Current Date: 2/2/2013
Current Time: 1:2:13

Comments

  • doggiedocdoggiedoc Posts: 2,239
    edited 2013-01-30 17:23
    Awesome Beau! I can put that to use!!
  • Beau SchwabeBeau Schwabe Posts: 6,557
    edited 2013-02-01 23:05
    One slight change in the logic order of these two IF/THEN statements...
    if ($Days <= $Month) {
       $CurrentMonth = $count;
       $CurrentDate  = $Days;  
       $count = 12;
       }
    
    if ($Days > $Month) {
       $Days = $Days - $Month;
       }
    


    Updated file in post #1
Sign In or Register to comment.