/root/doris/be/src/util/time_lut.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | |
19 | | #include "util/time_lut.h" |
20 | | |
21 | | #include <cstdint> |
22 | | |
23 | | #include "common/cast_set.h" |
24 | | #include "vec/runtime/vdatetime_value.h" |
25 | | |
26 | | namespace doris { |
27 | | #include "common/compile_check_begin.h" |
28 | 1 | TimeLUTImpl::TimeLUTImpl() { |
29 | 1 | init_time_lut(); |
30 | 1 | } |
31 | | |
32 | 1 | void TimeLUTImpl::init_time_lut() { |
33 | 81 | for (uint16_t y = LUT_START_YEAR; y < LUT_END_YEAR; y++) { |
34 | 80 | uint16_t tmp_year = 0; |
35 | 1.04k | for (uint8_t m = 0; m < NUM_MONTHS; m++) { |
36 | 30.7k | for (uint8_t i = 0; i < NUM_DAYS; i++) { |
37 | 29.7k | if (!VecDateTimeValue::check_date(y, m + 1, i + 1)) { |
38 | | // valid date |
39 | 29.2k | week_table[y - LUT_START_YEAR][m][i] = |
40 | 29.2k | calc_week(y, m + 1, i + 1, false, false, true, &tmp_year); |
41 | 29.2k | week_of_year_table[y - LUT_START_YEAR][m][i] = |
42 | 29.2k | calc_week(y, m + 1, i + 1, true, true, false, &tmp_year); |
43 | 29.2k | year_week_table[y - LUT_START_YEAR][m][i] = year_week(y, m + 1, i + 1); |
44 | 29.2k | } |
45 | 29.7k | } |
46 | 960 | } |
47 | 80 | } |
48 | 1 | } |
49 | | |
50 | | uint8_t calc_week(uint16_t year, uint8_t month, uint8_t day, bool monday_first, bool week_year, |
51 | 87.6k | bool first_weekday, uint16_t* to_year) { |
52 | 87.6k | uint64_t day_nr = calc_daynr(year, month, day); |
53 | 87.6k | uint64_t daynr_first_day = calc_daynr(year, 1, 1); |
54 | 87.6k | uint8_t weekday_first_day = calc_weekday(daynr_first_day, !monday_first); |
55 | | |
56 | 87.6k | uint16_t days = 0; // days in year |
57 | 87.6k | *to_year = year; |
58 | | |
59 | | // Check weather the first days of this year belongs to last year |
60 | 87.6k | if (month == 1 && day <= (7 - weekday_first_day)) { |
61 | 965 | if (!week_year && ((first_weekday && weekday_first_day != 0) || |
62 | 323 | (!first_weekday && weekday_first_day > 3))) { |
63 | 239 | return 0; |
64 | 239 | } |
65 | 726 | (*to_year)--; |
66 | 726 | week_year = true; |
67 | 726 | daynr_first_day -= (days = calc_days_in_year(*to_year)); |
68 | 726 | weekday_first_day = (weekday_first_day + 53 * 7 - days) % 7; |
69 | 726 | } |
70 | | |
71 | | // How many days since first week |
72 | 87.6k | DCHECK_LE(day_nr - daynr_first_day, 373); |
73 | 87.4k | if ((first_weekday && weekday_first_day != 0) || (!first_weekday && weekday_first_day > 3)) { |
74 | | // days in new year belongs to last year. |
75 | 62.3k | days = static_cast<uint16_t>(day_nr - daynr_first_day - (7 - weekday_first_day)); |
76 | 62.3k | } else { |
77 | | // days in new year belongs to this year. |
78 | 25.0k | days = static_cast<uint16_t>(day_nr - daynr_first_day + weekday_first_day); |
79 | 25.0k | } |
80 | | |
81 | 87.4k | if (week_year && days >= 52 * 7) { |
82 | 687 | weekday_first_day = (weekday_first_day + calc_days_in_year(*to_year)) % 7; |
83 | 687 | if ((first_weekday && weekday_first_day == 0) || |
84 | 687 | (!first_weekday && weekday_first_day <= 3)) { |
85 | | // Belong to next year. |
86 | 484 | (*to_year)++; |
87 | 484 | return 1; |
88 | 484 | } |
89 | 687 | } |
90 | | |
91 | 86.9k | return static_cast<uint8_t>(days / 7 + 1); |
92 | 87.4k | } |
93 | | |
94 | 1.43k | uint16_t calc_days_in_year(uint32_t year) { |
95 | 1.43k | return is_leap(year) ? 366 : 365; |
96 | 1.43k | } |
97 | | |
98 | 87.8k | uint8_t calc_weekday(uint64_t day_nr, bool is_sunday_first_day) { |
99 | 87.8k | return (day_nr + 5L + (is_sunday_first_day ? 1L : 0L)) % 7; |
100 | 87.8k | } |
101 | | |
102 | 29.2k | uint32_t year_week(uint16_t yy, uint8_t month, uint8_t day) { |
103 | | //not covered by year_week_table, calculate at runtime |
104 | 29.2k | uint16_t to_year = 0; |
105 | | // The range of the week in the year_week is 1-53, so the mode WEEK_YEAR is always true. |
106 | 29.2k | uint8_t week = calc_week(yy, month, day, false, true, true, &to_year); |
107 | 29.2k | return to_year * 100 + week; |
108 | 29.2k | } |
109 | | #include "common/compile_check_end.h" |
110 | | } // namespace doris |