AceTime  2.2.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 kConverterEpochYear = 2000;
27 
32  static const int32_t kDaysToConverterEpochFromUnixEpoch = 10957;
33 
42  static const int32_t kDaysToConverterEpochFromJulianEpoch = 1721060
43  + (kConverterEpochYear / 400) * 146097; // 2451545
44 
72  static int32_t toEpochDays(int16_t year, uint8_t month, uint8_t day) {
73  int8_t mm = (month - 14)/12;
74  int32_t jdn = ((int32_t) 1461 * (year + 4800 + mm))/4
75  + (367 * (month - 2 - 12 * mm))/12
76  - (3 * ((year + 4900 + mm)/100))/4
77  + day - 32075;
79  }
80 
88  static void fromEpochDays(int32_t epochDays,
89  int16_t& year, uint8_t& month, uint8_t& day) {
90 
91  uint32_t J = epochDays + kDaysToConverterEpochFromJulianEpoch;
92  uint32_t f = J + 1401 + (((4 * J + 274277 ) / 146097) * 3) / 4 - 38;
93  uint32_t e = 4 * f + 3;
94  uint32_t g = e % 1461 / 4;
95  uint32_t h = 5 * g + 2;
96  day = (h % 153) / 5 + 1;
97  month = (h / 153 + 2) % 12 + 1;
98  year = (e / 1461) - 4716 + (12 + 2 - month) / 12;
99 
100  // 2000-01-01 is Saturday (7)
101  //dayOfWeek = (epochDays + 6) % 7 + 1;
102  }
103 };
104 
105 }
106 }
107 
108 #endif
Utility class that converts AceTime epoch days to (year, month, day) in the Gregorian calendar and vi...
static const int16_t kConverterEpochYear
Epoch year used by this epoch converter.
static const int32_t kDaysToConverterEpochFromUnixEpoch
Number of days from Unix epoch (1970-01-01 00:00:00 UTC) to the converter epoch (2000-01-01 00:00:00 ...
static const int32_t kDaysToConverterEpochFromJulianEpoch
Number of days from the modified proleptic Julian calendar epoch (4713 BC 01-01, modified to start at...
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 converter 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.