/*!gcc -o strftime-formats strftime-formats.c * strftime-formats.c - enumerate strftime formats and values using bash printf %(%...)T * * %a locale abbreviated weekday name [Sun..Sat] Fri * %A locale weekday name [Sunday..Saturday] Friday * %b locale abbreviated month name [Jan..Dec] Aug * %B locale month name [January..December] August * %c locale date time 2022 Aug 26 Fri 12:45:19 * %C century [-99...] 20 * %d day in month, zero-padded [01..31] 26 * %D US date [%m/%d/%y] 08/26/22 * %e day in month, blank-padded [ 1..31] 26 * %E locale era extensions [ignored] * %f invalid format character ? * %F ISO 8601 date year-month-day [%Y-%m-%d] 2022-08-26 * %g ISO 8601 week based year in century [00..99] 22 * %G ISO 8601 week based year with century [-9999...] 2022 * %h locale abbreviated month name [Jan..Dec] Aug * %H hour, 24-hour clock [00..23] 12 * %i year in century [00..99] * %I hour, 12-hour clock [01..12] 12 * %j day in year [001..366] 238 * %J invalid format character ? * %k hour, 24-hour clock, blank pad [ 0..23] 12 * %K invalid format character ? * %l hour, 12-hour clock, blank pad [ 0..12] 12 * %L invalid format character ? * %m month [01..12] 08 * %M minute [00..59] 45 * %n newline ' * ' * %N nanoseconds [0-999999999] 123456789 * %N Emperor/Era Name * %o Emperor/Era Year * %O locale alternate digit extensions [ignored] * %p locale AM/PM 12-hour clock PM * %P locale lowercase am/pm 12-hour clock pm * %q quarter year [1..4] * %Q invalid format character ? * %r time, 12-hour [%I:%M:%S] 12:45:19 * %R ISO 8601 time, 24-hour [%H:%M] 12:45 * %s seconds since the epoch -...0... 1661539519 * %S second [00..60] 19 * %t tab ' ' * %T ISO 8601 time, 24-hour [%H:%M:%S] 12:45:19 * %u ISO 8601 day of week [1..7, Monday == 1] 5 * %U week in year, starting first Sunday [00..53] 34 * %v VMS/Oracle date [%e-%b-%Y] 26-Aug-2022 * %V ISO 8601 week in week based year [01..53] 34 * %w day of week [0..6, Sunday == 0] 5 * %W week in year, starting first Monday [00..53] 34 * %x locale date representation 2022-08-26 * %X locale time representation [%H:%M:%S] 12:45:19 * %y year in century [00..99] 22 * %Y year with century [-9999...] 2022 * %z timezone offset east of GMT [-2300..+2300] -0600 * %Z timezone abbreviation, blank if undetermined [XXX] MDT * %+ default locale date format [%a %b %e %T %Z %Y] Fri Aug 26 21:34:07 MDT 2022 * % no format character ? */ #include #include #include const char *tfmt[] = { "%a locale abbreviated weekday name [Sun..Sat]", "%A locale weekday name [Sunday..Saturday]", "%b locale abbreviated month name [Jan..Dec]", "%B locale month name [January..December]", "%c locale date time", "%C century [-99...]", "%d day in month, zero-padded [01..31]", "%D US date [%m/%d/%y]", "%e day in month, blank-padded [ 1..31]", "%E locale alternate era [ignored]", "%f invalid format character ?", "%F ISO 8601 date year-month-day [%Y-%m-%d]", "%g ISO 8601 week based year in century [00..99]", "%G ISO 8601 week based year with century [-9999...]", "%h locale abbreviated month name [Jan..Dec]", "%H hour, 24-hour clock [00..23]", "%i year in century [00..99]", "%I hour, 12-hour clock [01..12]", "%j day in year [001..366]", "%J invalid format character ?", "%k hour, 24-hour clock, blank pad [ 0..23]", "%K invalid format character ?", "%l hour, 12-hour clock, blank pad [ 0..12]", "%L invalid format character ?", "%m month [01..12]", "%M minute [00..59]", "%n newline", "%N nanoseconds [0-999999999]", "%N locale extension Emperor/Era Name", "%o locale extension Emperor/Era Year", "%O locale alternate digits [ignored]", "%p locale 12-hour clock AM/PM", "%P locale 12-hour clock lowercase am/pm", "%q quarter year [1..4]", "%Q invalid format character ?", "%r time, 12-hour [%I:%M:%S]", "%R ISO 8601 time, 24-hour [%H:%M]", "%s seconds since the epoch -...0...", "%S second [00..60]", "%t tab", "%T ISO 8601 time, 24-hour [%H:%M:%S]", "%u ISO 8601 day of week [1..7, Monday == 1]", "%U week in year, starting first Sunday [00..53]", "%v BSD/OSX/Ruby VMS/Oracle date [%e-%b-%Y]", "%V ISO 8601 week in week based year [01..53]", "%w day of week [0..6, Sunday == 0]", "%W week in year, starting first Monday [00..53]", "%x locale date representation", "%X locale time representation [%H:%M:%S]", "%y year in century [00..99]", "%Y year with century [-9999...]", "%z timezone offset east of GMT [-2300..+2300]", "%Z timezone abbreviation, blank if undetermined [XXX]", "%+ default locale date format [%a %b %e %T %Z %Y]", "% no format character ?", }; int main( void ) { char out[64] = ""; char fmt[4] = "% "; char * f = &fmt[1]; time_t now = time( &now ); struct tm * local = localtime( &now ); char * locale = setlocale( LC_ALL, ""); size_t len; int rc; for (size_t tf = 0; tf < sizeof( tfmt )/sizeof( *tfmt ); ++tf) { *f = tfmt[tf][1]; *out = '\0'; len = strftime( out, sizeof( out ), fmt, local); if (!(rc = printf( "%s\t'%s'\t%2zi\n", tfmt[tf], out, len))) { perror( "strftime-formats: printf" ); } } locale = setlocale( LC_ALL, locale); } /* main() */