Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char *max_time(char *t1, char *t2) {
- if (t1 == NULL && t2 == NULL) {
- fprintf(stderr, "t1, t2 are null");
- exit(1);
- }
- if (t1 == NULL) {
- return t2;
- } else if (t2 == NULL) {
- return t1;
- }
- printf("Incoming time1: %s, size: %zun", t1, strlen(t1));
- printf("Incoming time2: %s, size: %zun", t2, strlen(t2));
- struct tm tm1, tm2;
- time_t time1, time2;
- double diff;
- memset(&tm1, 0, sizeof(struct tm));
- memset(&tm2, 0, sizeof(struct tm));
- memset(&time1, 0, sizeof(struct tm));
- memset(&time2, 0, sizeof(struct tm));
- strptime(t1, "%a, %d %b %Y %X %Z", &tm1);
- strptime(t2, "%a, %d %b %Y %X %Z", &tm2);
- time1 = mktime(&tm1);
- time2 = mktime(&tm2);
- /* debug */
- char str1[50];
- char str2[50];
- strftime(str1, 50, "%a, %d %b %Y %X %Z", &tm1);
- strftime(str2, 50, "%a, %d %b %Y %X %Z", &tm2);
- printf("Time1: %sn", str1);
- printf("Time2: %sn", str2);
- /* end debugging */
- diff = difftime(time1, time2);
- printf("Time diff: %fn", diff);
- if (diff > 0) {
- return t1;
- } else {
- return t2;
- }
- }
- char *t1[] = {"Tue, 27 Mar 2018 04:46:22 GMT",
- "Tue, 27 Mar 2018 04:47:46 GMT",
- "Tue, 27 Mar 2018 04:57:17 GMT",
- "Tue, 27 Mar 2018 04:49:31 GMT",
- "Tue, 27 Mar 2018 04:48:46 GMT",
- "Mon, 26 Mar 2018 03:44:55 GMT"};
- int i = 0;
- char *res = NULL;
- for(i = 0; i < 5; i++) {
- res = max_time(res, t1[i]);
- printf("Res: %sn", res);
- }
Add Comment
Please, Sign In to add comment