AceTime  2.3.0
Date and time classes for Arduino that support timezones from the TZ Database.
EpochConverterJulian.h
1 /*
2  * MIT License
3  * Copyright (c) 2022 Brian T. Park
4  */
5 
6 #ifndef ACE_TIME_EPOCH_CONVERTER_JULIAN_H
7 #define ACE_TIME_EPOCH_CONVERTER_JULIAN_H
8 
9 #include <stdint.h>
10 
11 namespace ace_time {
12 namespace internal {
13 
21  public:
26  static const int16_t kInternalEpochYear = 2000;
27 
32  static const int32_t kDaysToInternalEpochFromUnixEpoch = 10957;
33 
41  static const int32_t kDaysToInternalEpochFromJulianEpoch = 1721060
42  + (kInternalEpochYear / 400) * 146097; // 2451545
43 
71  static int32_t toEpochDays(int16_t year, uint8_t month, uint8_t day) {
72  int8_t mm = (month - 14)/12;
73  int32_t jdn = ((int32_t) 1461 * (year + 4800 + mm))/4
74  + (367 * (month - 2 - 12 * mm))/12
75  - (3 * ((year + 4900 + mm)/100))/4
76  + day - 32075;
78  }
79 
87  static void fromEpochDays(int32_t epochDays,
88  int16_t& year, uint8_t& month, uint8_t& day) {
89 
90  uint32_t J = epochDays + kDaysToInternalEpochFromJulianEpoch;
91  uint32_t f = J + 1401 + (((4 * J + 274277 ) / 146097) * 3) / 4 - 38;
92  uint32_t e = 4 * f + 3;
93  uint32_t g = e % 1461 / 4;
94  uint32_t h = 5 * g + 2;
95  day = (h % 153) / 5 + 1;
96  month = (h / 153 + 2) % 12 + 1;
97  year = (e / 1461) - 4716 + (12 + 2 - month) / 12;
98 
99  // 2000-01-01 is Saturday (7)
100  //dayOfWeek = (epochDays + 6) % 7 + 1;
101  }
102 };
103 
104 }
105 }
106 
107 #endif
Utility class that converts AceTime epoch days to (year, month, day) in the Gregorian calendar and vi...
static const int16_t kInternalEpochYear
Epoch year used by this epoch converter.
static const int32_t kDaysToInternalEpochFromJulianEpoch
Number of days from the modified proleptic Julian calendar epoch (4713 BC 01-01, modified to start at...
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...
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).
static void fromEpochDays(int32_t epochDays, int16_t &year, uint8_t &month, uint8_t &day)
Extract the (year, month, day) fields from AceTime epochDays.