diff options
author | npwoods <npwoods@alumni.carnegiemellon.edu> | 2017-04-05 21:39:00 -0400 |
---|---|---|
committer | Vas Crabb <cuavas@users.noreply.github.com> | 2017-04-06 11:39:00 +1000 |
commit | f809f0e08d451c7fa05116c9c7b147f499ea82d7 (patch) | |
tree | 8a97a329d1be1014fb51bd36b400c4dee4d2153f /src/lib/util/coreutil.h | |
parent | ee19701c2cd582c17c8a3b855386cd1b79856560 (diff) |
Introduced an 'util::arbitrary_clock' template class, to represent a clock that "knows" when the epoch starts (#2010)
* Introduced an 'util::arbitrary_clock' template class, to represent a clock that "knows" when the epoch starts
Also:
- Converted the NTFS filetime code to use util::arbitrary_clock
- Converted the Mac datetime code to use util::atribrary_clock
This is in preparation for a bigger change to Imgtool where I eliminate usage of time_t
Diffstat (limited to 'src/lib/util/coreutil.h')
-rw-r--r-- | src/lib/util/coreutil.h | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/src/lib/util/coreutil.h b/src/lib/util/coreutil.h index 167f696970e..4e5526fc6f0 100644 --- a/src/lib/util/coreutil.h +++ b/src/lib/util/coreutil.h @@ -29,8 +29,48 @@ uint32_t bcd_2_dec(uint32_t a); GREGORIAN CALENDAR HELPERS ***************************************************************************/ -int gregorian_is_leap_year(int year); -int gregorian_days_in_month(int month, int year); +constexpr bool gregorian_is_leap_year(int year) +{ + return !((year % 100) ? (year % 4) : (year % 400)); +} + + + +//------------------------------------------------- +// gregorian_days_in_month - given a year and a one-counted +// month, return the amount of days in that month +//------------------------------------------------- + +inline int gregorian_days_in_month(int month, int year) +{ + int result; + switch (month) + { + case 4: case 6: + case 9: case 11: + // Thirty days have September, April, June, and November. + result = 30; + break; + + case 1: case 3: + case 5: case 7: + case 8: case 10: + case 12: + // All the rest have Thirty One + result = 31; + break; + + case 2: + // No exceptions, but save one: Twenty Eight hath February + // in fine, and each leap year Twenty Nine + result = gregorian_is_leap_year(year) ? 29 : 28; + break; + + default: + throw false; + } + return result; +} /*************************************************************************** |