fork download
  1.  
  2. #include <algorithm>
  3. #include <iostream>
  4.  
  5. struct SYSTEMTIME
  6. {
  7. int wHour;
  8. int wDayOfWeek;
  9. int wMinute;
  10. } ;
  11.  
  12. // store a table of the difference in days to account for the
  13. // rollover of days (Saturday/Sunday, Friday/Monday, etc.)
  14. size_t day_difference_table[7][7] =
  15. {
  16. {0, 1, 2, 3, 3, 2, 1},
  17. {1, 0, 1, 2, 3, 3, 2},
  18. {2, 1, 0, 1, 2, 3, 3},
  19. {3, 2, 1, 0, 1, 2, 3},
  20. {3, 3, 2, 1, 0, 1, 2},
  21. {2, 3, 3, 2, 1, 0, 1},
  22. {1, 2, 3, 3, 2, 1, 0}
  23. };
  24.  
  25. // returns a single size_t value for the difference in times
  26. // by having the days->hours->minutes shifted over 8 bits each
  27. size_t timediff(const SYSTEMTIME& t0, const SYSTEMTIME& t1)
  28. {
  29. // start with an assumption of an absolute match
  30. size_t diff = 0;
  31.  
  32. // get the absolute value of the difference in the days
  33. // as the day exerts the biggest influence on the result
  34. diff = day_difference_table[t0.wDayOfWeek][t1.wDayOfWeek];
  35.  
  36. // shift right, since days are "worth more" than hours or minutes...
  37. diff <<= 8;
  38.  
  39. // then the hours...
  40. diff += std::abs(t0.wHour - t1.wHour);
  41.  
  42. // shift right, since hours are "worth more" than minutes
  43. diff <<= 8;
  44.  
  45. // then the minutes
  46. diff += std::abs(t0.wMinute - t1.wMinute);
  47.  
  48. // nothing left to shift ;)
  49.  
  50. return diff;
  51. }
  52.  
  53.  
  54.  
  55. int main()
  56. {
  57. SYSTEMTIME m_MatchTime[3];
  58.  
  59. //current time
  60. m_MatchTime[0].wDayOfWeek = 0;
  61. m_MatchTime[0].wHour = 23;
  62. m_MatchTime[0].wMinute = 59;
  63.  
  64. // 23:59 from 0
  65. m_MatchTime[1].wDayOfWeek = 0;
  66. m_MatchTime[1].wHour = 0;
  67. m_MatchTime[1].wMinute = 0;
  68.  
  69. // 0:01 from 0
  70. m_MatchTime[2].wDayOfWeek = 1;
  71. m_MatchTime[2].wHour = 0;
  72. m_MatchTime[2].wMinute = 0;
  73.  
  74. std::cout << "Difference (23:59 actuallty): " << timediff(m_MatchTime[0], m_MatchTime[1]) << "\n";
  75. std::cout << "Difference: (0:01 actually)" << timediff(m_MatchTime[0], m_MatchTime[2]) << "\n";
  76. }
  77.  
Success #stdin #stdout 0.01s 2724KB
stdin
Standard input is empty
stdout
Difference (23:59 actuallty): 5947
Difference: (0:01 actually)71483