title: NTP convert seconds since 1st january 1900 to current date
body: <p>I'm programming embedded so I'm not able to use many of the c libraries.</p>
  
  <p>I've used the NTP protocol so I now have the number of seconds since the 1st January 1900. Now I need to convert it in a meaningful timestamp. I've been searching about it and I've found this @R.. implementation.</p>
  
  <p><a href="http://git.musl-libc.org/cgit/musl/tree/src/time/__secs_to_tm.c?h=v0.9.15" rel="nofollow">http://git.musl-libc.org/cgit/musl/tree/src/time/__secs_to_tm.c?h=v0.9.15</a></p>
  
  <p>I've tried to use it but I'm not getting a correct result.</p>
  
  <p>Does someone know what is the first argument of the function? I've tried to pass the numer of seconds since the 1st of January 1900 and the number of seconds since 1st of January 1970 but none of them worked as expected.</p>
  
  <p>The code I'm running is:</p>
  
  <pre><code>#define NTP_PACKET_SIZE         48
  uint8_t packetBuffer[ NTP_PACKET_SIZE];
  typedef struct tempo{
      int tm_sec;         /* seconds,  range 0 to 59          */
      int tm_min;         /* minutes, range 0 to 59           */
      int tm_hour;        /* hours, range 0 to 23             */
      int tm_mday;        /* day of the month, range 1 to 31  */
      int tm_mon;         /* month, range 0 to 11             */
      int tm_year;        /* The number of years since 1900   */
      int tm_wday;        /* day of the week, range 0 to 6    */
      int tm_yday;        /* day in the year, range 0 to 365  */
      int tm_isdst;       /* daylight saving time             */
   } tim;
  
   tim * t;
  
   static returnTypes_t bsdUdpClient(uint16_t AddrSize){
  
       int16_t Status = (int16_t) ZERO;
  
        memset(packetBuffer, 0, NTP_PACKET_SIZE);
  
        // Initialize values needed to form NTP request
            packetBuffer[0] = 0xE3; //0b11100011;   // LI, Version, Mode
            packetBuffer[1] = 0x00;     // Stratum, or type of clock
            packetBuffer[2] = 0x06;     // Polling Interval
            packetBuffer[3] = 0xEC;  // Peer Clock Precision
         // 8 bytes of zero for Root Delay &amp; Root Dispersion
            packetBuffer[12]  = 49;
            packetBuffer[13]  = 0x4E;
            packetBuffer[14]  = 49;
            packetBuffer[15]  = 52;
  
      SockID = sl_Socket(SL_AF_INET, SL_SOCK_DGRAM, (uint32_t) ZERO);
      if (SockID &lt; (int16_t) ZERO)
           return (SOCKET_ERROR);
  
      Status = sl_SendTo(SockID, packetBuffer, NTP_PACKET_SIZE * sizeof(uint8_t), (uint32_t) ZERO, (SlSockAddr_t *) &amp;Addr, AddrSize);
  
      /*Check if 0 transmitted bytes sent or error condition*/
      if (Status &lt;= (int16_t) ZERO) {
          sl_Close(SockID);
          return (SEND_ERROR);
      }
      else
          printf("request sent successfully\n\r");
  
      /* receive the reply from the server*/
      Status = sl_RecvFrom(SockID, packetBuffer, NTP_PACKET_SIZE * sizeof(uint8_t), (uint32_t) ZERO, (SlSockAddr_t *) &amp;Addr, &amp;AddrSize);
      if (Status &lt; (int16_t) ZERO){
          printf("error - no bytes received: %d\n\r", (int16_t)Status);
          return SOCKET_ERROR;
      }
      else
          printf("reply received\n\r");
  
      Status = sl_Close(SockID);
      if (Status &lt; 0)
          printf("problem on sl_Close\n\r");
  
      uint8_t index3 = packetBuffer[40];
      uint8_t index2 = packetBuffer[41];
      uint8_t index1 = packetBuffer[42];
      uint8_t index0 = packetBuffer[43];
  
      uint16_t highWord = index3 &lt;&lt; 8 | index2;
      uint16_t lowWord = index1 &lt;&lt; 8 | index0;
  
      uint32_t secondsSince1900 = highWord &lt;&lt; 16 | lowWord;
  
      printf("Seconds since 1 Janeiro de 1900: %lu\n\r", secondsSince1900);
      printf("Seconds since 1 Janeira de 1970: %lu\n\r", secondsSince1900 - 2208988800ul );
  
  
      if(secs_to_date( (secondsSince1900 - 2208988800ul) , t)!=0)
          printf("impossible to convert\n\r");
  
      printf("year: %d; month: %d; day:%d; hour:%d; minuts: %d; seconds:%d\n\r",  t-&gt;tm_year, t-&gt;tm_mon, t-&gt;tm_mday, t-&gt;tm_hour, t-&gt;tm_min, t-&gt;tm_sec );
  
       return (STATUS_OK);
  
    }
  </code></pre>
  
  <p>So, for example, I runned the code a few minuts ago, I was expecting the result </p>
  
  <p>year:2016;month:9;day:1;hour:9;minuts:20;seconds:56 (for example) </p>
  
  <p>but I got this </p>
  
  <p>year: 32281; month: 32279; day:32277; hour:32275; minuts: 32221; seconds:537001984.</p>