-
Notifications
You must be signed in to change notification settings - Fork 670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Epoch and Time offset #180
Comments
This is my DST routine with Epoch time from NTP server. #define nextSunday(time) (previousSunday(time)+SECS_PER_WEEK) bool CheckDSTTime(time_t EpochTime) |
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 `/* This library is free software; you can redistribute it and/or This library is distributed in the hope that it will be useful, You should have received a copy of the GNU Lesser General Public 1.0 6 Jan 2010 - initial release #if ARDUINO >= 100 #include "TimeLib.h" static tmElements_t tm; // a cache of time elements void refreshCache(time_t t) { 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 if given time is AM uint8_t isPM() { // returns true if PM uint8_t isPM(time_t t) { // returns true if PM int minute() { int minute(time_t t) { // the minute for the given time int second() { int second(time_t t) { // the second for the given time int day(){ int day(time_t t) { // the day for the given time (0-6) int weekday() { // Sunday is day 1 int weekday(time_t t) { int month(){ int month(time_t t) { // the month for the given time int year() { // as in Processing, the full four digit year: (2009, 2010 etc) int year(time_t t) { // the year for the given time /============================================================================/ // leap year calculator expects year argument as years offset from 1970 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){ uint8_t year; time = (uint32_t)timeInput; year = 0; days -= LEAP_YEAR(year) ? 366 : 365; days=0;
} time_t makeTime(const tmElements_t &tm){ int i; // seconds from 1970 till 1 jan 00:00:00 of the given year // add days for this year, months start from 1 static uint32_t sysTime = 0; getExternalTime getTimePtr; // pointer to external sync function #ifdef TIME_DRIFT_INFO // define this to get drift data time_t now() { time_t nowEpoch() { void setTime(time_t t) { sysTime = (uint32_t)t; void setTime(int hr,int min,int sec,int dy, int mnth, int yr){ void adjustTime(long adjustment) { long getAdjustTime() { // indicates if time has been set and recently synchronized void setSyncProvider( getExternalTime getTimeFunction){ void setSyncInterval(time_t interval){ // set the number of seconds between re-sync This is TimeLib.h: /* #ifndef _Time_h #include <inttypes.h> #if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc // This ugly hack allows us to define C++ overloaded functions, when included typedef enum { typedef enum { typedef struct { //convenience macros to convert to and from tm years typedef time_t(*getExternalTime)(); /==============================================================================/ /* Useful Macros for getting elapsed time */ /* Useful Macros for converting elapsed time to a time_t */ /============================================================================/ time_t now(); // return the current time as seconds since Jan 1 1970 with timeOffset /* date strings / /* time sync functions */ /* low level functions to convert to and from system time */ } // extern "C++" ` |
could you please make a pool request |
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
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.
The text was updated successfully, but these errors were encountered: