URI:
       0075-strftime.c - scc - simple c99 compiler
  HTML git clone git://git.simple-cc.org/scc
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       0075-strftime.c (3068B)
       ---
            1 /*
            2 output:
            3 testing
            4 done
            5 end:
            6 */
            7 
            8 #include <stdio.h>
            9 #include <stdlib.h>
           10 #include <time.h>
           11 
           12 static struct tm *tm;
           13 static time_t t;
           14 static char buf[256];
           15 
           16 void
           17 testval(const char *fmt, size_t siz, size_t expect)
           18 {
           19         size_t r;
           20 
           21         r = strftime(buf, siz, fmt, tm);
           22         if (r != expect) {
           23                 printf("test failed, fmt=%s, siz=%zu, result=%zu, expected=%zu\n",
           24                         fmt, siz, r, expect);
           25                 exit(1);
           26         }
           27 }
           28 
           29 int
           30 main(void)
           31 {
           32         puts("testing");
           33         t = time(NULL);
           34         if (t == (time_t)-1)
           35                 return 1;
           36 
           37         tm = gmtime(&t);
           38         if (tm == NULL)
           39                 return 1;
           40 
           41         /* "GMT" */
           42         testval("%Z", sizeof(buf), 3);
           43         testval("%Z", 4, 3); /* fit exactly */
           44         testval("%Z", 3, 0); /* too small */
           45         testval("%Z", 2, 0); /* too small */
           46 
           47         testval("%Y", sizeof(buf), 4);
           48         testval("%Y", 5, 4); /* fit exactly */
           49         testval("%Y", 4, 0); /* too small */
           50         testval("%Y", 2, 0); /* too small */
           51 
           52         testval("%G", sizeof(buf), 4);
           53         testval("%G", 5, 4); /* fit exactly */
           54         testval("%G", 4, 0); /* too small */
           55         testval("%G", 2, 0); /* too small */
           56 
           57         testval("%%", sizeof(buf), 1);
           58         testval("%%", 2, 1); /* fit exactly */
           59         testval("%%", 1, 0); /* too small */
           60         testval("%%", 0, 0); /* too small */
           61 
           62         testval("%d", sizeof(buf), tm->tm_mday > 10 ? 2 : 1);
           63         testval("%d", tm->tm_mday > 10 ? 3 : 2, tm->tm_mday > 10 ? 2 : 1); /* fit exactly */
           64         testval("%d", tm->tm_mday > 10 ? 2 : 1, 0); /* too small */
           65         testval("%d", 0, 0); /* too small */
           66 
           67         testval("abc", sizeof(buf), 3);
           68         testval("abc", 4, 3); /* fit exactly */
           69         testval("abc", 3, 0); /* too small */
           70         testval("abc", 0, 0); /* too small */
           71 
           72         testval("%t", sizeof(buf), 1);
           73         testval("%t", 2, 1); /* fit exactly */
           74         testval("%t", 1, 0); /* too small */
           75         testval("%t", 0, 0); /* too small */
           76 
           77         /* "AM" or "PM" */
           78         testval("%p", sizeof(buf), 2);
           79         testval("%p", 3, 2); /* fit exactly */
           80         testval("%p", 2, 0); /* too small */
           81         testval("%p", 0, 0); /* too small */
           82 
           83         testval("", sizeof(buf), 0);
           84         testval("", 1, 0); /* fit exactly */
           85         testval("", 0, 0); /* too small */
           86 
           87         /* weekday */
           88         testval("%w", sizeof(buf), 1);
           89         testval("%w", 2, 1); /* fit exactly */
           90         testval("%w", 1, 0); /* too small */
           91         testval("%w", 0, 0); /* too small */
           92 
           93         /* same as "%Y-%m-%d", recursive */
           94         testval("%F", sizeof(buf), 10);
           95         testval("%F", 11, 10); /* fit exactly */
           96         testval("%F", 10, 0); /* too small */
           97         testval("%F", 0, 0); /* too small */
           98 
           99         /* week number of the year, monday of first year, padded with '0', 0-53 */
          100         tm->tm_yday = 1;
          101         testval("%W", sizeof(buf), 2);
          102         testval("%W", 3, 2); /* fit exactly */
          103         testval("%W", 2, 0); /* too small */
          104         testval("%W", 0, 0); /* too small */
          105 
          106         /* week number of the year, padded with '0' */
          107         tm->tm_yday = 1; /* 1 */
          108         tm->tm_mon = 0; /* january */
          109         tm->tm_year = 122; /* 2022 */
          110         tm->tm_wday = 6; /* saturday */
          111         testval("%V", sizeof(buf), 2);
          112         testval("%V", 3, 2); /* fit exactly */
          113         testval("%V", 2, 0); /* too small */
          114         testval("%V", 0, 0); /* too small */
          115 
          116         tm->tm_wday = 10;
          117         testval("%w", sizeof(buf), 1);
          118         testval("%w", 2, 1); /* fit exactly */
          119         testval("%w", 1, 0); /* too small */
          120         testval("%w", 0, 0); /* too small */
          121         puts("done");
          122 
          123         return 0;
          124 }