Guest User

Untitled

a guest
Aug 31st, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.15 KB | None | 0 0
  1. // Bad Code Coding Challenge #44 by u/kaznovx v0.6
  2.  
  3. #include "stdio.h"
  4.  
  5. // T is used as offset between next elements in the array below (T between next 2digit sequences, 2*T between months March-August etc)
  6. #define T 3
  7. #define put(s) fputs(s, stdout)
  8. char c[] =  "00\00001\00002\00003\00004\00005\00006\00007\00008\00009\00010\00011\00012\00013\00014\00015\00016\00017\00018\00019\000"
  9.             "20\00021\00022\00023\00024\00025\00026\00027\00028\00029\00030\00031\00032\00033\00034\00035\00036\00037\00038\00039\000"
  10.             "40\00041\00042\00043\00044\00045\00046\00047\00048\00049\00050\00051\00052\00053\00054\00055\00056\00057\00058\00059\000"
  11.             "70\00071\00072\00073\00074\00075\00076\00077\00078\00079\000"
  12.             "80\00081\00082\00083\00084\00085\00086\00087\00088\00089\00090\00091\00092\00093\00094\00095\00096\00097\00098\00099\000"
  13.             "am\000pm\000day\000Thurs\000Fri\000\000\000Satur\000Sun\000\000\000Mon\000\000\000Tues\000\000Wednes\000"
  14.             "uary\000Jan\000Febr\000March\000April\000May\000\000\000June\000\000July\000\000August\000"
  15.             "ber\000Septem\000Octo\000\000\000Novem\000\000Decem\000"
  16.             "\004\001\004\003\004\003\004\004\003\004\003\004";
  17.  
  18. // day of year
  19. int D(int t) {
  20.     t /= 3600 * 24;
  21.     t += 365; // move epoch to 1969. NICE
  22.     t %= 1461; // days in 4-year windows
  23.     c[406] = (t > 1095) + 1; // update Feb length
  24.     return (t % 365 + 365 * (t > 1111 && t % 365 == 0)); // 365 in case of last day of leap year
  25. }
  26.  
  27. void d(int t) {
  28.     char* l = c + 405; // extra in month over 27
  29.     t = D(t);
  30.     while (t >= 27 + *l) t -= (*l++ + 27);
  31.     put(c + T * t + T);
  32. }
  33.  
  34. void A(int t) { put(c + 280 + 2*T*((t / 3600 / 24) % 7)), put(c + 276); }
  35.  
  36. void B2(int t, int s, int e, int o) { put(c + s + t * o), put(c + e); }
  37.  
  38. void B(int t) {
  39.     char* l = c + 405;  // extra in month over 27
  40.     t = D(t);
  41.     while (t >= 27 + *l) t -= (*l++ + 27);
  42.     t = l - c - 405;
  43.  
  44.     // months are padded differently in the array, Jan and Feb need offset 4, Mar-Aug offset 6, Sep-Dec offset 7
  45.     if(t < 2) B2(t, 328, 323, T + 1);           // month name beginning + "uary"
  46.     else if(t < 8) B2(t, 325, T - 1, 2 * T);    // month name beginning + ""
  47.     else B2(t, 322, 374, 2 * T + 1);            // month name beginning + "ber"
  48. }
  49.  
  50. void Y(int t) {
  51.     t /= 3600*24;
  52.     t += 365; // move epoch to 1969. NICE
  53.     // convert t to number of years since 1970
  54.     // (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
  55.     t = t / 1461 * 4 + (t - (t / 1461) * 1461) / 365 - (((t - (t / 1461) * 1461) / 365) == 4) - 1;
  56.     put(c + 57 + T*(t >= 30)); // 19 or 20
  57.     put(c + T * (t >= 30 ? t - 30 : t) + T * (70 - 10) * (t < 30)); // 70-99 or 00-29
  58.     // (note, that there are no numbers 60-69 in date format anywhere, so we offset by 70 - 10, not 70)
  59. }
  60.  
  61. void H(int t) { put(c + T*((t / 3600) % 24)); }
  62.  
  63. void h(int t) { put(c + T*(((t / 3600) % 12) ? ((t / 3600) % 12) : 12)); }
  64.  
  65. void M(int t) { put(c + T*((t / 60) % 60)); }
  66.  
  67. void S(int t) { put(c + T*(t % 60)); }
  68.  
  69. void p(int t) { put(c + 270 + T*((t / (3600 * 24 / 2)) & 1)); }
  70.  
  71. void str_to_time(int t, const char* tx) {
  72.     void (*f[10])(int) = {d, A, B, Y, H, h, M, S, p};
  73.     --tx;
  74.     while(*(++tx)) {
  75.         char s[] = "dABYHhMSp\001";
  76.         char* s2 = s;
  77.         while (*s2) if ((*s2++) == *tx) { f[s2 - s - 1](t); break; }
  78.         if (!*s2) putchar(*tx);
  79.     }
  80.     putchar('\n');
  81. }
  82.  
  83. int main() {
  84.     str_to_time(1599335154, "h:M:S p");  // 07:45:54 pm
  85.     str_to_time(683475320, "A d B Y");   // Thursday 29 August 1991
  86.     str_to_time(694310400, "A d B Y");   // Thursday 02 January 1992
  87.     str_to_time(951782400, "A d B Y");   // Tuesday 29 February 2000
  88.     str_to_time(951868800, "A d B Y");   // Wednesday 01 March 2000
  89.     str_to_time(978220800, "A d B Y");   // Sunday 31 December 2000
  90.     str_to_time(1598904481 , "A d B Y h:M:S p"); // Monday 31 August 2020 08:08:01 pm
  91.     str_to_time(1961670142 , "A d B Y h:M:S p"); // Sunday 29 February 2032 12:22:22 pm
  92.     str_to_time(0 , "A d B Y");          // Thursday 01 January 1970
  93. }
Advertisement
Add Comment
Please, Sign In to add comment