/root/doris/be/src/util/time_lut.cpp
Line | Count | Source (jump to first uncovered line) |
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 "vec/runtime/vdatetime_value.h" |
22 | | |
23 | | namespace doris { |
24 | 2 | TimeLUTImpl::TimeLUTImpl() { |
25 | 2 | init_time_lut(); |
26 | 2 | } |
27 | | |
28 | 2 | void TimeLUTImpl::init_time_lut() { |
29 | 162 | for (uint32_t y = LUT_START_YEAR; y < LUT_END_YEAR; y++) { |
30 | 160 | uint16_t tmp_year = 0; |
31 | 2.08k | for (uint8_t m = 0; m < NUM_MONTHS; m++) { |
32 | 61.4k | for (uint8_t i = 0; i < NUM_DAYS; i++) { |
33 | 59.5k | week_table[y - LUT_START_YEAR][m][i] = |
34 | 59.5k | calc_week(y, m + 1, i + 1, false, false, true, &tmp_year); |
35 | 59.5k | week_of_year_table[y - LUT_START_YEAR][m][i] = |
36 | 59.5k | calc_week(y, m + 1, i + 1, true, true, false, &tmp_year); |
37 | 59.5k | year_week_table[y - LUT_START_YEAR][m][i] = year_week(y, m + 1, i + 1); |
38 | 59.5k | } |
39 | 1.92k | } |
40 | 160 | } |
41 | 2 | } |
42 | | |
43 | | uint8_t calc_week(uint16_t year, uint8_t month, uint8_t day, bool monday_first, bool week_year, |
44 | 178k | bool first_weekday, uint16_t* to_year) { |
45 | 178k | uint64_t day_nr = calc_daynr(year, month, day); |
46 | 178k | uint64_t daynr_first_day = calc_daynr(year, 1, 1); |
47 | 178k | uint8_t weekday_first_day = calc_weekday(daynr_first_day, !monday_first); |
48 | | |
49 | 178k | int days = 0; |
50 | 178k | *to_year = year; |
51 | | |
52 | | // Check weather the first days of this year belongs to last year |
53 | 178k | if (month == 1 && day <= (7 - weekday_first_day)) { |
54 | 1.93k | if (!week_year && ((first_weekday && weekday_first_day != 0) || |
55 | 646 | (!first_weekday && weekday_first_day > 3))) { |
56 | 478 | return 0; |
57 | 478 | } |
58 | 1.45k | (*to_year)--; |
59 | 1.45k | week_year = true; |
60 | 1.45k | daynr_first_day -= (days = calc_days_in_year(*to_year)); |
61 | 1.45k | weekday_first_day = (weekday_first_day + 53 * 7 - days) % 7; |
62 | 1.45k | } |
63 | | |
64 | | // How many days since first week |
65 | 178k | if ((first_weekday && weekday_first_day != 0) || (!first_weekday && weekday_first_day > 3)) { |
66 | | // days in new year belongs to last year. |
67 | 127k | days = day_nr - (daynr_first_day + (7 - weekday_first_day)); |
68 | 127k | } else { |
69 | | // days in new year belongs to this year. |
70 | 51.0k | days = day_nr - (daynr_first_day - weekday_first_day); |
71 | 51.0k | } |
72 | | |
73 | 178k | if (week_year && days >= 52 * 7) { |
74 | 1.37k | weekday_first_day = (weekday_first_day + calc_days_in_year(*to_year)) % 7; |
75 | 1.37k | if ((first_weekday && weekday_first_day == 0) || |
76 | 1.37k | (!first_weekday && weekday_first_day <= 3)) { |
77 | | // Belong to next year. |
78 | 968 | (*to_year)++; |
79 | 968 | return 1; |
80 | 968 | } |
81 | 1.37k | } |
82 | | |
83 | 177k | return days / 7 + 1; |
84 | 178k | } |
85 | | |
86 | 2.83k | uint32_t calc_days_in_year(uint32_t year) { |
87 | 2.83k | return is_leap(year) ? 366 : 365; |
88 | 2.83k | } |
89 | | |
90 | 178k | uint8_t calc_weekday(uint64_t day_nr, bool is_sunday_first_day) { |
91 | 178k | return (day_nr + 5L + (is_sunday_first_day ? 1L : 0L)) % 7; |
92 | 178k | } |
93 | | |
94 | 59.5k | uint32_t year_week(uint16_t yy, uint8_t month, uint8_t day) { |
95 | | //not covered by year_week_table, calculate at runtime |
96 | 59.5k | uint16_t to_year = 0; |
97 | | // The range of the week in the year_week is 1-53, so the mode WEEK_YEAR is always true. |
98 | 59.5k | uint8_t week = calc_week(yy, month, day, false, true, true, &to_year); |
99 | 59.5k | return to_year * 100 + week; |
100 | 59.5k | } |
101 | | |
102 | | } // namespace doris |