AceTime  3.0.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 
20  public:
25  static const int16_t kInternalEpochYear = 2000;
26 
31  static const int32_t kDaysToInternalEpochFromUnixEpoch = 10957;
32 
40  static const int32_t kDaysToInternalEpochFromJulianEpoch = 1721060
41  + (kInternalEpochYear / 400) * 146097; // 2451545
42 
70  static int32_t toEpochDays(int16_t year, uint8_t month, uint8_t day) {
71  int8_t mm = (month - 14)/12;
72  int32_t jdn = ((int32_t) 1461 * (year + 4800 + mm))/4
73  + (367 * (month - 2 - 12 * mm))/12
74  - (3 * ((year + 4900 + mm)/100))/4
75  + day - 32075;
77  }
78 
86  static void fromEpochDays(int32_t epochDays,
87  int16_t& year, uint8_t& month, uint8_t& day) {
88 
89  uint32_t J = epochDays + kDaysToInternalEpochFromJulianEpoch;
90  uint32_t f = J + 1401 + (((4 * J + 274277 ) / 146097) * 3) / 4 - 38;
91  uint32_t e = 4 * f + 3;
92  uint32_t g = e % 1461 / 4;
93  uint32_t h = 5 * g + 2;
94  day = (h % 153) / 5 + 1;
95  month = (h / 153 + 2) % 12 + 1;
96  year = (e / 1461) - 4716 + (12 + 2 - month) / 12;
97 
98  // 2000-01-01 is Saturday (7)
99  //dayOfWeek = (epochDays + 6) % 7 + 1;
100  }
101 };
102 
103 }
104 
105 #endif
Utility class that converts AceTime epoch days to (year, month, day) in the Gregorian calendar and vi...
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 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 const int16_t kInternalEpochYear
Epoch year used by this epoch converter.
static void fromEpochDays(int32_t epochDays, int16_t &year, uint8_t &month, uint8_t &day)
Extract the (year, month, day) fields from AceTime epochDays.