fork download
  1. #include <iostream>
  2.  
  3.  
  4. // CODE FROM TIME LIBRARY, YOU DON'T NEED TO COPY THAT
  5.  
  6. typedef struct {
  7. uint8_t Second;
  8. uint8_t Minute;
  9. uint8_t Hour;
  10. uint8_t Wday; // day of week, sunday is day 1
  11. uint8_t Day;
  12. uint8_t Month;
  13. uint8_t Year; // offset from 1970;
  14. } tmElements_t, TimeElements, *tmElementsPtr_t;
  15.  
  16. /* Useful Constants */
  17. #define SECS_PER_MIN ((time_t)(60UL))
  18. #define SECS_PER_HOUR ((time_t)(3600UL))
  19. #define SECS_PER_DAY ((time_t)(SECS_PER_HOUR * 24UL))
  20. #define DAYS_PER_WEEK ((time_t)(7UL))
  21. #define SECS_PER_WEEK ((time_t)(SECS_PER_DAY * DAYS_PER_WEEK))
  22. #define SECS_PER_YEAR ((time_t)(SECS_PER_DAY * 365UL)) // TODO: ought to handle leap years
  23. #define SECS_YR_2000 ((time_t)(946684800UL)) // the time at the start of y2k
  24.  
  25. #define LEAP_YEAR(Y) ( ((1970+(Y))>0) && !((1970+(Y))%4) && ( ((1970+(Y))%100) || !((1970+(Y))%400) ) )
  26.  
  27. static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31}; // API starts months from 1, this array starts from 0
  28.  
  29. time_t makeTime(const tmElements_t &tm){
  30. // assemble time elements into time_t
  31. // note year argument is offset from 1970 (see macros in time.h to convert to other formats)
  32. // previous version used full four digit year (or digits since 2000),i.e. 2009 was 2009 or 9
  33.  
  34. int i;
  35. uint32_t seconds;
  36.  
  37. // seconds from 1970 till 1 jan 00:00:00 of the given year
  38. seconds= tm.Year*(SECS_PER_DAY * 365);
  39. for (i = 0; i < tm.Year; i++) {
  40. if (LEAP_YEAR(i)) {
  41. seconds += SECS_PER_DAY; // add extra days for leap years
  42. }
  43. }
  44.  
  45. // add days for this year, months start from 1
  46. for (i = 1; i < tm.Month; i++) {
  47. if ( (i == 2) && LEAP_YEAR(tm.Year)) {
  48. seconds += SECS_PER_DAY * 29;
  49. } else {
  50. seconds += SECS_PER_DAY * monthDays[i-1]; //monthDay array starts from 0
  51. }
  52. }
  53. seconds+= (tm.Day-1) * SECS_PER_DAY;
  54. seconds+= tm.Hour * SECS_PER_HOUR;
  55. seconds+= tm.Minute * SECS_PER_MIN;
  56. seconds+= tm.Second;
  57. return (time_t)seconds;
  58. }
  59.  
  60. // END OF CODE FROM TIME LIBRARY
  61.  
  62.  
  63.  
  64.  
  65. int main()
  66. {
  67. tmElements_t
  68. tmElements1,
  69. tmElements2;
  70.  
  71. tmElements1.Year = 2019 - 1970;
  72. tmElements1.Month = 12;
  73. tmElements1.Day = 24;
  74. tmElements1.Hour = 9;
  75. tmElements1.Minute = 0;
  76. tmElements1.Second = 0;
  77.  
  78. tmElements2.Year = 2019 - 1970;
  79. tmElements2.Month = 12;
  80. tmElements2.Day = 24;
  81. tmElements2.Hour = 9;
  82. tmElements2.Minute = 59;
  83. tmElements2.Second = 59;
  84.  
  85. time_t
  86. time1 = makeTime(tmElements1),
  87. time2 = makeTime(tmElements2),
  88. timeDifference = abs(time2 - time1);
  89.  
  90. printf( "time difference is %d seconds", timeDifference);
  91.  
  92. return 0;
  93. }
Success #stdin #stdout 0s 4432KB
stdin
Standard input is empty
stdout
time difference is 3599 seconds