Coverage Report

Created: 2024-11-21 13:02

/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
1
TimeLUTImpl::TimeLUTImpl() {
25
1
    init_time_lut();
26
1
}
27
28
1
void TimeLUTImpl::init_time_lut() {
29
81
    for (uint32_t y = LUT_START_YEAR; y < LUT_END_YEAR; y++) {
30
80
        uint16_t tmp_year = 0;
31
1.04k
        for (uint8_t m = 0; m < NUM_MONTHS; m++) {
32
30.7k
            for (uint8_t i = 0; i < NUM_DAYS; i++) {
33
29.7k
                week_table[y - LUT_START_YEAR][m][i] =
34
29.7k
                        calc_week(y, m + 1, i + 1, false, false, true, &tmp_year);
35
29.7k
                week_of_year_table[y - LUT_START_YEAR][m][i] =
36
29.7k
                        calc_week(y, m + 1, i + 1, true, true, false, &tmp_year);
37
29.7k
                year_week_table[y - LUT_START_YEAR][m][i] = year_week(y, m + 1, i + 1);
38
29.7k
            }
39
960
        }
40
80
    }
41
1
}
42
43
uint8_t calc_week(uint16_t year, uint8_t month, uint8_t day, bool monday_first, bool week_year,
44
89.2k
                  bool first_weekday, uint16_t* to_year) {
45
89.2k
    uint64_t day_nr = calc_daynr(year, month, day);
46
89.2k
    uint64_t daynr_first_day = calc_daynr(year, 1, 1);
47
89.2k
    uint8_t weekday_first_day = calc_weekday(daynr_first_day, !monday_first);
48
49
89.2k
    int days = 0;
50
89.2k
    *to_year = year;
51
52
    // Check weather the first days of this year belongs to last year
53
89.2k
    if (month == 1 && day <= (7 - weekday_first_day)) {
54
965
        if (!week_year && ((first_weekday && weekday_first_day != 0) ||
55
323
                           (!first_weekday && weekday_first_day > 3))) {
56
239
            return 0;
57
239
        }
58
726
        (*to_year)--;
59
726
        week_year = true;
60
726
        daynr_first_day -= (days = calc_days_in_year(*to_year));
61
726
        weekday_first_day = (weekday_first_day + 53 * 7 - days) % 7;
62
726
    }
63
64
    // How many days since first week
65
89.0k
    if ((first_weekday && weekday_first_day != 0) || (!first_weekday && weekday_first_day > 3)) {
66
        // days in new year belongs to last year.
67
63.5k
        days = day_nr - (daynr_first_day + (7 - weekday_first_day));
68
63.5k
    } else {
69
        // days in new year belongs to this year.
70
25.5k
        days = day_nr - (daynr_first_day - weekday_first_day);
71
25.5k
    }
72
73
89.0k
    if (week_year && days >= 52 * 7) {
74
687
        weekday_first_day = (weekday_first_day + calc_days_in_year(*to_year)) % 7;
75
687
        if ((first_weekday && weekday_first_day == 0) ||
76
687
            (!first_weekday && weekday_first_day <= 3)) {
77
            // Belong to next year.
78
484
            (*to_year)++;
79
484
            return 1;
80
484
        }
81
687
    }
82
83
88.5k
    return days / 7 + 1;
84
89.0k
}
85
86
1.41k
uint32_t calc_days_in_year(uint32_t year) {
87
1.41k
    return is_leap(year) ? 366 : 365;
88
1.41k
}
89
90
52.7k
bool is_leap(uint32_t year) {
91
52.7k
    return ((year % 4) == 0) && ((year % 100 != 0) || ((year % 400) == 0 && year));
92
52.7k
}
93
94
89.3k
uint8_t calc_weekday(uint64_t day_nr, bool is_sunday_first_day) {
95
89.3k
    return (day_nr + 5L + (is_sunday_first_day ? 1L : 0L)) % 7;
96
89.3k
}
97
98
399k
uint32_t calc_daynr(uint16_t year, uint8_t month, uint8_t day) {
99
    // date_day_offet_dict range from [1900-01-01, 2039-12-31]
100
399k
    if (date_day_offset_dict::can_speed_up_calc_daynr(year) &&
101
399k
        LIKELY(date_day_offset_dict::get_dict_init())) {
102
179k
        return date_day_offset_dict::get().daynr(year, month, day);
103
179k
    }
104
105
220k
    uint32_t delsum = 0;
106
220k
    int y = year;
107
108
220k
    if (year == 0 && month == 0) {
109
58
        return 0;
110
58
    }
111
112
    /* Cast to int to be able to handle month == 0 */
113
220k
    delsum = 365 * y + 31 * (month - 1) + day;
114
220k
    if (month <= 2) {
115
        // No leap year
116
134k
        y--;
117
134k
    } else {
118
        // This is great!!!
119
        // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
120
        // 0, 0, 3, 3, 4, 4, 5, 5, 5,  6,  7,  8
121
85.6k
        delsum -= (month * 4 + 23) / 10;
122
85.6k
    }
123
    // Every 400 year has 97 leap year, 100, 200, 300 are not leap year.
124
220k
    return delsum + y / 4 - y / 100 + y / 400;
125
220k
}
126
127
29.7k
uint32_t year_week(uint16_t yy, uint8_t month, uint8_t day) {
128
    //not covered by year_week_table, calculate at runtime
129
29.7k
    uint16_t to_year = 0;
130
    // The range of the week in the year_week is 1-53, so the mode WEEK_YEAR is always true.
131
29.7k
    uint8_t week = calc_week(yy, month, day, false, true, true, &to_year);
132
29.7k
    return to_year * 100 + week;
133
29.7k
}
134
135
} // namespace doris