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.

108 lines
2.7 KiB

  1. /*
  2. Part of NTP MODULE
  3. */
  4. // Based on https://github.com/PaulStoffregen/time
  5. #pragma once
  6. #include <time.h>
  7. #include <sys/time.h>
  8. constexpr time_t daysPerWeek = 7;
  9. constexpr time_t secondsPerMinute = 60;
  10. constexpr time_t secondsPerHour = 3600;
  11. constexpr time_t secondsPerDay = secondsPerHour * 24;
  12. constexpr time_t secondsPerWeek = daysPerWeek * secondsPerDay;
  13. constexpr time_t secondsPerYear = secondsPerWeek * 52;
  14. constexpr time_t secondsY2K = 946684800; // the time at the start of y2k
  15. // wall clock values
  16. template <typename T>
  17. constexpr const T numberOfSeconds(T ts) {
  18. return (ts % (T)secondsPerMinute);
  19. }
  20. template <typename T>
  21. constexpr const T numberOfMinutes(T ts) {
  22. return ((ts / (T)secondsPerMinute) % (T)secondsPerMinute);
  23. }
  24. template <typename T>
  25. constexpr const time_t numberOfHours(T ts) {
  26. return ((ts % (T)secondsPerDay) / (T)secondsPerHour);
  27. }
  28. // week starts with sunday as number 1, monday as 2 etc.
  29. constexpr const int dayOfWeek(time_t ts) {
  30. return ((ts / secondsPerDay + 4) % daysPerWeek) + 1;
  31. }
  32. // the number of days since 0 (Jan 1 1970 in case of time_t values)
  33. constexpr const int elapsedDays(uint32_t ts) {
  34. return (ts / secondsPerDay);
  35. }
  36. // the number of seconds since last midnight
  37. constexpr const uint32_t elapsedSecsToday(uint32_t ts) {
  38. return (ts % (uint32_t)secondsPerDay);
  39. }
  40. // note that week starts on day 1
  41. constexpr const uint32_t elapsedSecsThisWeek(uint32_t ts) {
  42. return elapsedSecsToday(ts) + ((dayOfWeek(ts) - 1) * (uint32_t)secondsPerDay);
  43. }
  44. // The following methods are used in calculating alarms and assume the clock is set to a date later than Jan 1 1971
  45. // Always set the correct time before settting alarms
  46. // time at the start of the given day
  47. constexpr const time_t previousMidnight(time_t ts) {
  48. return ((ts / secondsPerDay) * secondsPerDay);
  49. }
  50. // time at the end of the given day
  51. constexpr const time_t nextMidnight(time_t ts) {
  52. return previousMidnight(ts) + secondsPerDay;
  53. }
  54. // time at the start of the week for the given time
  55. constexpr const time_t previousSunday(time_t ts) {
  56. return ts - elapsedSecsThisWeek(ts);
  57. }
  58. // time at the end of the week for the given time
  59. constexpr const time_t nextSunday(time_t ts) {
  60. return previousSunday(ts) + secondsPerWeek;
  61. }
  62. int utc_hour(time_t ts);
  63. int utc_minute(time_t ts);
  64. int utc_second(time_t ts);
  65. int utc_day(time_t ts);
  66. int utc_weekday(time_t ts);
  67. int utc_month(time_t ts);
  68. int utc_year(time_t ts);
  69. int hour(time_t ts);
  70. int minute(time_t ts);
  71. int second(time_t ts);
  72. int day(time_t ts);
  73. int weekday(time_t ts);
  74. int month(time_t ts);
  75. int year(time_t ts);
  76. int hour();
  77. int minute();
  78. int second();
  79. int day();
  80. int weekday();
  81. int month();
  82. int year();
  83. time_t now();