Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Bad Code Coding Challenge #44 by u/kaznovx v0.6
- #include "stdio.h"
- // T is used as offset between next elements in the array below (T between next 2digit sequences, 2*T between months March-August etc)
- #define T 3
- #define put(s) fputs(s, stdout)
- char c[] = "00\00001\00002\00003\00004\00005\00006\00007\00008\00009\00010\00011\00012\00013\00014\00015\00016\00017\00018\00019\000"
- "20\00021\00022\00023\00024\00025\00026\00027\00028\00029\00030\00031\00032\00033\00034\00035\00036\00037\00038\00039\000"
- "40\00041\00042\00043\00044\00045\00046\00047\00048\00049\00050\00051\00052\00053\00054\00055\00056\00057\00058\00059\000"
- "70\00071\00072\00073\00074\00075\00076\00077\00078\00079\000"
- "80\00081\00082\00083\00084\00085\00086\00087\00088\00089\00090\00091\00092\00093\00094\00095\00096\00097\00098\00099\000"
- "am\000pm\000day\000Thurs\000Fri\000\000\000Satur\000Sun\000\000\000Mon\000\000\000Tues\000\000Wednes\000"
- "uary\000Jan\000Febr\000March\000April\000May\000\000\000June\000\000July\000\000August\000"
- "ber\000Septem\000Octo\000\000\000Novem\000\000Decem\000"
- "\004\001\004\003\004\003\004\004\003\004\003\004";
- // day of year
- int D(int t) {
- t /= 3600 * 24;
- t += 365; // move epoch to 1969. NICE
- t %= 1461; // days in 4-year windows
- c[406] = (t > 1095) + 1; // update Feb length
- return (t % 365 + 365 * (t > 1111 && t % 365 == 0)); // 365 in case of last day of leap year
- }
- void d(int t) {
- char* l = c + 405; // extra in month over 27
- t = D(t);
- while (t >= 27 + *l) t -= (*l++ + 27);
- put(c + T * t + T);
- }
- void A(int t) { put(c + 280 + 2*T*((t / 3600 / 24) % 7)), put(c + 276); }
- void B2(int t, int s, int e, int o) { put(c + s + t * o), put(c + e); }
- void B(int t) {
- char* l = c + 405; // extra in month over 27
- t = D(t);
- while (t >= 27 + *l) t -= (*l++ + 27);
- t = l - c - 405;
- // months are padded differently in the array, Jan and Feb need offset 4, Mar-Aug offset 6, Sep-Dec offset 7
- if(t < 2) B2(t, 328, 323, T + 1); // month name beginning + "uary"
- else if(t < 8) B2(t, 325, T - 1, 2 * T); // month name beginning + ""
- else B2(t, 322, 374, 2 * T + 1); // month name beginning + "ber"
- }
- void Y(int t) {
- t /= 3600*24;
- t += 365; // move epoch to 1969. NICE
- // convert t to number of years since 1970
- // (complete 4 years) * 4 + (number of days in this 4 years period) / 365 (- 1 if its last day of leap year) - 1 to get back to 1970-based epoch
- t = t / 1461 * 4 + (t - (t / 1461) * 1461) / 365 - (((t - (t / 1461) * 1461) / 365) == 4) - 1;
- put(c + 57 + T*(t >= 30)); // 19 or 20
- put(c + T * (t >= 30 ? t - 30 : t) + T * (70 - 10) * (t < 30)); // 70-99 or 00-29
- // (note, that there are no numbers 60-69 in date format anywhere, so we offset by 70 - 10, not 70)
- }
- void H(int t) { put(c + T*((t / 3600) % 24)); }
- void h(int t) { put(c + T*(((t / 3600) % 12) ? ((t / 3600) % 12) : 12)); }
- void M(int t) { put(c + T*((t / 60) % 60)); }
- void S(int t) { put(c + T*(t % 60)); }
- void p(int t) { put(c + 270 + T*((t / (3600 * 24 / 2)) & 1)); }
- void str_to_time(int t, const char* tx) {
- void (*f[10])(int) = {d, A, B, Y, H, h, M, S, p};
- --tx;
- while(*(++tx)) {
- char s[] = "dABYHhMSp\001";
- char* s2 = s;
- while (*s2) if ((*s2++) == *tx) { f[s2 - s - 1](t); break; }
- if (!*s2) putchar(*tx);
- }
- putchar('\n');
- }
- int main() {
- str_to_time(1599335154, "h:M:S p"); // 07:45:54 pm
- str_to_time(683475320, "A d B Y"); // Thursday 29 August 1991
- str_to_time(694310400, "A d B Y"); // Thursday 02 January 1992
- str_to_time(951782400, "A d B Y"); // Tuesday 29 February 2000
- str_to_time(951868800, "A d B Y"); // Wednesday 01 March 2000
- str_to_time(978220800, "A d B Y"); // Sunday 31 December 2000
- str_to_time(1598904481 , "A d B Y h:M:S p"); // Monday 31 August 2020 08:08:01 pm
- str_to_time(1961670142 , "A d B Y h:M:S p"); // Sunday 29 February 2032 12:22:22 pm
- str_to_time(0 , "A d B Y"); // Thursday 01 January 1970
- }
Advertisement
Add Comment
Please, Sign In to add comment