PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

TimeLib.h 6.4KB

3 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. time.h - low level time and date functions
  3. */
  4. /*
  5. July 3 2011 - fixed elapsedSecsThisWeek macro (thanks Vincent Valdy for this)
  6. - fixed daysToTime_t macro (thanks maniacbug)
  7. */
  8. #ifndef _Time_h
  9. #ifdef __cplusplus
  10. #define _Time_h
  11. #include <inttypes.h>
  12. #ifndef __AVR__
  13. #include <sys/types.h> // for __time_t_defined, but avr libc lacks sys/types.h
  14. #endif
  15. #if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc
  16. typedef unsigned long time_t;
  17. #endif
  18. // This ugly hack allows us to define C++ overloaded functions, when included
  19. // from within an extern "C", as newlib's sys/stat.h does. Actually it is
  20. // intended to include "time.h" from the C library (on ARM, but AVR does not
  21. // have that file at all). On Mac and Windows, the compiler will find this
  22. // "Time.h" instead of the C library "time.h", so we may cause other weird
  23. // and unpredictable effects by conflicting with the C library header "time.h",
  24. // but at least this hack lets us define C++ functions as intended. Hopefully
  25. // nothing too terrible will result from overriding the C library header?!
  26. extern "C++" {
  27. typedef enum {timeNotSet, timeNeedsSync, timeSet
  28. } timeStatus_t ;
  29. typedef enum {
  30. dowInvalid, dowSunday, dowMonday, dowTuesday, dowWednesday, dowThursday, dowFriday, dowSaturday
  31. } timeDayOfWeek_t;
  32. typedef enum {
  33. tmSecond, tmMinute, tmHour, tmWday, tmDay,tmMonth, tmYear, tmNbrFields
  34. } tmByteFields;
  35. typedef struct {
  36. uint8_t Second;
  37. uint8_t Minute;
  38. uint8_t Hour;
  39. uint8_t Wday; // day of week, sunday is day 1
  40. uint8_t Day;
  41. uint8_t Month;
  42. uint8_t Year; // offset from 1970;
  43. } tmElements_t, TimeElements, *tmElementsPtr_t;
  44. //convenience macros to convert to and from tm years
  45. #define tmYearToCalendar(Y) ((Y) + 1970) // full four digit year
  46. #define CalendarYrToTm(Y) ((Y) - 1970)
  47. #define tmYearToY2k(Y) ((Y) - 30) // offset is from 2000
  48. #define y2kYearToTm(Y) ((Y) + 30)
  49. typedef time_t(*getExternalTime)();
  50. //typedef void (*setExternalTime)(const time_t); // not used in this version
  51. /*==============================================================================*/
  52. /* Useful Constants */
  53. #define SECS_PER_MIN ((time_t)(60UL))
  54. #define SECS_PER_HOUR ((time_t)(3600UL))
  55. #define SECS_PER_DAY ((time_t)(SECS_PER_HOUR * 24UL))
  56. #define DAYS_PER_WEEK ((time_t)(7UL))
  57. #define SECS_PER_WEEK ((time_t)(SECS_PER_DAY * DAYS_PER_WEEK))
  58. #define SECS_PER_YEAR ((time_t)(SECS_PER_DAY * 365UL)) // TODO: ought to handle leap years
  59. #define SECS_YR_2000 ((time_t)(946684800UL)) // the time at the start of y2k
  60. /* Useful Macros for getting elapsed time */
  61. #define numberOfSeconds(_time_) ((_time_) % SECS_PER_MIN)
  62. #define numberOfMinutes(_time_) (((_time_) / SECS_PER_MIN) % SECS_PER_MIN)
  63. #define numberOfHours(_time_) (((_time_) % SECS_PER_DAY) / SECS_PER_HOUR)
  64. #define dayOfWeek(_time_) ((((_time_) / SECS_PER_DAY + 4) % DAYS_PER_WEEK)+1) // 1 = Sunday
  65. #define elapsedDays(_time_) ((_time_) / SECS_PER_DAY) // this is number of days since Jan 1 1970
  66. #define elapsedSecsToday(_time_) ((_time_) % SECS_PER_DAY) // the number of seconds since last midnight
  67. // The following macros are used in calculating alarms and assume the clock is set to a date later than Jan 1 1971
  68. // Always set the correct time before setting alarms
  69. #define previousMidnight(_time_) (((_time_) / SECS_PER_DAY) * SECS_PER_DAY) // time at the start of the given day
  70. #define nextMidnight(_time_) (previousMidnight(_time_) + SECS_PER_DAY) // time at the end of the given day
  71. #define elapsedSecsThisWeek(_time_) (elapsedSecsToday(_time_) + ((dayOfWeek(_time_)-1) * SECS_PER_DAY)) // note that week starts on day 1
  72. #define previousSunday(_time_) ((_time_) - elapsedSecsThisWeek(_time_)) // time at the start of the week for the given time
  73. #define nextSunday(_time_) (previousSunday(_time_)+SECS_PER_WEEK) // time at the end of the week for the given time
  74. /* Useful Macros for converting elapsed time to a time_t */
  75. #define minutesToTime_t ((M)) ( (M) * SECS_PER_MIN)
  76. #define hoursToTime_t ((H)) ( (H) * SECS_PER_HOUR)
  77. #define daysToTime_t ((D)) ( (D) * SECS_PER_DAY) // fixed on Jul 22 2011
  78. #define weeksToTime_t ((W)) ( (W) * SECS_PER_WEEK)
  79. /*============================================================================*/
  80. /* time and date functions */
  81. int hour(); // the hour now
  82. int hour(time_t t); // the hour for the given time
  83. int hourFormat12(); // the hour now in 12 hour format
  84. int hourFormat12(time_t t); // the hour for the given time in 12 hour format
  85. uint8_t isAM(); // returns true if time now is AM
  86. uint8_t isAM(time_t t); // returns true the given time is AM
  87. uint8_t isPM(); // returns true if time now is PM
  88. uint8_t isPM(time_t t); // returns true the given time is PM
  89. int minute(); // the minute now
  90. int minute(time_t t); // the minute for the given time
  91. int second(); // the second now
  92. int second(time_t t); // the second for the given time
  93. int day(); // the day now
  94. int day(time_t t); // the day for the given time
  95. int weekday(); // the weekday now (Sunday is day 1)
  96. int weekday(time_t t); // the weekday for the given time
  97. int month(); // the month now (Jan is month 1)
  98. int month(time_t t); // the month for the given time
  99. int year(); // the full four digit year: (2009, 2010 etc)
  100. int year(time_t t); // the year for the given time
  101. time_t now(); // return the current time as seconds since Jan 1 1970
  102. void setTime(time_t t);
  103. void setTime(int hr,int min,int sec,int day, int month, int yr);
  104. void adjustTime(long adjustment);
  105. /* date strings */
  106. #define dt_MAX_STRING_LEN 9 // length of longest date string (excluding terminating null)
  107. char* monthStr(uint8_t month);
  108. char* dayStr(uint8_t day);
  109. char* monthShortStr(uint8_t month);
  110. char* dayShortStr(uint8_t day);
  111. /* time sync functions */
  112. timeStatus_t timeStatus(); // indicates if time has been set and recently synchronized
  113. void setSyncProvider( getExternalTime getTimeFunction); // identify the external time provider
  114. void setSyncInterval(time_t interval); // set the number of seconds between re-sync
  115. /* low level functions to convert to and from system time */
  116. void breakTime(time_t time, tmElements_t &tm); // break time_t into elements
  117. time_t makeTime(const tmElements_t &tm); // convert time elements into time_t
  118. } // extern "C++"
  119. #endif // __cplusplus
  120. #endif /* _Time_h */