PaulStoffregen / Time

Time library for Arduino

Home Page:http://playground.arduino.cc/code/time

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Epoch and Time offset

AdvdLaar opened this issue · comments

commented

Please use this form only to report code defects or bugs.

For any question, even questions directly pertaining to this code, post your question on the forums related to the board you are using.

Arduino: forum.arduino.cc
Teensy: forum.pjrc.com
ESP8266: www.esp8266.com
ESP32: www.esp32.com
Adafruit Feather/Metro/Trinket: forums.adafruit.com
Particle Photon: community.particle.io

If you are experiencing trouble but not certain of the cause, or need help using this code, ask on the appropriate forum. This is not the place to ask for support or help, even directly related to this code. Only use this form you are certain you have discovered a defect in this code!

Please verify the problem occurs when using the very latest version, using the newest version of Arduino and any other related software.

----------------------------- Remove above -----------------------------

Description

Describe your problem.
I am using the time library and I am happy with it. There is just 1 thing. I am using your library and wanted to make corrections for the DST or no DST. I set the adjustment with adjustTime. It adds the value to sysTime. I want the adjustTime to be the same after a NTP time callback routine(setSyncProvider routine) . Can you make a global variable TimeOffset that will replace or will be set by adjustTime. I like it to be a global variable, so I can read back the offset elsewhere in the program. The time I will provide the Timelib will be Epoch time. The offset will be used by the return value of now(). return (time_t)(sysTime + TimeOffset);
This way the the sysTime is epoch and every second that passes adds 1 count to the sysTime EpochTime. By using an offset the time will be correct all the time. if it's between 2 and 3 AM during the last sunday of Octobre it's not clear if the time is during or after DST, but the epoch time never lies. Has no time correction. So please use a EpochTime sysTime with an offset as return value of now(). I am living in Europe and I think it has other DST rules as other places in the world. The Epoch DST routine must be done by the end user of this library (in the setSyncProvider callback which gets Epoch time). if DST changes I want to set the other offset with adjustTime or a global variable. sysTime will stay the same, but now() will change it value after the offset will change.
I wrote some DST routines that you can also use. works fine now, but I can't use adjustTime. Epoch time keeps returning ( setTime(TimeData.Epoch)); My TimeData struct contains 4 members (Epoch, DST, Offset and Actual). I can't use this to set time, because my setSyncProvider returns Epoch time and the previous adjustTime offset is disregarded. I also want to be able to get the sysTime Epoch time from the timelib. I am saving all my sensor data in my project onto an SD card with Epoch time codes.

Steps To Reproduce Problem

Use adjustTime and don't update the time (with setSyncProvider). adjustTime will probably work. If you use a setSyncProvider routine than it doesn't work anymore. the offset is forgotten. problems and solution in text above.
Please give detailed instructions needed for anyone to attempt to reproduce the problem.
problems and solution in text above.

Hardware & Software

I am using a nodeMCU at the moment.
Board
Shields / modules used WH24P (weather station)
Arduino IDE version 2.1.0
Teensyduino version (if using Teensy)
Version info & package name (from Tools > Boards > Board Manager)
Operating system & version Windows 11
Any other software or hardware?

Arduino Sketch

// Change the code below by your sketch (please try to give the smallest code which demonstrates the problem)
#include <Arduino.h>

// libraries: give links/details so anyone can compile your code for the same result

void setup() {
}

void loop() {
}

Errors or Incorrect Output

If you see any errors or incorrect output, please show it here. Please use copy & paste to give an exact copy of the message. Details matter, so please show (not merely describe) the actual message or error exactly as it appears.

commented

This is my DST routine with Epoch time from NTP server.

#define nextSunday(time) (previousSunday(time)+SECS_PER_WEEK)

