AceTime 2.4.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 namespace internal {
13
19 class EpochConverterHinnant {
20 public:
25 static const int16_t kInternalEpochYear = 2000;
26
31 static const int32_t kDaysToInternalEpochFromUnixEpoch = 10957;
32
48 static int32_t toEpochDays(int16_t year, uint8_t month, uint8_t day) {
49 uint16_t yearPrime = year - ((month <= 2) ? 1 : 0);
50 uint16_t era = yearPrime / 400; // [0,24]
51 uint16_t yearOfEra = yearPrime - 400 * era; // [0,399]
52
53 uint8_t monthPrime = (month <= 2) ? month + 9 : month - 3; // [0,11]
54 uint16_t daysUntilMonthPrime = toDaysUntilMonthPrime(monthPrime);
55 uint16_t dayOfYearPrime = daysUntilMonthPrime + day - 1; // [0,365]
56 uint32_t dayOfEra = (uint32_t) 365 * yearOfEra + (yearOfEra / 4)
57 - (yearOfEra / 100) + dayOfYearPrime; // [0, 146096]
58
59 int32_t dayOfEpochPrime = dayOfEra + 146097 * era;
60 return dayOfEpochPrime
61 - (kInternalEpochYear / 400) * 146097 /*relative to 2000-03-01*/
62 + 60 /*relative to 2000-01-01, 2000 is a leap year*/;
63 }
64
76 static void fromEpochDays(int32_t epochDays,
77 int16_t& year, uint8_t& month, uint8_t& day) {
78
79 int32_t dayOfEpochPrime = epochDays
80 + (kInternalEpochYear / 400) * 146097 - 60;
81 uint16_t era = (uint32_t) dayOfEpochPrime / 146097; // [0,24]
82 uint32_t dayOfEra = dayOfEpochPrime - 146097 * era; // [0,146096]
83 uint16_t yearOfEra = (dayOfEra - dayOfEra / 1460 + dayOfEra / 36524
84 - dayOfEra / 146096) / 365; // [0,399]
85 uint16_t yearPrime = yearOfEra + 400 * era; // [0,9999]
86 uint16_t dayOfYearPrime = dayOfEra - (365 * yearOfEra + yearOfEra/4
87 - yearOfEra/100);
88 uint8_t monthPrime = (5 * dayOfYearPrime + 2) / 153;
89 uint16_t daysUntilMonthPrime = toDaysUntilMonthPrime(monthPrime);
90
91 day = dayOfYearPrime - daysUntilMonthPrime + 1; // [1,31]
92 month = (monthPrime < 10) ? monthPrime + 3 : monthPrime - 9; // [1,12]
93 year = yearPrime + ((month <= 2) ? 1 : 0); // [1,9999]
94 }
95
100 static uint16_t toDaysUntilMonthPrime(uint8_t monthPrime) {
101 return (153 * monthPrime + 2) / 5;
102 }
103 };
104
105 }
106 }
107
108 #endif
ace_time::internal::EpochConverterHinnant
Utility class that converts AceTime epoch days to (year, month, day) in the Gregorian calendar and vi...
Definition: EpochConverterHinnant.h:19
ace_time::internal::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:31
ace_time::internal::EpochConverterHinnant::kInternalEpochYear
static const int16_t kInternalEpochYear
Epoch year used by this epoch converter.
Definition: EpochConverterHinnant.h:25
ace_time::internal::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:76
ace_time::internal::EpochConverterHinnant::toDaysUntilMonthPrime
static uint16_t toDaysUntilMonthPrime(uint8_t monthPrime)
Return the number days before the given monthPrime.
Definition: EpochConverterHinnant.h:100
ace_time::internal::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:48