Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- const int MAX_LENGTH = 501;
- const int DS[] = {31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- const int OK[] = {1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
- const int init_w = 3;
- const int init_d = 0;
- const int init_m = 0;
- const int init_y = 1970;
- int pre = 0;
- int pre_w = init_w;
- int pre_d = init_d;
- int pre_m = init_m;
- int pre_y = init_y;
- fm(n, m)
- int n;
- int m;
- {
- while(1)
- if(n < m)
- return n;
- else
- n -= m;
- }
- void next() {
- pre += 60*60*24;
- pre_w = fm(++pre_w, 7);
- ++pre_d;
- if(OK[pre_m]) {
- if(pre_d == DS[pre_m]) {
- pre_d = 0;
- ++pre_m;
- }
- } else if(pre_d == 29) {
- pre_d = 0;
- ++pre_m;
- } else if(pre_d == 28) {
- if(!fm(pre_y, 100)) {
- if(!fm(pre_y, 4) && !fm(pre_y, 400));
- else {
- pre_d = 0;
- ++pre_m;
- }
- } else if(!fm(pre_y, 4));
- else {
- pre_d = 0;
- ++pre_m;
- }
- }
- if(pre_m == 12) {
- pre_m = 0;
- ++pre_y;
- }
- }
- #define PARAMS char** a, int s, int m, int h, int h2, int p
- #define MYFUNC(x) void Put ## x(PARAMS)
- typedef void (*delegateptr)(PARAMS);
- MYFUNC(d) { *a += sprintf(*a, "%02d", pre_d+1); }
- const char* wdays[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
- MYFUNC(A) { *a += sprintf(*a, wdays[pre_w]); }
- const char* months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
- MYFUNC(B) { *a += sprintf(*a, months[pre_m]); }
- MYFUNC(Y) { *a += sprintf(*a, "%04d", pre_y); }
- MYFUNC(H) { *a += sprintf(*a, "%02d", h); }
- MYFUNC(h) { *a += sprintf(*a, "%02d", h2); }
- MYFUNC(M) { *a += sprintf(*a, "%02d", m); }
- MYFUNC(S) { *a += sprintf(*a, "%02d", s); }
- const char* ampm[] = { "am", "pm" };
- MYFUNC(p) { *a += sprintf(*a, ampm[p]); }
- const delegateptr dptrs[] = {
- ['d'] = Putd,
- ['A'] = PutA,
- ['B'] = PutB,
- ['Y'] = PutY,
- ['H'] = PutH,
- ['h'] = Puth,
- ['M'] = PutM,
- ['S'] = PutS,
- ['p'] = Putp,
- };
- char* str_to_time(t, f)
- int t;
- char* f;
- {
- char* a = malloc(MAX_LENGTH);
- char* orig_a = a;
- int i, s, m, h, h2, p;
- i = MAX_LENGTH;
- while(--i) a[i] = '\0';
- s = fm(t, 60);
- m = fm((t-s) / 60, 60);
- h = fm(((((t - s) / 60) - m) / 60), 24);
- h2 = fm(h, 12);
- p = h2 < h;
- if(pre > t) {
- pre = 0; pre_w = init_w; pre_d = init_d; pre_m = init_m; pre_y = init_y;
- }
- while(pre < 1+t-(60*60*24))
- next();
- --f;
- for(;;) {
- if(!*(++f))
- return orig_a;
- if(!dptrs[*f])
- a += sprintf(a, "%c", *f);
- else
- dptrs[*f](&a, s, m, h, h2, p);
- }
- }
- void main() {
- printf(str_to_time(1599335154, "h:M:S p\n"));
- printf(str_to_time(683475320, "A d B Y\n"));
- }
Advertisement
Add Comment
Please, Sign In to add comment