/root/doris/be/src/util/time_lut.h
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 | | #pragma once |
20 | | |
21 | | #include <stdint.h> |
22 | | |
23 | | #include <atomic> |
24 | | |
25 | | namespace doris { |
26 | | |
27 | | constexpr uint32_t LUT_START_YEAR = 1950; |
28 | | constexpr uint32_t LUT_END_YEAR = 2030; |
29 | | |
30 | | constexpr uint32_t NUM_MONTHS = 12; |
31 | | constexpr uint32_t NUM_DAYS = 31; |
32 | | |
33 | | uint32_t year_week(uint16_t yy, uint8_t month, uint8_t day); |
34 | | |
35 | | uint32_t calc_daynr(uint16_t year, uint8_t month, uint8_t day); |
36 | | |
37 | | uint8_t calc_weekday(uint64_t day_nr, bool is_sunday_first_day); |
38 | | |
39 | | bool is_leap(uint32_t year); |
40 | | |
41 | | uint32_t calc_days_in_year(uint32_t year); |
42 | | |
43 | | uint8_t calc_week(uint16_t yy, uint8_t month, uint8_t day, bool monday_first, bool week_year, |
44 | | bool first_weekday, uint16_t* to_year); |
45 | | |
46 | | class TimeLUTImpl { |
47 | | public: |
48 | | int8_t week_of_year_table[LUT_END_YEAR - LUT_START_YEAR][NUM_MONTHS][NUM_DAYS]; |
49 | | |
50 | | uint32_t year_week_table[LUT_END_YEAR - LUT_START_YEAR][NUM_MONTHS][NUM_DAYS]; |
51 | | |
52 | | int8_t week_table[LUT_END_YEAR - LUT_START_YEAR][NUM_MONTHS][NUM_DAYS]; |
53 | | |
54 | | private: |
55 | | friend class TimeLUT; |
56 | | |
57 | | TimeLUTImpl(); |
58 | | void init_time_lut(); |
59 | | }; |
60 | | |
61 | | class TimeLUT { |
62 | | public: |
63 | 12 | static const TimeLUTImpl* GetImplement() { |
64 | 12 | static TimeLUT time_lut; |
65 | 12 | return time_lut._impl.load(); |
66 | 12 | } |
67 | | |
68 | | protected: |
69 | 1 | TimeLUT() { _impl.store(new TimeLUTImpl()); } |
70 | | |
71 | | private: |
72 | | std::atomic<const TimeLUTImpl*> _impl; |
73 | | }; |
74 | | |
75 | | } // namespace doris |