skalibs

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

localtm_scan.c (1210B)


      1 /* ISC license. */
      2 
      3 #include <errno.h>
      4 #include <time.h>
      5 #include <skalibs/types.h>
      6 #include <skalibs/djbtime.h>
      7 
      8 size_t localtm_scan (char const *s, struct tm *l)
      9 {
     10   struct tm ll = { .tm_isdst = -1 } ;
     11   size_t n = 0 ;
     12   unsigned int u ;
     13   size_t i = uint_scan(s+n, &u) ;
     14   if (!i) goto fail ;
     15   n += i ;
     16   if (u < 1900) goto fail ;
     17   u -= 1900 ; ll.tm_year = u ;
     18   if (s[n++] != '-') goto fail ;
     19   i = uint_scan(s+n, &u) ;
     20   if (!i) goto fail ;
     21   n += i ;
     22   if (!u || (u > 12)) goto fail ;
     23   u-- ; ll.tm_mon = u ;
     24   if (s[n++] != '-') goto fail ;
     25   i = uint_scan(s+n, &u) ;
     26   if (!i) goto fail ;
     27   n += i ;
     28   if (!u || (u > 31)) goto fail ;
     29   ll.tm_mday = u ;
     30   if ((s[n] != ' ') && (s[n] != 'T')) goto fail ;
     31   n++ ;
     32   i = uint_scan(s+n, &u) ;
     33   if (!i) goto fail ;
     34   n += i ;
     35   if (u > 23) goto fail ;
     36   ll.tm_hour = u ;
     37   if (s[n++] != ':') goto fail ;
     38   i = uint_scan(s+n, &u) ;
     39   if (!i) goto fail ;
     40   n += i ;
     41   if (u > 59) goto fail ;
     42   ll.tm_min = u ;
     43   if (s[n++] != ':') goto fail ;
     44   i = uint_scan(s+n, &u) ;
     45   if (!i) goto fail ;
     46   n += i ;
     47   if (u > 60) goto fail ;
     48   ll.tm_sec = u ;
     49   if (mktime(&ll) == (time_t)-1) goto fail ;
     50   *l = ll ;
     51   return n ;
     52  fail:
     53   return (errno = EINVAL, 0) ;
     54 }