aboutsummaryrefslogtreecommitdiffstats
path: root/util/datetime
diff options
context:
space:
mode:
authornae202 <nae202@yandex-team.com>2024-11-20 12:52:01 +0300
committernae202 <nae202@yandex-team.com>2024-11-20 13:14:09 +0300
commit0b9d91e900b52bccd6eabd033acbb57c4ee173fc (patch)
tree6839ed69fbda4d5214eb418acc4554924bc64160 /util/datetime
parent878e26057d11cce46b7bc3a6c838209d4686e28b (diff)
downloadydb-0b9d91e900b52bccd6eabd033acbb57c4ee173fc.tar.gz
Part of PR. Style
Часть большого ПР REVIEW:7264088 commit_hash:0f5b03fbbed0ac30f734943309e3ef5cd4d7a30e
Diffstat (limited to 'util/datetime')
-rw-r--r--util/datetime/parser.h94
1 files changed, 62 insertions, 32 deletions
diff --git a/util/datetime/parser.h b/util/datetime/parser.h
index 43b6df8da2..eaffa03818 100644
--- a/util/datetime/parser.h
+++ b/util/datetime/parser.h
@@ -21,49 +21,60 @@ struct TDateTimeFields {
i32 ZoneOffsetMinutes;
void SetLooseYear(ui32 year) {
- if (year < 60)
+ if (year < 60) {
year += 100;
- if (year < 160)
+ }
+ if (year < 160) {
year += 1900;
+ }
Year = year;
}
bool IsOk() const noexcept {
- if (Year < 1970)
+ if (Year < 1970) {
return false;
- if (Month < 1 || Month > 12)
+ }
+ if (Month < 1 || Month > 12) {
return false;
+ }
unsigned int maxMonthDay = 31;
if (Month == 4 || Month == 6 || Month == 9 || Month == 11) {
maxMonthDay = 30;
} else if (Month == 2) {
- if (Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0))
+ if (Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)) {
// leap year
maxMonthDay = 29;
- else
+ } else {
maxMonthDay = 28;
+ }
}
- if (Day > maxMonthDay)
+ if (Day > maxMonthDay) {
return false;
+ }
- if (Hour > 23)
+ if (Hour > 23) {
return false;
+ }
- if (Minute > 59)
+ if (Minute > 59) {
return false;
+ }
// handle leap second which is explicitly allowed by ISO 8601:2004(E) $2.2.2
// https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
- if (Second > 60)
+ if (Second > 60) {
return false;
+ }
- if (MicroSecond > 999999)
+ if (MicroSecond > 999999) {
return false;
+ }
if (Year == 1970 && Month == 1 && Day == 1) {
- if ((i64)(3600 * Hour + 60 * Minute + Second) < (60 * ZoneOffsetMinutes))
+ if ((i64)(3600 * Hour + 60 * Minute + Second) < (60 * ZoneOffsetMinutes)) {
return false;
+ }
}
return true;
@@ -71,14 +82,16 @@ struct TDateTimeFields {
TInstant ToInstant(TInstant defaultValue) const {
time_t tt = ToTimeT(-1);
- if (tt == -1)
+ if (tt == -1) {
return defaultValue;
+ }
return TInstant::Seconds(tt) + TDuration::MicroSeconds(MicroSecond);
}
time_t ToTimeT(time_t defaultValue) const {
- if (!IsOk())
+ if (!IsOk()) {
return defaultValue;
+ }
struct tm tm;
Zero(tm);
tm.tm_year = Year - 1900;
@@ -88,8 +101,9 @@ struct TDateTimeFields {
tm.tm_min = Minute;
tm.tm_sec = Second;
time_t tt = TimeGM(&tm);
- if (tt == -1)
+ if (tt == -1) {
return defaultValue;
+ }
return tt - ZoneOffsetMinutes * 60;
}
};
@@ -118,8 +132,9 @@ protected:
}
inline TInstant GetResult(int firstFinalState, TInstant defaultValue) const {
- if (cs < firstFinalState)
+ if (cs < firstFinalState) {
return defaultValue;
+ }
return DateTimeFields.ToInstant(defaultValue);
}
};
@@ -177,47 +192,58 @@ struct TDateTimeFieldsDeprecated {
i32 ZoneOffsetMinutes;
void SetLooseYear(ui32 year) {
- if (year < 60)
+ if (year < 60) {
year += 100;
- if (year < 160)
+ }
+ if (year < 160) {
year += 1900;
+ }
Year = year;
}
bool IsOk() const noexcept {
- if (Year < 1970)
+ if (Year < 1970) {
return false;
- if (Month < 1 || Month > 12)
+ }
+ if (Month < 1 || Month > 12) {
return false;
+ }
unsigned int maxMonthDay = 31;
if (Month == 4 || Month == 6 || Month == 9 || Month == 11) {
maxMonthDay = 30;
} else if (Month == 2) {
- if (Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0))
+ if (Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)) {
// leap year
maxMonthDay = 29;
- else
+ } else {
maxMonthDay = 28;
+ }
}
- if (Day > maxMonthDay)
+ if (Day > maxMonthDay) {
return false;
+ }
- if (Hour > 23)
+ if (Hour > 23) {
return false;
+ }
- if (Minute > 59)
+ if (Minute > 59) {
return false;
+ }
- if (Second > 60)
+ if (Second > 60) {
return false;
+ }
- if (MicroSecond > 999999)
+ if (MicroSecond > 999999) {
return false;
+ }
if (Year == 1970 && Month == 1 && Day == 1) {
- if ((i64)(3600 * Hour + 60 * Minute + Second) < (60 * ZoneOffsetMinutes))
+ if ((i64)(3600 * Hour + 60 * Minute + Second) < (60 * ZoneOffsetMinutes)) {
return false;
+ }
}
return true;
@@ -225,14 +251,16 @@ struct TDateTimeFieldsDeprecated {
TInstant ToInstant(TInstant defaultValue) const {
time_t tt = ToTimeT(-1);
- if (tt == -1)
+ if (tt == -1) {
return defaultValue;
+ }
return TInstant::Seconds(tt) + TDuration::MicroSeconds(MicroSecond);
}
time_t ToTimeT(time_t defaultValue) const {
- if (!IsOk())
+ if (!IsOk()) {
return defaultValue;
+ }
struct tm tm;
Zero(tm);
tm.tm_year = Year - 1900;
@@ -242,8 +270,9 @@ struct TDateTimeFieldsDeprecated {
tm.tm_min = Minute;
tm.tm_sec = Second;
time_t tt = TimeGM(&tm);
- if (tt == -1)
+ if (tt == -1) {
return defaultValue;
+ }
return tt - ZoneOffsetMinutes * 60;
}
};
@@ -272,8 +301,9 @@ protected:
}
inline TInstant GetResult(int firstFinalState, TInstant defaultValue) const {
- if (cs < firstFinalState)
+ if (cs < firstFinalState) {
return defaultValue;
+ }
return DateTimeFields.ToInstant(defaultValue);
}
};