diff --git a/DateStrings.cpp b/DateStrings.cpp index 10181f6..3f0f007 100644 --- a/DateStrings.cpp +++ b/DateStrings.cpp @@ -17,8 +17,10 @@ #define PGM_P const char * #define pgm_read_byte(addr) (*(const unsigned char *)(addr)) #define pgm_read_word(addr) (*(const unsigned char **)(addr)) +#ifndef strcpy_P #define strcpy_P(dest, src) strcpy((dest), (src)) #endif +#endif #include // for strcpy_P or strcpy #include "microTimeLib.h" diff --git a/Readme.md b/Readme.md index 86add2c..af0ee32 100644 --- a/Readme.md +++ b/Readme.md @@ -99,10 +99,10 @@ illustrating how the library can be used with various time sources: if it is running and connected to the Arduino serial port. - `TimeSerialDateStrings.pde` adds day and month name strings to the sketch above - Short (3 character) and long strings are available to print the days of + Short (3 characters) and long strings are available to print the days of the week and names of the months. -- `TimeRTC` uses a DS1307 real time clock to provide time synchronization. +- `TimeRTC` uses a DS1307 real-time clock to provide time synchronization. A basic RTC library named DS1307RTC is included in the download. To run this sketch the DS1307RTC library must be installed. diff --git a/docs/issue_template.md b/docs/issue_template.md new file mode 100644 index 0000000..0610992 --- /dev/null +++ b/docs/issue_template.md @@ -0,0 +1,64 @@ +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. + + + +### Steps To Reproduce Problem + +Please give detailed instructions needed for anyone to attempt to reproduce the problem. + + + +### Hardware & Software + +Board +Shields / modules used +Arduino IDE version +Teensyduino version (if using Teensy) +Version info & package name (from Tools > Boards > Board Manager) +Operating system & version +Any other software or hardware? + + +### Arduino Sketch + +```cpp +// Change the code below by your sketch (please try to give the smallest code which demonstrates the problem) +#include + +// 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. + + diff --git a/microTime.cpp b/microTime.cpp index 0535862..00e806e 100644 --- a/microTime.cpp +++ b/microTime.cpp @@ -132,7 +132,7 @@ int day(time_t t) { // the day for the given time (0-6) } int weekday() { // Sunday is day 1 - return weekday(now()); + return weekday(now()); } int weekday(time_t t) { @@ -164,26 +164,24 @@ int year(time_t t) { // the year for the given time // leap year calulator expects year argument as years offset from 1970 #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)) -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){ +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 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 !!! - uint8_t year; - uint8_t month, monthLength; - uint32_t time; - unsigned long days; + uint8_t period; + time_t length; - 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 + 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. @@ -199,38 +197,26 @@ void breakTime(time_t timeInput, tmElements_t &tm){ tm.Wday = DAYS_TO_WDAY(time); - year = 0; - days = 0; - while((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) { - year++; + period = 0; + while (time >= (length = daysInYear(period))) + { + time -= length; + period++; } - 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.Year = period; // year is offset from 1970 + // time is now days since 1 Jan of the year + + bool leap_year = LEAP_YEAR(period); + period = 0; + while (period < 12 && time >= (length = monthDays[period] + (leap_year && period==1))) + { + time -= length; + period++; } - tm.Month = month + 1; // jan is month 1 - tm.Day = time + 1; // day of month + tm.Month = period + 1; // jan is month 1 + // time is now days since the 1st day of the month + + tm.Day = time + 1; // day of month } time_t makeTime(const tmElements_t &tm){ @@ -242,7 +228,7 @@ time_t makeTime(const tmElements_t &tm){ uint32_t seconds; // seconds from 1970 till 1 jan 00:00:00 of the given year - seconds= tm.Year*(SECS_PER_DAY * 365); + seconds = SECS_PER_DAY * (365 * tm.Year); for (i = 0; i < tm.Year; i++) { if (LEAP_YEAR(i)) { seconds += SECS_PER_DAY; // add extra days for leap years @@ -266,9 +252,9 @@ time_t makeTime(const tmElements_t &tm){ /*=====================================================*/ /* Low level system time functions */ -static uint32_t sysTime = 0; +static time_t sysTime = 0; static uint32_t prevMicros = 0; -static uint32_t nextSyncTime = 0; +static time_t nextSyncTime = 0; static timeStatus_t Status = timeNotSet; getExternalTime getTimePtr; // pointer to external sync function @@ -316,7 +302,7 @@ time_t now(uint32_t& sysTimeMicros) { } } } - return (time_t)sysTime; + return sysTime; } void setTime(time_t t) { @@ -325,8 +311,8 @@ void setTime(time_t t) { sysUnsyncedTime = t; // store the time of the first call to set a valid Time #endif - sysTime = (uint32_t)t; - nextSyncTime = (uint32_t)t + syncInterval; + sysTime = t; + nextSyncTime = t + (time_t) syncInterval; Status = timeSet; #ifndef usePPS prevMicros = micros(); // restart counting from now (thanks to Korman for this fix) @@ -361,7 +347,7 @@ timeStatus_t timeStatus() { return Status; } -void setSyncProvider( getExternalTime getTimeFunction){ +void setSyncProvider(getExternalTime getTimeFunction){ getTimePtr = getTimeFunction; nextSyncTime = sysTime; now(); // this will sync the clock