From e1b239bd37584e12585fe19f8cb24ffaae41b506 Mon Sep 17 00:00:00 2001 From: Irsyad Date: Sun, 4 Jun 2023 17:38:34 +0800 Subject: [PATCH] adds yearday functionality --- Time.cpp | 25 ++++++++++++++++++++++++- TimeLib.h | 2 ++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Time.cpp b/Time.cpp index 0dcb29f..3dfd8eb 100644 --- a/Time.cpp +++ b/Time.cpp @@ -120,7 +120,30 @@ int weekday(time_t t) { refreshCache(t); return tm.Wday; } - + +int yearday(time_t t) { + tmElements_t tm; + breakTime(t, tm); + uint32_t y = tm.Year; + int m = tm.Month; + int d = tm.Day; + + #define LEAP_YEAR(Y) ( ((0+(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 + + uint16_t days = d; + for (uint8_t i = 1; i < m; i++) + days += monthDays[i - 1]; + if (m > 2 && LEAP_YEAR(y)) + days++; + return days; +} + +int yearday() { + return yearday(now()); +} + int month(){ return month(now()); } diff --git a/TimeLib.h b/TimeLib.h index b587046..87ba61e 100644 --- a/TimeLib.h +++ b/TimeLib.h @@ -112,6 +112,8 @@ 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 yearday(); // the yearday now +int yearday(time_t t); // the yearday 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)