bool CheckDSTTime(time_t EpochTime)
{
bool Result = false;
tmElements_t CurrentTime;
time_t DSTBegin, DSTEnd;
breakTime(EpochTime, CurrentTime);
tmElements_t DSTBeginDate = {0, 0, 0, 0, 24, 3, CurrentTime.Year};
tmElements_t DSTEndDate = {0, 0, 0, 0, 24, 10, CurrentTime.Year};
//Calculate Epoch Time when clock changes
//Starts a 2AM - a hour back from CET-1
//DST starts at 2.00h in the Netherlands. That's 1.00h Epoch time
DSTBegin = nextSunday(makeTime(DSTBeginDate)) + 3600UL;
//Calculate Epoch Time when clock changes
//Starts a 3AM - a hour from CET-1 - 1 hour of DST difference. That's 1.00h Epoch time
DSTEnd = nextSunday(makeTime(DSTEndDate)) + 3600UL;
Result = ((EpochTime >= DSTBegin) && (EpochTime < DSTEnd));
return (Result);
}

commented

I have solved the adjustTime problem and have rewritten now() (to return sysTime + timeOffset) and adjustTime(). I have added nowEpoch() and getAdjustTime(). I didn't rename anything (including adjustTime). The timeOffset will be kept in a long. So now the adjustTime offset will be remembered after a NTP/RTC callback. sysTime is Epoch time. I didn't change anything else. I don't want to use my own library, because I want it to be updateable. So please use it if it makes any sense. Both Time.cpp and TimeLib.h are below. Use them both to make it work.

This is Time.cpp

