Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

47 рядки
1.4 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. __attribute__((__optimize__("O2")))
  16. timestamp_t makeTimestamp(const tmElements_t &time) {
  17. timestamp_t timestamp;
  18. timestamp += (time.Day - 1) * SECS_PER_DAY;
  19. timestamp += time.Hour * SECS_PER_HOUR;
  20. timestamp += time.Minute * SECS_PER_MIN;
  21. timestamp += time.Second;
  22. return timestamp;
  23. }
  24. __attribute__((__optimize__("O2")))
  25. void breakTime(timestamp_t timestamp, tmElements_t &time) {
  26. time.Year = 0;
  27. time.Month = 0;
  28. time.Day = 0;
  29. time.Second = timestamp % 60;
  30. timestamp /= 60; // now it is minutes
  31. time.Minute = timestamp % 60;
  32. timestamp /= 60; // now it is hours
  33. time.Hour = timestamp % 24;
  34. timestamp /= 24; // now it is days
  35. time.Day = timestamp; //this is purely for indication / computation only as it might get over the number of days in a month
  36. }
  37. }
  38. }