Last active
July 18, 2024 22:00
-
-
Save jrleeman/3b7c10712112e49d8607 to your computer and use it in GitHub Desktop.
Arduino Day of Year Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
toDayOfYear | |
This sketch demonstrates a function to convert the year, month, day | |
format to the day of the year. Leap years are corrected. Unit tests | |
include leap year and regular year tests for every month as well as | |
corner case and error case testing. | |
Created 30 March 2015 | |
Modified 14 April 2015 | |
J.R. Leeman | |
www.github.com/jrleeman | |
"THE BEER-WARE LICENSE" (Revision 42): | |
<[email protected]> wrote this file. As long as you retain this notice you | |
can do whatever you want with this stuff. If we meet some day, and you think | |
this stuff is worth it, you can buy me a beer in return. | |
*/ | |
void setup() { | |
Serial.begin(115200); | |
} | |
void loop() { | |
int result = 0; | |
Serial.print("\n\n"); | |
Serial.println("TEST FOR NON-LEAP YEAR"); | |
Serial.println("RESULT, EXPECTED VALUE"); | |
result = calculateDayOfYear(20,1,2015); | |
Serial.print(result); | |
Serial.println(", 20"); | |
result = calculateDayOfYear(1,2,2015); | |
Serial.print(result); | |
Serial.println(", 32"); | |
result = calculateDayOfYear(31,3,2015); | |
Serial.print(result); | |
Serial.println(", 90"); | |
result = calculateDayOfYear(30,4,2015); | |
Serial.print(result); | |
Serial.println(", 120"); | |
result = calculateDayOfYear(5,5,2015); | |
Serial.print(result); | |
Serial.println(", 125"); | |
result = calculateDayOfYear(8,6,2015); | |
Serial.print(result); | |
Serial.println(", 159"); | |
result = calculateDayOfYear(10,7,2015); | |
Serial.print(result); | |
Serial.println(", 191"); | |
result = calculateDayOfYear(31,8,2015); | |
Serial.print(result); | |
Serial.println(", 243"); | |
result = calculateDayOfYear(29,9,2015); | |
Serial.print(result); | |
Serial.println(", 272"); | |
result = calculateDayOfYear(5,10,2015); | |
Serial.print(result); | |
Serial.println(", 278"); | |
result = calculateDayOfYear(30,11,2015); | |
Serial.print(result); | |
Serial.println(", 334"); | |
result = calculateDayOfYear(29,12,2015); | |
Serial.print(result); | |
Serial.println(", 363"); | |
Serial.print("\n\n"); | |
Serial.println("TEST FOR LEAP YEAR"); | |
Serial.println("RESULT, EXPECTED VALUE"); | |
result = calculateDayOfYear(20,1,2016); | |
Serial.print(result); | |
Serial.println(", 20"); | |
result = calculateDayOfYear(1,2,2016); | |
Serial.print(result); | |
Serial.println(", 32"); | |
result = calculateDayOfYear(31,3,2016); | |
Serial.print(result); | |
Serial.println(", 91"); | |
result = calculateDayOfYear(30,4,2016); | |
Serial.print(result); | |
Serial.println(", 121"); | |
result = calculateDayOfYear(5,5,2016); | |
Serial.print(result); | |
Serial.println(", 126"); | |
result = calculateDayOfYear(8,6,2016); | |
Serial.print(result); | |
Serial.println(", 160"); | |
result = calculateDayOfYear(10,7,2016); | |
Serial.print(result); | |
Serial.println(", 192"); | |
result = calculateDayOfYear(31,8,2016); | |
Serial.print(result); | |
Serial.println(", 244"); | |
result = calculateDayOfYear(29,9,2016); | |
Serial.print(result); | |
Serial.println(", 273"); | |
result = calculateDayOfYear(5,10,2016); | |
Serial.print(result); | |
Serial.println(", 279"); | |
result = calculateDayOfYear(30,11,2016); | |
Serial.print(result); | |
Serial.println(", 335"); | |
result = calculateDayOfYear(29,12,2016); | |
Serial.print(result); | |
Serial.println(", 364"); | |
Serial.print("\n\n"); | |
Serial.println("TEST FOR CORNER/ERROR CASES"); | |
Serial.println("RESULT, EXPECTED VALUE"); | |
result = calculateDayOfYear(0,1,2016); | |
Serial.print(result); | |
Serial.println(", 999"); | |
result = calculateDayOfYear(32,8,2016); | |
Serial.print(result); | |
Serial.println(", 999"); | |
result = calculateDayOfYear(29,2,2016); | |
Serial.print(result); | |
Serial.println(", 60"); | |
result = calculateDayOfYear(29,2,2015); | |
Serial.print(result); | |
Serial.println(", 999"); | |
result = calculateDayOfYear(31,12,2015); | |
Serial.print(result); | |
Serial.println(", 365"); | |
result = calculateDayOfYear(31,12,2016); | |
Serial.print(result); | |
Serial.println(", 366"); | |
result = calculateDayOfYear(32,12,2015); | |
Serial.print(result); | |
Serial.println(", 999"); | |
result = calculateDayOfYear(32,12,2016); | |
Serial.print(result); | |
Serial.println(", 999"); | |
Serial.print("\n\n"); | |
Serial.println("VERIFY LEAP YEAR CALCULATION"); | |
Serial.println("RESULT, EXPECTED VALUE"); | |
result = calculateDayOfYear(29,2,1700); | |
Serial.print(result); | |
Serial.println(", 999"); | |
result = calculateDayOfYear(29,2,1800); | |
Serial.print(result); | |
Serial.println(", 999"); | |
result = calculateDayOfYear(29,2,1900); | |
Serial.print(result); | |
Serial.println(", 999"); | |
result = calculateDayOfYear(29,2,2100); | |
Serial.print(result); | |
Serial.println(", 999"); | |
result = calculateDayOfYear(29,2,2200); | |
Serial.print(result); | |
Serial.println(", 999"); | |
result = calculateDayOfYear(29,2,2300); | |
Serial.print(result); | |
Serial.println(", 999"); | |
result = calculateDayOfYear(29,2,2500); | |
Serial.print(result); | |
Serial.println(", 999"); | |
result = calculateDayOfYear(29,2,2400); | |
Serial.print(result); | |
Serial.println(", 60"); | |
result = calculateDayOfYear(29,2,1600); | |
Serial.print(result); | |
Serial.println(", 60"); | |
result = calculateDayOfYear(29,2,2000); | |
Serial.print(result); | |
Serial.println(", 60"); | |
Serial.println("------------------ END TESTS ------------------"); | |
delay(10000); | |
} | |
int calculateDayOfYear(int day, int month, int year) { | |
// Given a day, month, and year (4 digit), returns | |
// the day of year. Errors return 999. | |
int daysInMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31}; | |
// Verify we got a 4-digit year | |
if (year < 1000) { | |
return 999; | |
} | |
// Check if it is a leap year, this is confusing business | |
// See: https://support.microsoft.com/en-us/kb/214019 | |
if (year%4 == 0) { | |
if (year%100 != 0) { | |
daysInMonth[1] = 29; | |
} | |
else { | |
if (year%400 == 0) { | |
daysInMonth[1] = 29; | |
} | |
} | |
} | |
// Make sure we are on a valid day of the month | |
if (day < 1) | |
{ | |
return 999; | |
} else if (day > daysInMonth[month-1]) { | |
return 999; | |
} | |
int doy = 0; | |
for (int i = 0; i < month - 1; i++) { | |
doy += daysInMonth[i]; | |
} | |
doy += day; | |
return doy; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I love the function you to get the day of the year but I ran into one issue. When using the RTCLib and you pass in now.day(),now.month(),now.year() it does not return the DOY it returns 999 and I cannot figure out why. Any idea?