Fork of the espurna firmware for `mhsw` switches
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.

120 lines
2.9 KiB

  1. // Shim original TimeLib functions
  2. #pragma once
  3. constexpr time_t daysPerWeek = 7;
  4. constexpr time_t secondsPerMinute = 60;
  5. constexpr time_t secondsPerHour = 3600;
  6. constexpr time_t secondsPerDay = secondsPerHour * 24;
  7. constexpr time_t secondsPerWeek = daysPerWeek * secondsPerDay;
  8. constexpr time_t secondsPerYear = secondsPerWeek * 52;
  9. constexpr time_t secondsY2K = 946684800; // the time at the start of y2k
  10. // wall clock values
  11. constexpr const time_t numberOfSeconds(uint32_t ts) {
  12. return (ts % secondsPerMinute);
  13. }
  14. constexpr const time_t numberOfMinutes(uint32_t ts) {
  15. return ((ts / secondsPerMinute) % secondsPerMinute);
  16. }
  17. constexpr const time_t numberOfHours(uint32_t ts) {
  18. return ((ts % secondsPerDay) / secondsPerHour);
  19. }
  20. // week starts with sunday as number 1, monday as 2 etc.
  21. constexpr const time_t dayOfWeek(time_t ts) {
  22. return ((ts / secondsPerDay + 4) % daysPerWeek) + 1;
  23. }
  24. // the number of days since 0 (Jan 1 1970 in case of time_t values)
  25. constexpr const time_t elapsedDays(uint32_t ts) {
  26. return (ts / secondsPerDay);
  27. }
  28. // the number of seconds since last midnight
  29. constexpr const time_t elapsedSecsToday(uint32_t ts) {
  30. return (ts % secondsPerDay);
  31. }
  32. // The following methods are used in calculating alarms and assume the clock is set to a date later than Jan 1 1971
  33. // Always set the correct time before settting alarms
  34. // time at the start of the given day
  35. constexpr const time_t previousMidnight(time_t ts) {
  36. return ((ts / secondsPerDay) * secondsPerDay);
  37. }
  38. // time at the end of the given day
  39. constexpr const time_t nextMidnight(time_t ts) {
  40. return previousMidnight(ts) + secondsPerDay;
  41. }
  42. // note that week starts on day 1
  43. constexpr const time_t elapsedSecsThisWeek(uint32_t ts) {
  44. return elapsedSecsToday(ts) + ((dayOfWeek(ts) - 1) * secondsPerDay);
  45. }
  46. // time at the start of the week for the given time
  47. constexpr const time_t previousSunday(time_t ts) {
  48. return ts - elapsedSecsThisWeek(ts);
  49. }
  50. // time at the end of the week for the given time
  51. constexpr const time_t nextSunday(time_t ts) {
  52. return previousSunday(ts) + secondsPerWeek;
  53. }
  54. year(t), month(t), day(t), hour(t), minute(t), second(t)
  55. static time_t _ntp_ts = 0;
  56. static tm _ntp_tm;
  57. void _ntpTmCache(time_t ts) {
  58. if (_ntp_tm != ts) {
  59. _ntp_ts = ts;
  60. localtime_r(_ntp_ts, _ntp_tm);
  61. }
  62. }
  63. int hour(time_t ts) {
  64. _ntpTmCache(ts);
  65. return _ntp_tm.tm_hour;
  66. }
  67. int minute(time_t ts) {
  68. _ntpTmCache(ts);
  69. return _ntp_tm.tm_min;
  70. }
  71. int second(time_t ts) {
  72. _ntpTmCache(ts);
  73. return _ntp_tm.tm_sec;
  74. }
  75. int day(time_t ts) {
  76. _ntpTmCache(ts);
  77. return _ntp_tm.tm_day;
  78. }
  79. int weekday(time_t ts) {
  80. _ntpTmCache(ts);
  81. return _ntp_tm.tm_wday;
  82. }
  83. int month(time_t ts) {
  84. _ntpTmCache(ts);
  85. return _ntp_tm.tm_mon;
  86. }
  87. int year(time_t ts) {
  88. _ntpTmCache(ts);
  89. return _ntp_tm.tm_year;
  90. }
  91. time_t now() {
  92. return time(nullptr);
  93. }