You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 regels
1.3 KiB

  1. #include "Time2.h"
  2. /*==============================================================================*/
  3. /* Useful Constants */
  4. #define SECS_PER_MIN (60UL)
  5. #define SECS_PER_HOUR (3600UL)
  6. #define SECS_PER_DAY (SECS_PER_HOUR * 24UL)
  7. #define DAYS_PER_WEEK (7UL)
  8. #define SECS_PER_WEEK (SECS_PER_DAY * DAYS_PER_WEEK)
  9. #define SECS_PER_YEAR (SECS_PER_WEEK * 52UL)
  10. #define SECS_YR_2000 (946684800UL) // the time at the start of y2k
  11. /*==============================================================================*/
  12. /* Utility functions */
  13. namespace utils {
  14. namespace time {
  15. timestamp_t makeTimestamp(const tmElements_t &time) {
  16. timestamp_t timestamp;
  17. timestamp += (time.day - 1) * SECS_PER_DAY;
  18. timestamp += time.hour * SECS_PER_HOUR;
  19. timestamp += time.minute * SECS_PER_MIN;
  20. timestamp += time.second;
  21. return timestamp;
  22. }
  23. void breakTime(timestamp_t timestamp, tmElements_t &time) {
  24. time.year = 0;
  25. time.month = 0;
  26. time.day = 0;
  27. time.second = timestamp % 60;
  28. timestamp /= 60; // now it is minutes
  29. time.minute = timestamp % 60;
  30. timestamp /= 60; // now it is hours
  31. time.hour = timestamp % 24;
  32. timestamp /= 24; // now it is days
  33. time.day = timestamp; //this is purely for indication / computation only as it might get over the number of days in a month
  34. }
  35. }
  36. }