`/*
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
*/

#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

#include "TimeLib.h"

static tmElements_t tm; // 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

void refreshCache(time_t t) {
if (t != cacheTime) {
breakTime(t, tm);
cacheTime = t;
}
}

int hour() { // the hour now
return hour(now());
}

int hour(time_t t) { // the hour for the given time
refreshCache(t);
return tm.Hour;
}

int hourFormat12() { // the hour now in 12 hour format
return hourFormat12(now());
}

int hourFormat12(time_t t) { // the hour for the given time in 12 hour format
refreshCache(t);
if( tm.Hour == 0 )
return 12; // 12 midnight
else if( tm.Hour > 12)
return tm.Hour - 12 ;
else
return tm.Hour ;
}

uint8_t isAM() { // returns true if time now is AM
return !isPM(now());
}

uint8_t isAM(time_t t) { // returns true if given time is AM
return !isPM(t);
}

uint8_t isPM() { // returns true if PM
return isPM(now());
}

uint8_t isPM(time_t t) { // returns true if PM
return (hour(t) >= 12);
}

int minute() {
return minute(now());
}

int minute(time_t t) { // the minute for the given time
refreshCache(t);
return tm.Minute;
}

int second() {
return second(now());
}

int second(time_t t) { // the second for the given time
refreshCache(t);
return tm.Second;
}

int day(){
return(day(now()));
}

int day(time_t t) { // the day for the given time (0-6)
refreshCache(t);
return tm.Day;
}

int weekday() { // Sunday is day 1
return weekday(now());
}

int weekday(time_t t) {
refreshCache(t);
return tm.Wday;
}

int month(){
return month(now());
}

int month(time_t t) { // the month for the given time
refreshCache(t);
return tm.Month;
}

int year() { // as in Processing, the full four digit year: (2009, 2010 etc)
return year(now());
}

int year(time_t t) { // the year for the given time
refreshCache(t);
return tmYearToCalendar(tm.Year);
}

/============================================================================/
/* functions to convert to and from system time /
/
These are for interfacing with time services and are not normally needed in a sketch */

// leap year calculator expects year argument as years offset from 1970
#define LEAP_YEAR(Y) ( ((1970+(Y))>0) && !((1970+(Y))%4) && ( ((1970+(Y))%100) || !((1970+(Y))%400) ) )

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

void breakTime(time_t timeInput, 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 !!!

uint8_t year;
uint8_t month, monthLength;
uint32_t time;
unsigned long days;

time = (uint32_t)timeInput;
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
tm.Wday = ((time + 4) % 7) + 1; // Sunday is day 1

year = 0;
days = 0;
while((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) {
year++;
}
tm.Year = year; // year is offset from 1970

days -= LEAP_YEAR(year) ? 366 : 365;
time -= days; // now it is days in this year, starting at 0

days=0;
month=0;
monthLength=0;
for (month=0; month<12; month++) {
if (month==1) { // february
if (LEAP_YEAR(year)) {
monthLength=29;
} else {
monthLength=28;
}
} else {
monthLength = monthDays[month];
}

if (time >= monthLength) {
  time -= monthLength;
} else {
    break;
}

}
tm.Month = month + 1; // jan is month 1
tm.Day = time + 1; // day of month
}

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

int i;
uint32_t seconds;

// seconds from 1970 till 1 jan 00:00:00 of the given year
seconds= tm.Year*(SECS_PER_DAY * 365);
for (i = 0; i < tm.Year; i++) {
if (LEAP_YEAR(i)) {
seconds += SECS_PER_DAY; // add extra days for leap years
}
}

// add days for this year, months start from 1
for (i = 1; i < tm.Month; i++) {
if ( (i == 2) && LEAP_YEAR(tm.Year)) {
seconds += SECS_PER_DAY * 29;
} else {
seconds += SECS_PER_DAY * monthDays[i-1]; //monthDay array starts from 0
}
}
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;
}
/=====================================================/
/* Low level system time functions */

static uint32_t sysTime = 0;
static uint32_t prevMillis = 0;
static uint32_t nextSyncTime = 0;
static long timeOffset = 0;
static timeStatus_t Status = timeNotSet;

getExternalTime getTimePtr; // pointer to external sync function
//setExternalTime setTimePtr; // not used in this version

#ifdef TIME_DRIFT_INFO // define this to get drift data
time_t sysUnsyncedTime = 0; // the time sysTime unadjusted by sync
#endif

time_t now() {
// calculate number of seconds passed since last call to now()
while (millis() - prevMillis >= 1000) {
// millis() and prevMillis are both unsigned ints thus the subtraction will always be the absolute value of the difference
sysTime++;
prevMillis += 1000;
#ifdef TIME_DRIFT_INFO
sysUnsyncedTime++; // this can be compared to the synced time to measure long term drift
#endif
}
if (nextSyncTime <= sysTime) {
if (getTimePtr != 0) {
time_t t = getTimePtr();
if (t != 0) {
setTime(t);
} else {
nextSyncTime = sysTime + syncInterval;
Status = (Status == timeNotSet) ? timeNotSet : timeNeedsSync;
}
}
}
return (time_t)(sysTime + timeOffset);
}

time_t nowEpoch() {
// calculate number of seconds passed since last call to now()
while (millis() - prevMillis >= 1000) {
// millis() and prevMillis are both unsigned ints thus the subtraction will always be the absolute value of the difference
sysTime++;
prevMillis += 1000;
#ifdef TIME_DRIFT_INFO
sysUnsyncedTime++; // this can be compared to the synced time to measure long term drift
#endif
}
if (nextSyncTime <= sysTime) {
if (getTimePtr != 0) {
time_t t = getTimePtr();
if (t != 0) {
setTime(t);
}
else {
nextSyncTime = sysTime + syncInterval;
Status = (Status == timeNotSet) ? timeNotSet : timeNeedsSync;
}
}
}
return (time_t)sysTime;
}

void setTime(time_t t) {
#ifdef TIME_DRIFT_INFO
if(sysUnsyncedTime == 0)
sysUnsyncedTime = t; // store the time of the first call to set a valid Time
#endif

sysTime = (uint32_t)t;
nextSyncTime = (uint32_t)t + syncInterval;
Status = timeSet;
prevMillis = millis(); // restart counting from now (thanks to Korman for this fix)
}

void setTime(int hr,int min,int sec,int dy, int mnth, int yr){
// year can be given as full four digit year or two digts (2010 or 10 for 2010);
//it is converted to years since 1970
if( yr > 99)
yr = yr - 1970;
else
yr += 30;
tm.Year = yr;
tm.Month = mnth;
tm.Day = dy;
tm.Hour = hr;
tm.Minute = min;
tm.Second = sec;
setTime(makeTime(tm));
}

void adjustTime(long adjustment) {
//sysTime += adjustment;
timeOffset = adjustment;
}

long getAdjustTime() {
return (timeOffset);
}

// indicates if time has been set and recently synchronized
timeStatus_t timeStatus() {
now(); // required to actually update the status
return Status;
}

void setSyncProvider( getExternalTime getTimeFunction){
getTimePtr = getTimeFunction;
nextSyncTime = sysTime;
now(); // this will sync the clock
}

void setSyncInterval(time_t interval){ // set the number of seconds between re-sync
syncInterval = (uint32_t)interval;
nextSyncTime = sysTime + syncInterval;
}
`

