Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DateStrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <string.h> // for strcpy_P or strcpy
#include "microTimeLib.h"

Expand Down
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
64 changes: 64 additions & 0 deletions docs/issue_template.md
Original file line number Diff line number Diff line change
@@ -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 <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.


80 changes: 33 additions & 47 deletions microTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand All @@ -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){
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -316,7 +302,7 @@ time_t now(uint32_t& sysTimeMicros) {
}
}
}
return (time_t)sysTime;
return sysTime;
}

void setTime(time_t t) {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down