Guest User

Untitled

a guest
Apr 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. char *max_time(char *t1, char *t2) {
  2. if (t1 == NULL && t2 == NULL) {
  3. fprintf(stderr, "t1, t2 are null");
  4. exit(1);
  5. }
  6.  
  7. if (t1 == NULL) {
  8. return t2;
  9. } else if (t2 == NULL) {
  10. return t1;
  11. }
  12.  
  13.  
  14. printf("Incoming time1: %s, size: %zun", t1, strlen(t1));
  15. printf("Incoming time2: %s, size: %zun", t2, strlen(t2));
  16.  
  17. struct tm tm1, tm2;
  18. time_t time1, time2;
  19. double diff;
  20.  
  21. memset(&tm1, 0, sizeof(struct tm));
  22. memset(&tm2, 0, sizeof(struct tm));
  23. memset(&time1, 0, sizeof(struct tm));
  24. memset(&time2, 0, sizeof(struct tm));
  25.  
  26. strptime(t1, "%a, %d %b %Y %X %Z", &tm1);
  27. strptime(t2, "%a, %d %b %Y %X %Z", &tm2);
  28. time1 = mktime(&tm1);
  29. time2 = mktime(&tm2);
  30.  
  31. /* debug */
  32. char str1[50];
  33. char str2[50];
  34. strftime(str1, 50, "%a, %d %b %Y %X %Z", &tm1);
  35. strftime(str2, 50, "%a, %d %b %Y %X %Z", &tm2);
  36. printf("Time1: %sn", str1);
  37. printf("Time2: %sn", str2);
  38. /* end debugging */
  39.  
  40.  
  41. diff = difftime(time1, time2);
  42. printf("Time diff: %fn", diff);
  43. if (diff > 0) {
  44. return t1;
  45. } else {
  46. return t2;
  47. }
  48. }
  49.  
  50. char *t1[] = {"Tue, 27 Mar 2018 04:46:22 GMT",
  51. "Tue, 27 Mar 2018 04:47:46 GMT",
  52. "Tue, 27 Mar 2018 04:57:17 GMT",
  53. "Tue, 27 Mar 2018 04:49:31 GMT",
  54. "Tue, 27 Mar 2018 04:48:46 GMT",
  55. "Mon, 26 Mar 2018 03:44:55 GMT"};
  56.  
  57. int i = 0;
  58. char *res = NULL;
  59. for(i = 0; i < 5; i++) {
  60. res = max_time(res, t1[i]);
  61. printf("Res: %sn", res);
  62. }
Add Comment
Please, Sign In to add comment