ESP32-PaxCounter/lib/microTime/src/Time.cpp

355 lines
9.8 KiB
C++
Raw Normal View History

2019-03-02 14:03:36 +01:00
/*
time.c - low level time and date functions
Copyright (c) Michael Margolis 2009-2014
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1.0 6 Jan 2010 - initial release
1.1 12 Feb 2010 - fixed leap year calculation error
1.2 1 Nov 2010 - fixed setTime bug (thanks to Korman for this)
1.3 24 Mar 2012 - many edits by Paul Stoffregen: fixed timeStatus() to update
status, updated examples for Arduino 1.0, fixed ARM
compatibility issues, added TimeArduinoDue and TimeTeensy3
examples, add error checking and messages to RTC examples,
add examples to DS1307RTC library.
1.4 5 Sep 2014 - compatibility with Arduino 1.5.7
*/
2019-03-02 20:01:27 +01:00
#include <Arduino.h>
2019-03-02 14:03:36 +01:00
#define TIMELIB_ENABLE_MILLIS
#define usePPS
#include "TimeLib.h"
// Convert days since epoch to week day. Sunday is day 1.
#define DAYS_TO_WDAY(x) (((x) + 4) % 7) + 1
2019-03-02 20:01:27 +01:00
static tmElements_t cacheElements; // a cache of time elements
static time_t cacheTime; // the time the cache was updated
static uint32_t syncInterval =
300; // time sync will be attempted after this many seconds
2019-03-02 14:03:36 +01:00
void refreshCache(time_t t) {
if (t != cacheTime) {
breakTime(t, cacheElements);
cacheTime = t;
}
}
2019-03-02 20:01:27 +01:00
int hour() { // the hour now
return hour(now());
2019-03-02 14:03:36 +01:00
}
int hour(time_t t) { // the hour for the given time
refreshCache(t);
return cacheElements.Hour;
}
int hourFormat12() { // the hour now in 12 hour format
2019-03-02 20:01:27 +01:00
return hourFormat12(now());
2019-03-02 14:03:36 +01:00
}
int hourFormat12(time_t t) { // the hour for the given time in 12 hour format
refreshCache(t);
2019-03-02 20:01:27 +01:00
if (cacheElements.Hour == 0)
2019-03-02 14:03:36 +01:00
return 12; // 12 midnight
2019-03-02 20:01:27 +01:00
else if (cacheElements.Hour > 12)
return cacheElements.Hour - 12;
2019-03-02 14:03:36 +01:00
else
2019-03-02 20:01:27 +01:00
return cacheElements.Hour;
2019-03-02 14:03:36 +01:00
}
uint8_t isAM() { // returns true if time now is AM
2019-03-02 20:01:27 +01:00
return !isPM(now());
2019-03-02 14:03:36 +01:00
}
uint8_t isAM(time_t t) { // returns true if given time is AM
2019-03-02 20:01:27 +01:00
return !isPM(t);
2019-03-02 14:03:36 +01:00
}
uint8_t isPM() { // returns true if PM
2019-03-02 20:01:27 +01:00
return isPM(now());
2019-03-02 14:03:36 +01:00
}
uint8_t isPM(time_t t) { // returns true if PM
2019-03-02 20:01:27 +01:00
return (hour(t) >= 12);
2019-03-02 14:03:36 +01:00
}
2019-03-02 20:01:27 +01:00
int minute() { return minute(now()); }
2019-03-02 14:03:36 +01:00
int minute(time_t t) { // the minute for the given time
refreshCache(t);
return cacheElements.Minute;
}
2019-03-02 20:01:27 +01:00
int second() { return second(now()); }
2019-03-02 14:03:36 +01:00
2019-03-02 20:01:27 +01:00
int second(time_t t) { // the second for the given time
2019-03-02 14:03:36 +01:00
refreshCache(t);
return cacheElements.Second;
}
int millisecond() {
uint32_t ms;
now(ms);
ms = ms / 1000;
return (int)ms;
}
int microsecond() {
uint32_t us;
now(us);
return (int)us;
}
2019-03-02 20:01:27 +01:00
int day() { return (day(now())); }
2019-03-02 14:03:36 +01:00
int day(time_t t) { // the day for the given time (0-6)
refreshCache(t);
return cacheElements.Day;
}
2019-03-02 20:01:27 +01:00
int weekday() { // Sunday is day 1
return weekday(now());
2019-03-02 14:03:36 +01:00
}
int weekday(time_t t) {
refreshCache(t);
return cacheElements.Wday;
}
2019-03-02 20:01:27 +01:00
int month() { return month(now()); }
int month(time_t t) { // the month for the given time
2019-03-02 14:03:36 +01:00
refreshCache(t);
return cacheElements.Month;
}
2019-03-02 20:01:27 +01:00
int year() { // as in Processing, the full four digit year: (2009, 2010 etc)
return year(now());
2019-03-02 14:03:36 +01:00
}
int year(time_t t) { // the year for the given time
refreshCache(t);
return tmYearToCalendar(cacheElements.Year);
}
2019-03-02 20:01:27 +01:00
/*============================================================================*/
2019-03-02 14:03:36 +01:00
/* functions to convert to and from system time */
2019-03-02 20:01:27 +01:00
/* These are for interfacing with time serivces and are not normally needed in a
* sketch */
2019-03-02 14:03:36 +01:00
// leap year calulator expects year argument as years offset from 1970
2019-03-02 20:01:27 +01:00
#define LEAP_YEAR(Y) \
(((1970 + (Y)) > 0) && !((1970 + (Y)) % 4) && \
(((1970 + (Y)) % 100) || !((1970 + (Y)) % 400)))
#define daysInYear(year) ((time_t)(LEAP_YEAR(year) ? 366 : 365))
2019-03-02 14:03:36 +01:00
2019-03-02 20:01:27 +01:00
static const uint8_t monthDays[] = {
31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31}; // API starts months from 1, this array starts from 0
2019-03-02 14:03:36 +01:00
2019-03-02 20:01:27 +01:00
void breakTime(time_t time, tmElements_t &tm) {
// break the given time_t into time components
// this is a more compact version of the C library localtime function
// note that year is offset from 1970 !!!
2019-03-02 14:03:36 +01:00
uint8_t period;
time_t length;
tm.Second = time % 60;
time /= 60; // now it is minutes
tm.Minute = time % 60;
time /= 60; // now it is hours
tm.Hour = time % 24;
time /= 24; // now it is days since 1 Jan 1970
// if the number of days since epoch matches cacheTime, then can take date
// elements from cacheElements and avoid expensive calculation.
if (time == (cacheTime / SECS_PER_DAY)) {
2019-03-02 20:01:27 +01:00
if (&tm != &cacheElements) { // check whether tm is actually cacheElements
2019-03-02 14:03:36 +01:00
tm.Wday = cacheElements.Wday;
tm.Day = cacheElements.Day;
tm.Month = cacheElements.Month;
tm.Year = cacheElements.Year;
}
return;
}
tm.Wday = DAYS_TO_WDAY(time);
2019-03-02 20:01:27 +01:00
2019-03-02 14:03:36 +01:00
period = 0;
2019-03-02 20:01:27 +01:00
while (time >= (length = daysInYear(period))) {
2019-03-02 14:03:36 +01:00
time -= length;
period++;
}
2019-03-02 20:01:27 +01:00
tm.Year = period; // year is offset from 1970
2019-03-02 14:03:36 +01:00
// time is now days since 1 Jan of the year
bool leap_year = LEAP_YEAR(period);
period = 0;
2019-03-02 20:01:27 +01:00
while (period < 12 &&
time >= (length = monthDays[period] + (leap_year && period == 1))) {
2019-03-02 14:03:36 +01:00
time -= length;
period++;
}
2019-03-02 20:01:27 +01:00
tm.Month = period + 1; // jan is month 1
2019-03-02 14:03:36 +01:00
// time is now days since the 1st day of the month
2019-03-02 20:01:27 +01:00
tm.Day = time + 1; // day of month
2019-03-02 14:03:36 +01:00
}
2019-03-02 20:01:27 +01:00
time_t makeTime(const tmElements_t &tm) {
// assemble time elements into time_t
// note year argument is offset from 1970 (see macros in time.h to convert to
// other formats) previous version used full four digit year (or digits since
// 2000),i.e. 2009 was 2009 or 9
2019-03-02 14:03:36 +01:00
int i;
uint32_t seconds;
// seconds from 1970 till 1 jan 00:00:00 of the given year
seconds = SECS_PER_DAY * (365 * tm.Year);
for (i = 0; i < tm.Year; i++) {
if (LEAP_YEAR(i)) {
2019-03-02 20:01:27 +01:00
seconds += SECS_PER_DAY; // add extra days for leap years
2019-03-02 14:03:36 +01:00
}
}
2019-03-02 20:01:27 +01:00
2019-03-02 14:03:36 +01:00
// add days for this year, months start from 1
for (i = 1; i < tm.Month; i++) {
2019-03-02 20:01:27 +01:00
if ((i == 2) && LEAP_YEAR(tm.Year)) {
2019-03-02 14:03:36 +01:00
seconds += SECS_PER_DAY * 29;
} else {
2019-03-02 20:01:27 +01:00
seconds += SECS_PER_DAY * monthDays[i - 1]; // monthDay array starts from
// 0
2019-03-02 14:03:36 +01:00
}
}
2019-03-02 20:01:27 +01:00
seconds += (tm.Day - 1) * SECS_PER_DAY;
seconds += tm.Hour * SECS_PER_HOUR;
seconds += tm.Minute * SECS_PER_MIN;
seconds += tm.Second;
return (time_t)seconds;
2019-03-02 14:03:36 +01:00
}
2019-03-02 20:01:27 +01:00
/*=====================================================*/
2019-03-02 14:03:36 +01:00
/* Low level system time functions */
static time_t sysTime = 0;
static uint32_t prevMicros = 0;
static time_t nextSyncTime = 0;
static timeStatus_t Status = timeNotSet;
2019-03-02 20:01:27 +01:00
getExternalTime getTimePtr; // pointer to external sync function
// setExternalTime setTimePtr; // not used in this version
2019-03-02 14:03:36 +01:00
2019-03-02 20:01:27 +01:00
#ifdef TIME_DRIFT_INFO // define this to get drift data
time_t sysUnsyncedTime = 0; // the time sysTime unadjusted by sync
2019-03-02 14:03:36 +01:00
#endif
#ifdef usePPS
2019-03-02 20:01:27 +01:00
time_t SyncToPPS() {
2019-03-02 14:03:36 +01:00
sysTime++;
prevMicros = micros();
2019-03-02 20:01:27 +01:00
return sysTime;
2019-03-02 14:03:36 +01:00
}
#endif
time_t now() {
uint32_t sysTimeMicros;
return now(sysTimeMicros);
}
2019-03-02 20:01:27 +01:00
time_t now(uint32_t &sysTimeMicros) {
2019-03-02 14:03:36 +01:00
// calculate number of seconds passed since last call to now()
2019-03-02 20:01:27 +01:00
while ((sysTimeMicros = micros() - prevMicros) >= 1000000) {
2019-03-02 14:03:36 +01:00
// micros() and prevMicros are both unsigned ints thus the subtraction will
// always result in a positive difference. This is OK since it corrects for
// wrap-around and micros() is monotonic.
sysTime++;
2019-03-02 20:01:27 +01:00
prevMicros += 1000000;
2019-03-02 14:03:36 +01:00
#ifdef TIME_DRIFT_INFO
2019-03-02 20:01:27 +01:00
sysUnsyncedTime++; // this can be compared to the synced time to measure
// long term drift
2019-03-02 14:03:36 +01:00
#endif
}
if (nextSyncTime <= sysTime) {
if (getTimePtr != 0) {
time_t t = getTimePtr();
2019-03-02 20:01:27 +01:00
2019-03-02 14:03:36 +01:00
if (t != 0) {
setTime(t);
} else {
nextSyncTime = sysTime + syncInterval;
2019-03-02 20:01:27 +01:00
Status = (Status == timeNotSet) ? timeNotSet : timeNeedsSync;
2019-03-02 14:03:36 +01:00
}
}
2019-03-02 20:01:27 +01:00
}
2019-03-02 14:03:36 +01:00
return sysTime;
}
2019-03-02 20:01:27 +01:00
void setTime(time_t t) {
2019-03-02 14:03:36 +01:00
#ifdef TIME_DRIFT_INFO
2019-03-02 20:01:27 +01:00
if (sysUnsyncedTime == 0)
sysUnsyncedTime = t; // store the time of the first call to set a valid Time
2019-03-02 14:03:36 +01:00
#endif
2019-03-02 20:01:27 +01:00
sysTime = t;
nextSyncTime = t + (time_t)syncInterval;
2019-03-02 14:03:36 +01:00
Status = timeSet;
2019-03-02 20:01:27 +01:00
#ifndef usePPS
prevMicros =
micros(); // restart counting from now (thanks to Korman for this fix)
#endif
}
2019-03-02 14:03:36 +01:00
void setTime(int hr, int min, int sec, int dy, int mnth, int yr) {
2019-03-02 20:01:27 +01:00
// year can be given as full four digit year or two digts (2010 or 10 for
// 2010); it is converted to years since 1970
2019-03-02 14:03:36 +01:00
if (yr > 99)
2019-03-02 20:01:27 +01:00
yr = CalendarYrToTm(yr);
2019-03-02 14:03:36 +01:00
else
2019-03-02 20:01:27 +01:00
yr = tmYearToY2k(yr);
2019-03-02 14:03:36 +01:00
cacheElements.Year = yr;
cacheElements.Month = mnth;
cacheElements.Day = dy;
cacheElements.Hour = hr;
cacheElements.Minute = min;
cacheElements.Second = sec;
cacheTime = makeTime(cacheElements);
cacheElements.Wday = DAYS_TO_WDAY(cacheTime / SECS_PER_DAY);
setTime(cacheTime);
}
2019-03-02 20:01:27 +01:00
void adjustTime(long adjustment) { sysTime += adjustment; }
2019-03-02 14:03:36 +01:00
// indicates if time has been set and recently synchronized
timeStatus_t timeStatus() {
now(); // required to actually update the status
return Status;
}
2019-03-02 20:01:27 +01:00
void setSyncProvider(getExternalTime getTimeFunction) {
getTimePtr = getTimeFunction;
2019-03-02 14:03:36 +01:00
nextSyncTime = sysTime;
now(); // this will sync the clock
}
2019-03-02 20:01:27 +01:00
void setSyncInterval(
time_t interval) { // set the number of seconds between re-sync
2019-03-02 14:03:36 +01:00
syncInterval = (uint32_t)interval;
nextSyncTime = sysTime + syncInterval;
}