6 #ifndef ACE_TIME_DATE_TUPLE_H
7 #define ACE_TIME_DATE_TUPLE_H
10 #include "common/logging.h"
11 #include "local_date_mutation.h"
13 #ifndef ACE_TIME_EXTENDED_ZONE_PROCESSOR_DEBUG
14 #define ACE_TIME_EXTENDED_ZONE_PROCESSOR_DEBUG 0
24 enum class CompareStatus : uint8_t {
39 DateTuple(int16_t y, uint8_t mon, uint8_t d, int32_t secs, uint8_t mod)
40 : year(y), month(mon), day(d), seconds(secs), suffix(mod)
51 if (ACE_TIME_EXTENDED_ZONE_PROCESSOR_DEBUG) {
52 int16_t minutes = seconds / 60;
53 int8_t second = seconds - int32_t(60) * minutes;
54 int8_t hour = minutes / 60;
55 int8_t minute = minutes - hour * 60;
56 char c =
"wsu"[(suffix>>4)];
58 logging::printf(
"%04d-%02u-%02uT%02d:%02d:%02d%c",
59 year, month, day, hour, minute, second, c);
61 logging::printf(
"%04d-%02u-%02uT%02d:%02d%c",
62 year, month, day, hour, minute, c);
69 inline bool operator<(
const DateTuple& a,
const DateTuple& b) {
70 if (a.year < b.year)
return true;
71 if (a.year > b.year)
return false;
72 if (a.month < b.month)
return true;
73 if (a.month > b.month)
return false;
74 if (a.day < b.day)
return true;
75 if (a.day > b.day)
return false;
76 if (a.seconds < b.seconds)
return true;
77 if (a.seconds > b.seconds)
return false;
81 inline bool operator>=(
const DateTuple& a,
const DateTuple& b) {
85 inline bool operator<=(
const DateTuple& a,
const DateTuple& b) {
89 inline bool operator>(
const DateTuple& a,
const DateTuple& b) {
94 inline bool operator==(
const DateTuple& a,
const DateTuple& b) {
95 return a.year == b.year
98 && a.seconds == b.seconds
99 && a.suffix == b.suffix;
112 inline void normalizeDateTuple(DateTuple* dt) {
113 const int32_t kOneDayAsSeconds = int32_t(60) * 60 * 24;
114 if (dt->seconds <= -kOneDayAsSeconds) {
116 local_date_mutation::decrementOneDay(ld);
117 dt->year = ld.year();
118 dt->month = ld.month();
120 dt->seconds += kOneDayAsSeconds;
121 }
else if (kOneDayAsSeconds <= dt->seconds) {
123 local_date_mutation::incrementOneDay(ld);
124 dt->year = ld.year();
125 dt->month = ld.month();
127 dt->seconds -= kOneDayAsSeconds;
138 inline void expandDateTuple(
140 int32_t offsetSeconds,
141 int32_t deltaSeconds,
148 *ttu = {tt->year, tt->month, tt->day,
149 tt->seconds - offsetSeconds,
151 *ttw = {tt->year, tt->month, tt->day,
152 tt->seconds + deltaSeconds,
156 *tts = {tt->year, tt->month, tt->day,
157 tt->seconds + offsetSeconds,
159 *ttw = {tt->year, tt->month, tt->day,
160 tt->seconds + (offsetSeconds + deltaSeconds),
166 *tts = {tt->year, tt->month, tt->day,
167 tt->seconds - deltaSeconds,
169 *ttu = {tt->year, tt->month, tt->day,
170 tt->seconds - (deltaSeconds + offsetSeconds),
174 normalizeDateTuple(ttw);
175 normalizeDateTuple(tts);
176 normalizeDateTuple(ttu);
185 inline acetime_t subtractDateTuple(
const DateTuple& a,
const DateTuple& b) {
195 return (epochDaysA - epochDaysB) * 86400 + a.seconds - b.seconds;
210 inline CompareStatus compareDateTupleFuzzy(
212 const DateTuple& start,
213 const DateTuple& until) {
216 int32_t tMonths = t.year * (int32_t) 12 + t.month;
217 int32_t startMonths = start.year * (int32_t) 12 + start.month;
218 if (tMonths < startMonths - 1)
return CompareStatus::kPrior;
219 int32_t untilMonths = until.year * 12 + until.month;
220 if (untilMonths + 1 < tMonths)
return CompareStatus::kFarFuture;
221 return CompareStatus::kWithinMatch;
static LocalDate forComponents(int16_t year, uint8_t month, uint8_t day)
Factory method using separated year, month and day fields.
int32_t toEpochDays() const
Return number of days since the current epoch year sCurrentEpochYear.
int32_t acetime_t
Type for the number of seconds from epoch.
static const uint8_t kSuffixW
Represents 'w' or wall time.
static const uint8_t kSuffixS
Represents 's' or standard time.
static const uint8_t kSuffixU
Represents 'u' or UTC time.
A tuple that represents a date and time.
void log() const
Used only for debugging.