AceTime 3.0.0 Date and time classes for Arduino that support timezones from the TZ Database.

EpochConverterHinnant.h
1 /*
2 * MIT License
3 * Copyright (c) 2022 Brian T. Park
4 */
5
6 #ifndef ACE_TIME_EPOCH_CONVERTER_HINNANT_H
7 #define ACE_TIME_EPOCH_CONVERTER_HINNANT_H
8
9 #include <stdint.h>
10
11 namespace ace_time {
12
18 class EpochConverterHinnant {
19 public:
24 static const int16_t kInternalEpochYear = 2000;
25
30 static const int32_t kDaysToInternalEpochFromUnixEpoch = 10957;
31
47 static int32_t toEpochDays(int16_t year, uint8_t month, uint8_t day) {
48 uint16_t yearPrime = year - ((month <= 2) ? 1 : 0);
49 uint16_t era = yearPrime / 400; // [0,24]
50 uint16_t yearOfEra = yearPrime - 400 * era; // [0,399]
51
52 uint8_t monthPrime = (month <= 2) ? month + 9 : month - 3; // [0,11]
53 uint16_t daysUntilMonthPrime = toDaysUntilMonthPrime(monthPrime);
54 uint16_t dayOfYearPrime = daysUntilMonthPrime + day - 1; // [0,365]
55 uint32_t dayOfEra = (uint32_t) 365 * yearOfEra + (yearOfEra / 4)
56 - (yearOfEra / 100) + dayOfYearPrime; // [0, 146096]
57
58 int32_t dayOfEpochPrime = dayOfEra + 146097 * era;
59 return dayOfEpochPrime
60 - (kInternalEpochYear / 400) * 146097 /*relative to 2000-03-01*/
61 + 60 /*relative to 2000-01-01, 2000 is a leap year*/;
62 }
63
75 static void fromEpochDays(int32_t epochDays,
76 int16_t& year, uint8_t& month, uint8_t& day) {
77
78 int32_t dayOfEpochPrime = epochDays
79 + (kInternalEpochYear / 400) * 146097 - 60;
80 uint16_t era = (uint32_t) dayOfEpochPrime / 146097; // [0,24]
81 uint32_t dayOfEra = dayOfEpochPrime - 146097 * era; // [0,146096]
82 uint16_t yearOfEra = (dayOfEra - dayOfEra / 1460 + dayOfEra / 36524
83 - dayOfEra / 146096) / 365; // [0,399]
84 uint16_t yearPrime = yearOfEra + 400 * era; // [0,9999]
85 uint16_t dayOfYearPrime = dayOfEra - (365 * yearOfEra + yearOfEra/4
86 - yearOfEra/100);
87 uint8_t monthPrime = (5 * dayOfYearPrime + 2) / 153;
88 uint16_t daysUntilMonthPrime = toDaysUntilMonthPrime(monthPrime);
89
90 day = dayOfYearPrime - daysUntilMonthPrime + 1; // [1,31]
91 month = (monthPrime < 10) ? monthPrime + 3 : monthPrime - 9; // [1,12]
92 year = yearPrime + ((month <= 2) ? 1 : 0); // [1,9999]
93 }
94
99 static uint16_t toDaysUntilMonthPrime(uint8_t monthPrime) {
100 return (153 * monthPrime + 2) / 5;
101 }
102 };
103
104 }
105
106 #endif
ace_time::EpochConverterHinnant
Utility class that converts AceTime epoch days to (year, month, day) in the Gregorian calendar and vi...
Definition: EpochConverterHinnant.h:18
ace_time::EpochConverterHinnant::kDaysToInternalEpochFromUnixEpoch
static const int32_t kDaysToInternalEpochFromUnixEpoch
Number of days from Unix epoch (1970-01-01 00:00:00 UTC) to the internal epoch (2000-01-01 00:00:00 U...
Definition: EpochConverterHinnant.h:30
ace_time::EpochConverterHinnant::toDaysUntilMonthPrime
static uint16_t toDaysUntilMonthPrime(uint8_t monthPrime)
Return the number days before the given monthPrime.
Definition: EpochConverterHinnant.h:99
ace_time::EpochConverterHinnant::fromEpochDays
static void fromEpochDays(int32_t epochDays, int16_t &year, uint8_t &month, uint8_t &day)
Extract the (year, month, day) fields from AceTime epochDays.
Definition: EpochConverterHinnant.h:75
ace_time::EpochConverterHinnant::toEpochDays
static int32_t toEpochDays(int16_t year, uint8_t month, uint8_t day)
Convert (year, month, day) in the Gregorian calendar to days since the internal epoch (2000-01-01).
Definition: EpochConverterHinnant.h:47
ace_time::EpochConverterHinnant::kInternalEpochYear
static const int16_t kInternalEpochYear
Epoch year used by this epoch converter.
Definition: EpochConverterHinnant.h:24