#include #include #include static unsigned print_decimal(size_t places, char buf[places], unsigned val) { for (size_t pos = places; pos > 0; pos--) { buf[pos-1] = (val % 10) + '0'; val /= 10; } return val; } char *asctime_r(const struct tm *tm, char *buf) { static char const wday[7][3] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", }; static char const mon[12][3] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", }; memcpy(buf, "\0\0\0 \0\0\0 \0 \0\0:\0\0:\0\0 \0\0\0\0\n", 26); if (tm->tm_wday >= 7u) goto CLEANUP; memcpy(buf, wday[tm->tm_wday], 3); if (tm->tm_mon >= 12u) goto CLEANUP; memcpy(buf+4, mon[tm->tm_mon], 3); if (tm->tm_mday < 10u) { if (print_decimal(1, buf+9, tm->tm_mday)) goto CLEANUP; } else { if (print_decimal(2, buf+8, tm->tm_mday)) goto CLEANUP; } if (print_decimal(2, buf+11, tm->tm_hour)) goto CLEANUP; if (print_decimal(2, buf+14, tm->tm_min)) goto CLEANUP; if (print_decimal(2, buf+17, tm->tm_sec)) goto CLEANUP; if (1900u+tm->tm_year < 1000u || print_decimal(4, buf+20, 1900u+tm->tm_year)) goto CLEANUP; CLEANUP: return buf; } int main(int argc, char* argv[argc+1]) { char buf[26]; struct tm T; time_t t = time(0); gmtime_r(&t, &T); if (argc == 2) T.tm_mon = 13; if (argc == 3) T.tm_mon = -6; asctime_r(&T, buf); puts(buf); //puts(asctime(&T)); }