aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortobo <tobo@yandex-team.com>2025-02-27 23:36:24 +0300
committertobo <tobo@yandex-team.com>2025-02-27 23:53:09 +0300
commit08060dc027babcf4a9eefc4d77122524ac4714fe (patch)
tree4f3280457c7fe7f82a409c35c2bfb012ea08cfa1
parent042383afc665c770fae0a51b02fcd6671cc3bd35 (diff)
downloadydb-08060dc027babcf4a9eefc4d77122524ac4714fe.tar.gz
[util] getting rid of LUT in DayOfYearToMonth
commit_hash:9ec33d0cbe2da6aeea6e8d02fdaed5f45b6e3534
-rw-r--r--util/datetime/systime.cpp33
1 files changed, 5 insertions, 28 deletions
diff --git a/util/datetime/systime.cpp b/util/datetime/systime.cpp
index 04972b6e48..8bd20c35ff 100644
--- a/util/datetime/systime.cpp
+++ b/util/datetime/systime.cpp
@@ -124,36 +124,13 @@ namespace {
constexpr ui16 MONTH_TO_DAYS_LEAP[12] = {
0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
- struct TMonth32LUT {
- ui8 LastMonthDay32[12];
- ui8 FirstMonthDay32[12];
- };
-
- constexpr TMonth32LUT COMMON_YEAR = {
- .LastMonthDay32 = {31, 27, 26, 24, 23, 21, 20, 19, 17, 16, 14, 13},
- .FirstMonthDay32 = {0, 1, 5, 6, 8, 9, 11, 12, 13, 15, 16, 18},
- };
-
constexpr int DayOfYearToMonth(ui32& yearDay, const bool leapYear) {
- Y_ASSERT(yearDay < DAYS_IN_YEAR + leapYear);
- if (leapYear) {
- if (yearDay > 59) {
- --yearDay;
- } else if (yearDay == 59) {
- // February, 29th
- yearDay = 28;
- return 1;
- }
+ if (yearDay >= 31 + 28 + leapYear) {
+ yearDay += 2 - leapYear;
}
- const int approxMonth = static_cast<int>(yearDay / 32);
- const int approxMDay = static_cast<int>(yearDay % 32);
- const int dayThreshold = COMMON_YEAR.LastMonthDay32[approxMonth];
- const int currentMonthMDayOffset = COMMON_YEAR.FirstMonthDay32[approxMonth];
- const bool nextMonth = (approxMDay >= dayThreshold);
- const int dayCorrection = nextMonth ? -dayThreshold : currentMonthMDayOffset;
- yearDay = approxMDay + dayCorrection;
- const int month = approxMonth + nextMonth;
- return month;
+ const ui32 month = (yearDay * 67 + 35) >> 11;
+ yearDay -= (month * 489 + 8) >> 4;
+ return static_cast<int>(month);
}
class TDayNoToYearLookupTable {