format check in time lib

pull/293/head
Peter Buchegger 2023-05-17 21:34:41 +02:00
rodzic 74e01a76a7
commit a0ce4608ae
4 zmienionych plików z 322 dodań i 330 usunięć

Wyświetl plik

@ -47,7 +47,7 @@ jobs:
- 'lib/NTPClient' - 'lib/NTPClient'
- 'lib/PowerManagement' - 'lib/PowerManagement'
- 'lib/System' - 'lib/System'
#- 'lib/TimeLib' - 'lib/TimeLib'
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v3 uses: actions/checkout@v3

Wyświetl plik

@ -57,12 +57,12 @@ int hourFormat12() { // the hour now in 12 hour format
int hourFormat12(time_t t) { // the hour for the given time in 12 hour format int hourFormat12(time_t t) { // the hour for the given time in 12 hour format
refreshCache(t); refreshCache(t);
if( tm.Hour == 0 ) if (tm.Hour == 0)
return 12; // 12 midnight return 12; // 12 midnight
else if( tm.Hour > 12) else if (tm.Hour > 12)
return tm.Hour - 12 ; return tm.Hour - 12;
else else
return tm.Hour ; return tm.Hour;
} }
uint8_t isAM() { // returns true if time now is AM uint8_t isAM() { // returns true if time now is AM
@ -99,8 +99,8 @@ int second(time_t t) { // the second for the given time
return tm.Second; return tm.Second;
} }
int day(){ int day() {
return(day(now())); return (day(now()));
} }
int day(time_t t) { // the day for the given time (0-6) int day(time_t t) { // the day for the given time (0-6)
@ -117,7 +117,7 @@ int weekday(time_t t) {
return tm.Wday; return tm.Wday;
} }
int month(){ int month() {
return month(now()); return month(now());
} }
@ -135,13 +135,11 @@ int year(time_t t) { // the year for the given time
return tmYearToCalendar(tm.Year); return tmYearToCalendar(tm.Year);
} }
const String timeString() const String timeString() {
{
return timeString(now()); return timeString(now());
} }
const String timeString(time_t t) const String timeString(time_t t) {
{
char line[30]; char line[30];
sprintf(line, "%02d:%02d:%02d", hour(t), minute(t), second(t)); sprintf(line, "%02d:%02d:%02d", hour(t), minute(t), second(t));
return String(line); return String(line);
@ -152,14 +150,14 @@ const String timeString(time_t t)
/* These are for interfacing with time services and are not normally needed in a sketch */ /* 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 // 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) ) ) #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 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){ void breakTime(time_t timeInput, tmElements_t &tm) {
// break the given time_t into time components // break the given time_t into time components
// this is a more compact version of the C library localtime function // this is a more compact version of the C library localtime function
// note that year is offset from 1970 !!! // note that year is offset from 1970 !!!
uint8_t year; uint8_t year;
uint8_t month, monthLength; uint8_t month, monthLength;
@ -177,7 +175,7 @@ void breakTime(time_t timeInput, tmElements_t &tm){
year = 0; year = 0;
days = 0; days = 0;
while((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) { while ((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) {
year++; year++;
} }
tm.Year = year; // year is offset from 1970 tm.Year = year; // year is offset from 1970
@ -185,15 +183,15 @@ void breakTime(time_t timeInput, tmElements_t &tm){
days -= LEAP_YEAR(year) ? 366 : 365; days -= LEAP_YEAR(year) ? 366 : 365;
time -= days; // now it is days in this year, starting at 0 time -= days; // now it is days in this year, starting at 0
days=0; days = 0;
month=0; month = 0;
monthLength=0; monthLength = 0;
for (month=0; month<12; month++) { for (month = 0; month < 12; month++) {
if (month==1) { // february if (month == 1) { // february
if (LEAP_YEAR(year)) { if (LEAP_YEAR(year)) {
monthLength=29; monthLength = 29;
} else { } else {
monthLength=28; monthLength = 28;
} }
} else { } else {
monthLength = monthDays[month]; monthLength = monthDays[month];
@ -209,16 +207,16 @@ void breakTime(time_t timeInput, tmElements_t &tm){
tm.Day = time + 1; // day of month tm.Day = time + 1; // day of month
} }
time_t makeTime(const tmElements_t &tm){ time_t makeTime(const tmElements_t &tm) {
// assemble time elements into time_t // assemble time elements into time_t
// note year argument is offset from 1970 (see macros in time.h to convert to other formats) // 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 // previous version used full four digit year (or digits since 2000),i.e. 2009 was 2009 or 9
int i; int i;
uint32_t seconds; uint32_t seconds;
// seconds from 1970 till 1 jan 00:00:00 of the given year // seconds from 1970 till 1 jan 00:00:00 of the given year
seconds= tm.Year*(SECS_PER_DAY * 365); seconds = tm.Year * (SECS_PER_DAY * 365);
for (i = 0; i < tm.Year; i++) { for (i = 0; i < tm.Year; i++) {
if (LEAP_YEAR(i)) { if (LEAP_YEAR(i)) {
seconds += SECS_PER_DAY; // add extra days for leap years seconds += SECS_PER_DAY; // add extra days for leap years
@ -227,16 +225,16 @@ time_t makeTime(const tmElements_t &tm){
// add days for this year, months start from 1 // add days for this year, months start from 1
for (i = 1; i < tm.Month; i++) { for (i = 1; i < tm.Month; i++) {
if ( (i == 2) && LEAP_YEAR(tm.Year)) { if ((i == 2) && LEAP_YEAR(tm.Year)) {
seconds += SECS_PER_DAY * 29; seconds += SECS_PER_DAY * 29;
} else { } else {
seconds += SECS_PER_DAY * monthDays[i-1]; //monthDay array starts from 0 seconds += SECS_PER_DAY * monthDays[i - 1]; // monthDay array starts from 0
} }
} }
seconds+= (tm.Day-1) * SECS_PER_DAY; seconds += (tm.Day - 1) * SECS_PER_DAY;
seconds+= tm.Hour * SECS_PER_HOUR; seconds += tm.Hour * SECS_PER_HOUR;
seconds+= tm.Minute * SECS_PER_MIN; seconds += tm.Minute * SECS_PER_MIN;
seconds+= tm.Second; seconds += tm.Second;
return (time_t)seconds; return (time_t)seconds;
} }
/*=====================================================*/ /*=====================================================*/
@ -248,13 +246,12 @@ static uint32_t nextSyncTime = 0;
static timeStatus_t Status = timeNotSet; static timeStatus_t Status = timeNotSet;
getExternalTime getTimePtr; // pointer to external sync function getExternalTime getTimePtr; // pointer to external sync function
//setExternalTime setTimePtr; // not used in this version // setExternalTime setTimePtr; // not used in this version
#ifdef TIME_DRIFT_INFO // define this to get drift data #ifdef TIME_DRIFT_INFO // define this to get drift data
time_t sysUnsyncedTime = 0; // the time sysTime unadjusted by sync time_t sysUnsyncedTime = 0; // the time sysTime unadjusted by sync
#endif #endif
time_t now() { time_t now() {
// calculate number of seconds passed since last call to now() // calculate number of seconds passed since last call to now()
while (millis() - prevMillis >= 1000) { while (millis() - prevMillis >= 1000) {
@ -281,7 +278,7 @@ time_t now() {
void setTime(time_t t) { void setTime(time_t t) {
#ifdef TIME_DRIFT_INFO #ifdef TIME_DRIFT_INFO
if(sysUnsyncedTime == 0) if (sysUnsyncedTime == 0)
sysUnsyncedTime = t; // store the time of the first call to set a valid Time sysUnsyncedTime = t; // store the time of the first call to set a valid Time
#endif #endif
@ -291,10 +288,10 @@ void setTime(time_t t) {
prevMillis = millis(); // restart counting from now (thanks to Korman for this fix) 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){ 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); // year can be given as full four digit year or two digts (2010 or 10 for 2010);
//it is converted to years since 1970 // it is converted to years since 1970
if( yr > 99) if (yr > 99)
yr = yr - 1970; yr = yr - 1970;
else else
yr += 30; yr += 30;
@ -317,13 +314,13 @@ timeStatus_t timeStatus() {
return Status; return Status;
} }
void setSyncProvider( getExternalTime getTimeFunction){ void setSyncProvider(getExternalTime getTimeFunction) {
getTimePtr = getTimeFunction; getTimePtr = getTimeFunction;
nextSyncTime = sysTime; nextSyncTime = sysTime;
now(); // this will sync the clock now(); // this will sync the clock
} }
void setSyncInterval(time_t interval){ // set the number of seconds between re-sync void setSyncInterval(time_t interval) { // set the number of seconds between re-sync
syncInterval = (uint32_t)interval; syncInterval = (uint32_t)interval;
nextSyncTime = sysTime + syncInterval; nextSyncTime = sysTime + syncInterval;
} }

Wyświetl plik

@ -10,18 +10,35 @@
#ifndef _Time_h #ifndef _Time_h
#define _Time_h #define _Time_h
#include <inttypes.h>
#include <Arduino.h> #include <Arduino.h>
#include <inttypes.h>
typedef enum {timeNotSet, timeNeedsSync, timeSet
} timeStatus_t ;
typedef enum { typedef enum {
dowInvalid, dowSunday, dowMonday, dowTuesday, dowWednesday, dowThursday, dowFriday, dowSaturday timeNotSet,
timeNeedsSync,
timeSet
} timeStatus_t;
typedef enum {
dowInvalid,
dowSunday,
dowMonday,
dowTuesday,
dowWednesday,
dowThursday,
dowFriday,
dowSaturday
} timeDayOfWeek_t; } timeDayOfWeek_t;
typedef enum { typedef enum {
tmSecond, tmMinute, tmHour, tmWday, tmDay,tmMonth, tmYear, tmNbrFields tmSecond,
tmMinute,
tmHour,
tmWday,
tmDay,
tmMonth,
tmYear,
tmNbrFields
} tmByteFields; } tmByteFields;
typedef struct { typedef struct {
@ -34,15 +51,14 @@ typedef struct {
uint8_t Year; // offset from 1970; uint8_t Year; // offset from 1970;
} tmElements_t, TimeElements, *tmElementsPtr_t; } tmElements_t, TimeElements, *tmElementsPtr_t;
//convenience macros to convert to and from tm years // convenience macros to convert to and from tm years
#define tmYearToCalendar(Y) ((Y) + 1970) // full four digit year #define tmYearToCalendar(Y) ((Y) + 1970) // full four digit year
#define CalendarYrToTm(Y) ((Y) - 1970) #define CalendarYrToTm(Y) ((Y)-1970)
#define tmYearToY2k(Y) ((Y) - 30) // offset is from 2000 #define tmYearToY2k(Y) ((Y)-30) // offset is from 2000
#define y2kYearToTm(Y) ((Y) + 30) #define y2kYearToTm(Y) ((Y) + 30)
typedef time_t(*getExternalTime)(); typedef time_t (*getExternalTime)();
//typedef void (*setExternalTime)(const time_t); // not used in this version // typedef void (*setExternalTime)(const time_t); // not used in this version
/*==============================================================================*/ /*==============================================================================*/
/* Useful Constants */ /* Useful Constants */
@ -58,23 +74,22 @@ typedef time_t(*getExternalTime)();
#define numberOfSeconds(_time_) ((_time_) % SECS_PER_MIN) #define numberOfSeconds(_time_) ((_time_) % SECS_PER_MIN)
#define numberOfMinutes(_time_) (((_time_) / SECS_PER_MIN) % SECS_PER_MIN) #define numberOfMinutes(_time_) (((_time_) / SECS_PER_MIN) % SECS_PER_MIN)
#define numberOfHours(_time_) (((_time_) % SECS_PER_DAY) / SECS_PER_HOUR) #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 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 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 #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 // 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 // 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 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 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 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 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 #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 */ /* Useful Macros for converting elapsed time to a time_t */
#define minutesToTime_t(M) ((M) * SECS_PER_MIN) #define minutesToTime_t(M) ((M)*SECS_PER_MIN)
#define hoursToTime_t(H) ((H) * SECS_PER_HOUR) #define hoursToTime_t(H) ((H)*SECS_PER_HOUR)
#define daysToTime_t(D) ((D) * SECS_PER_DAY) // fixed on Jul 22 2011 #define daysToTime_t(D) ((D)*SECS_PER_DAY) // fixed on Jul 22 2011
#define weeksToTime_t(W) ((W) * SECS_PER_WEEK) #define weeksToTime_t(W) ((W)*SECS_PER_WEEK)
/*============================================================================*/ /*============================================================================*/
/* time and date functions */ /* time and date functions */
@ -104,7 +119,7 @@ const String timeString(time_t t);
time_t now(); // return the current time as seconds since Jan 1 1970 time_t now(); // return the current time as seconds since Jan 1 1970
void setTime(time_t t); void setTime(time_t t);
void setTime(int hr,int min,int sec,int day, int month, int yr); void setTime(int hr, int min, int sec, int day, int month, int yr);
void adjustTime(long adjustment); void adjustTime(long adjustment);
/* date strings */ /* date strings */
@ -116,7 +131,7 @@ const String dayShortStr(uint8_t day);
/* time sync functions */ /* time sync functions */
timeStatus_t timeStatus(); // indicates if time has been set and recently synchronized timeStatus_t timeStatus(); // indicates if time has been set and recently synchronized
void setSyncProvider( getExternalTime getTimeFunction); // identify the external time provider void setSyncProvider(getExternalTime getTimeFunction); // identify the external time provider
void setSyncInterval(time_t interval); // set the number of seconds between re-sync void setSyncInterval(time_t interval); // set the number of seconds between re-sync
/* low level functions to convert to and from system time */ /* low level functions to convert to and from system time */
@ -124,4 +139,3 @@ 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 time_t makeTime(const tmElements_t &tm); // convert time elements into time_t
#endif /* _Time_h */ #endif /* _Time_h */

Wyświetl plik

@ -9,48 +9,29 @@
* *
*/ */
#include <Arduino.h>
#include "TimeLib.h" #include "TimeLib.h"
#include <Arduino.h>
const String monthNames[] = const String monthNames[] = {"Error", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
{
"Error", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
};
const String monthStr(uint8_t month) const String monthStr(uint8_t month) {
{
return monthNames[month]; return monthNames[month];
} }
const String monthShortNames[] = {"Err", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
const String monthShortNames[] = const String monthShortStr(uint8_t month) {
{
"Err", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
const String monthShortStr(uint8_t month)
{
return monthShortNames[month]; return monthShortNames[month];
} }
const String dayNames[] = {"Err", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const String dayNames[] = const String dayStr(uint8_t day) {
{
"Err", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};
const String dayStr(uint8_t day)
{
return dayNames[day]; return dayNames[day];
} }
const String dayShortNames[] = {"Err", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
const String dayShortNames[] = const String dayShortStr(uint8_t day) {
{
"Err", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
const String dayShortStr(uint8_t day)
{
return dayShortNames[day]; return dayShortNames[day];
} }