skalibs

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

convert-leapsecs.c (1406B)


      1 /* ISC license. */
      2 
      3 #include <sys/types.h>
      4 #include <time.h>
      5 
      6 #include <skalibs/uint64.h>
      7 #include <skalibs/buffer.h>
      8 #include <skalibs/strerr2.h>
      9 #include <skalibs/stralloc.h>
     10 #include <skalibs/genalloc.h>
     11 #include <skalibs/skamisc.h>
     12 
     13 static genalloc table = GENALLOC_ZERO ; /* uint64_t */
     14 
     15 static void add_leapsecs (uint64_t *t)
     16 {
     17   uint64_t *tab = genalloc_s(uint64_t, &table) ;
     18   size_t n = genalloc_len(uint64_t, &table) ;
     19   size_t i = 0 ;
     20   for (; i < n ; i++) if (*t >= tab[i]) (*t)++ ;
     21 }
     22 
     23 int main (int argc, char const *const *argv)
     24 {
     25   stralloc sa = STRALLOC_ZERO ;
     26   for (;;)
     27   {
     28     struct tm tm ;
     29     uint64_t tt ;
     30     time_t t ;
     31     int r ;
     32     char fmt[UINT64_FMT] ;
     33     sa.len = 0 ;
     34     r = skagetln(buffer_0, &sa, '\n') ;
     35     if (r < 0) strerr_diefu1sys(111, "read from stdin") ;
     36     if (!r) break ;
     37     sa.s[sa.len-1] = 0 ;
     38     if (!strptime(sa.s, "+%Y-%m-%d", &tm)) continue ;
     39     tm.tm_sec = 59 ;
     40     tm.tm_min = 59 ;
     41     tm.tm_hour = 23 ;
     42     t = mktime(&tm) ;
     43     if (t < 0) strerr_diefu1sys(111, "mktime") ;
     44     tt = t + 1 ;
     45     add_leapsecs(&tt) ;
     46     if (!genalloc_append(uint64_t, &table, &tt))
     47       strerr_diefu1sys(111, "genalloc_append") ;
     48     fmt[uint64_fmt(fmt, tt)] = 0 ;
     49     buffer_puts(buffer_1, "  TAI_MAGIC + ") ;
     50     buffer_puts(buffer_1, fmt) ;
     51     buffer_puts(buffer_1, ",\n") ;
     52   }
     53   buffer_unput(buffer_1, 2) ;
     54   buffer_putsflush(buffer_1, "\n") ;
     55   return 0 ;
     56 }