skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

ntp_from_tain.c (600B)


      1 /* ISC license. */
      2 
      3 #include <errno.h>
      4 #include <skalibs/uint64.h>
      5 #include <skalibs/tai.h>
      6 #include <skalibs/djbtime.h>
      7 
      8 int ntp_from_tain (uint64_t *u, tain const *a)
      9 {
     10   uint64_t secs, frac ;
     11   if (!utc_from_tai(&secs, tain_secp(a))) return 0 ;
     12   secs += NTP_OFFSET ;
     13   if (secs < TAI_MAGIC + 2147483648UL) goto ifail ;
     14   secs -= TAI_MAGIC ;
     15   if (secs >= ((uint64_t)3 << 31)) goto ifail ;
     16   secs &= (secs < ((uint64_t)1 << 32)) ? 0xFFFFFFFFUL : 0x7FFFFFFFUL ;
     17   frac = ((uint64_t)a->nano << 32) / 1000000000UL ;
     18   *u = (secs << 32) + frac ;
     19   return 1 ;
     20  ifail:
     21   errno = EINVAL ;
     22   return 0 ;
     23 }