1/*2output:3testing4done5end:6*/78#include <stdio.h>9#include <stdlib.h>10#include <time.h>1112static struct tm *tm;13static time_t t;14static char buf[256];1516void17testval(const char *fmt, size_t siz, size_t expect)18{19 size_t r;2021 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}2829int30main(void)31{32 puts("testing");33 t = time(NULL);34 if (t == (time_t)-1)35 return 1;3637 tm = gmtime(&t);38 if (tm == NULL)39 return 1;4041 /* "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 */4647 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 */5152 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 */5657 testval("%%", sizeof(buf), 1);58 testval("%%", 2, 1); /* fit exactly */59 testval("%%", 1, 0); /* too small */60 testval("%%", 0, 0); /* too small */6162 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 */6667 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 */7172 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 */7677 /* "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 */8283 testval("", sizeof(buf), 0);84 testval("", 1, 0); /* fit exactly */85 testval("", 0, 0); /* too small */8687 /* 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 */9293 /* 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 */9899 /* 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 */105106 /* 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 */115116 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");122123 return 0;124}