Skip to content
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

adds yearday functionality #181

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 24 additions & 1 deletion Time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
2 changes: 2 additions & 0 deletions TimeLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down