This is TimeLib.h:
`
/*
time.h - low level time and date functions
*/

/*
July 3 2011 - fixed elapsedSecsThisWeek macro (thanks Vincent Valdy for this)
- fixed daysToTime_t macro (thanks maniacbug)
*/

#ifndef _Time_h
#ifdef __cplusplus
#define _Time_h

#include <inttypes.h>
#ifndef AVR
#include <sys/types.h> // for __time_t_defined, but avr libc lacks sys/types.h
#endif

#if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc
typedef unsigned long time_t;
#endif

// This ugly hack allows us to define C++ overloaded functions, when included
// from within an extern "C", as newlib's sys/stat.h does. Actually it is
// intended to include "time.h" from the C library (on ARM, but AVR does not
// have that file at all). On Mac and Windows, the compiler will find this
// "Time.h" instead of the C library "time.h", so we may cause other weird
// and unpredictable effects by conflicting with the C library header "time.h",
// but at least this hack lets us define C++ functions as intended. Hopefully
// nothing too terrible will result from overriding the C library header?!
extern "C++" {
typedef enum {timeNotSet, timeNeedsSync, timeSet
} timeStatus_t ;

typedef enum {
dowInvalid, dowSunday, dowMonday, dowTuesday, dowWednesday, dowThursday, dowFriday, dowSaturday
} timeDayOfWeek_t;

typedef enum {
tmSecond, tmMinute, tmHour, tmWday, tmDay,tmMonth, tmYear, tmNbrFields
} tmByteFields;

typedef struct {
uint8_t Second;
uint8_t Minute;
uint8_t Hour;
uint8_t Wday; // day of week, sunday is day 1
uint8_t Day;
uint8_t Month;
uint8_t Year; // offset from 1970;
} tmElements_t, TimeElements, *tmElementsPtr_t;

//convenience macros to convert to and from tm years
#define tmYearToCalendar(Y) ((Y) + 1970) // full four digit year
#define CalendarYrToTm(Y) ((Y) - 1970)
#define tmYearToY2k(Y) ((Y) - 30) // offset is from 2000
#define y2kYearToTm(Y) ((Y) + 30)

typedef time_t(*getExternalTime)();
//typedef void (*setExternalTime)(const time_t); // not used in this version

/==============================================================================/
/* Useful Constants */
#define SECS_PER_MIN ((time_t)(60UL))
#define SECS_PER_HOUR ((time_t)(3600UL))
#define SECS_PER_DAY ((time_t)(SECS_PER_HOUR * 24UL))
#define DAYS_PER_WEEK ((time_t)(7UL))
#define SECS_PER_WEEK ((time_t)(SECS_PER_DAY * DAYS_PER_WEEK))
#define SECS_PER_YEAR ((time_t)(SECS_PER_DAY * 365UL)) // TODO: ought to handle leap years
#define SECS_YR_2000 ((time_t)(946684800UL)) // the time at the start of y2k

/* Useful Macros for getting elapsed time */
#define numberOfSeconds(time) ((time) % SECS_PER_MIN)
#define numberOfMinutes(time) (((time) / SECS_PER_MIN) % SECS_PER_MIN)
#define numberOfHours(time) (((time) % SECS_PER_DAY) / SECS_PER_HOUR)
#define dayOfWeek(time) ((((time) / SECS_PER_DAY + 4) % DAYS_PER_WEEK)+1) // 1 = Sunday
#define elapsedDays(time) ((time) / SECS_PER_DAY) // this is number of days since Jan 1 1970
#define elapsedSecsToday(time) ((time) % SECS_PER_DAY) // the number of seconds since last midnight
// The following macros are used in calculating alarms and assume the clock is set to a date later than Jan 1 1971
// Always set the correct time before setting alarms
#define previousMidnight(time) (((time) / SECS_PER_DAY) * SECS_PER_DAY) // time at the start of the given day
#define nextMidnight(time) (previousMidnight(time) + SECS_PER_DAY) // time at the end of the given day
#define elapsedSecsThisWeek(time) (elapsedSecsToday(time) + ((dayOfWeek(time)-1) * SECS_PER_DAY)) // note that week starts on day 1
#define previousSunday(time) ((time) - elapsedSecsThisWeek(time)) // time at the start of the week for the given time
#define nextSunday(time) (previousSunday(time)+SECS_PER_WEEK) // time at the end of the week for the given time

/* Useful Macros for converting elapsed time to a time_t */
#define minutesToTime_t ((M)) ( (M) * SECS_PER_MIN)
#define hoursToTime_t ((H)) ( (H) * SECS_PER_HOUR)
#define daysToTime_t ((D)) ( (D) * SECS_PER_DAY) // fixed on Jul 22 2011
#define weeksToTime_t ((W)) ( (W) * SECS_PER_WEEK)

/============================================================================/
/* time and date functions */
int hour(); // the hour now
int hour(time_t t); // the hour for the given time
int hourFormat12(); // the hour now in 12 hour format
int hourFormat12(time_t t); // the hour for the given time in 12 hour format
uint8_t isAM(); // returns true if time now is AM
uint8_t isAM(time_t t); // returns true the given time is AM
uint8_t isPM(); // returns true if time now is PM
uint8_t isPM(time_t t); // returns true the given time is PM
int minute(); // the minute now
int minute(time_t t); // the minute for the given time
int second(); // the second now
int second(time_t t); // the second for the given time
int day(); // the day now
int day(time_t t); // the day for the given time
int weekday(); // the weekday now (Sunday is day 1)
int weekday(time_t t); // the weekday for the given time
int month(); // the month now (Jan is month 1)
int month(time_t t); // the month for the given time
int year(); // the full four digit year: (2009, 2010 etc)
int year(time_t t); // the year for the given time

time_t now(); // return the current time as seconds since Jan 1 1970 with timeOffset
time_t nowEpoch(); // return the current time as seconds since Jan 1 1970
void setTime(time_t t);
void setTime(int hr,int min,int sec,int day, int month, int yr);
void adjustTime(long adjustment);
long getAdjustTime();

/* date strings /
#define dt_MAX_STRING_LEN 9 // length of longest date string (excluding terminating null)
char
monthStr(uint8_t month);
char* dayStr(uint8_t day);
char* monthShortStr(uint8_t month);
char* dayShortStr(uint8_t day);

/* time sync functions */
timeStatus_t timeStatus(); // indicates if time has been set and recently synchronized
void setSyncProvider( getExternalTime getTimeFunction); // identify the external time provider
void setSyncInterval(time_t interval); // set the number of seconds between re-sync

/* low level functions to convert to and from system time */
void breakTime(time_t time, tmElements_t &tm); // break time_t into elements
time_t makeTime(const tmElements_t &tm); // convert time elements into time_t

} // extern "C++"
#endif // __cplusplus
#endif /* _Time_h */

`

could you please make a pool request