/root/doris/be/src/util/date_func.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 | | #include "util/date_func.h" |
19 | | |
20 | | #include <fmt/compile.h> |
21 | | #include <fmt/format.h> |
22 | | #include <glog/logging.h> |
23 | | #include <string.h> |
24 | | #include <time.h> |
25 | | |
26 | | #include <ostream> |
27 | | |
28 | | #include "vec/runtime/vdatetime_value.h" |
29 | | |
30 | | namespace doris { |
31 | | |
32 | 7 | VecDateTimeValue timestamp_from_datetime(const std::string& datetime_str) { |
33 | 7 | tm time_tm; |
34 | 7 | char* res = strptime(datetime_str.c_str(), "%Y-%m-%d %H:%M:%S", &time_tm); |
35 | | |
36 | 7 | uint64_t value = 0; |
37 | 7 | if (nullptr != res) { |
38 | 6 | value = ((time_tm.tm_year + 1900) * 10000L + (time_tm.tm_mon + 1) * 100L + |
39 | 6 | time_tm.tm_mday) * |
40 | 6 | 1000000L + |
41 | 6 | time_tm.tm_hour * 10000L + time_tm.tm_min * 100L + time_tm.tm_sec; |
42 | 6 | } else { |
43 | | // 1400 - 01 - 01 |
44 | 1 | value = 14000101000000; |
45 | 1 | } |
46 | | |
47 | 7 | return VecDateTimeValue::create_from_olap_datetime(value); |
48 | 7 | } |
49 | | |
50 | 7 | VecDateTimeValue timestamp_from_date(const std::string& date_str) { |
51 | 7 | tm time_tm; |
52 | 7 | char* res = strptime(date_str.c_str(), "%Y-%m-%d", &time_tm); |
53 | | |
54 | 7 | uint32_t value = 0; |
55 | 7 | if (nullptr != res) { |
56 | 6 | value = (uint32_t)((time_tm.tm_year + 1900) * 16 * 32 + (time_tm.tm_mon + 1) * 32 + |
57 | 6 | time_tm.tm_mday); |
58 | 6 | } else { |
59 | 1 | LOG(WARNING) << "Invalid date string: " << date_str; |
60 | | // 1400 - 01 - 01 |
61 | 1 | value = 716833; |
62 | 1 | } |
63 | | |
64 | 7 | return VecDateTimeValue::create_from_olap_date(value); |
65 | 7 | } |
66 | | |
67 | 0 | DateV2Value<DateV2ValueType> timestamp_from_date_v2(const std::string& date_str) { |
68 | 0 | tm time_tm; |
69 | 0 | char* res = strptime(date_str.c_str(), "%Y-%m-%d", &time_tm); |
70 | |
|
71 | 0 | uint32_t value = 0; |
72 | 0 | if (nullptr != res) { |
73 | 0 | value = ((time_tm.tm_year + 1900) << 9) | ((time_tm.tm_mon + 1) << 5) | time_tm.tm_mday; |
74 | 0 | } else { |
75 | 0 | value = MIN_DATE_V2; |
76 | 0 | } |
77 | |
|
78 | 0 | return DateV2Value<DateV2ValueType>::create_from_olap_date(value); |
79 | 0 | } |
80 | | |
81 | 0 | DateV2Value<DateTimeV2ValueType> timestamp_from_datetime_v2(const std::string& date_str) { |
82 | 0 | DateV2Value<DateTimeV2ValueType> val; |
83 | 0 | std::string date_format = "%Y-%m-%d %H:%i:%s.%f"; |
84 | 0 | val.from_date_format_str(date_format.data(), date_format.size(), date_str.data(), |
85 | 0 | date_str.size()); |
86 | 0 | return val; |
87 | 0 | } |
88 | | // refer to https://dev.mysql.com/doc/refman/5.7/en/time.html |
89 | | // the time value between '-838:59:59' and '838:59:59' |
90 | | /// TODO: Why is the time type stored as double? Can we directly use int64 and remove the time limit? |
91 | 10 | int32_t time_to_buffer_from_double(double time, char* buffer) { |
92 | 10 | char* begin = buffer; |
93 | 10 | if (time < 0) { |
94 | 4 | time = -time; |
95 | 4 | *buffer++ = '-'; |
96 | 4 | } |
97 | 10 | if (time > 3020399) { |
98 | 3 | time = 3020399; |
99 | 3 | } |
100 | 10 | int64_t hour = (int64_t)(time / 3600); |
101 | 10 | if (hour >= 100) { |
102 | 4 | buffer = fmt::format_to(buffer, FMT_COMPILE("{}"), hour); |
103 | 6 | } else { |
104 | 6 | *buffer++ = (char)('0' + (hour / 10)); |
105 | 6 | *buffer++ = (char)('0' + (hour % 10)); |
106 | 6 | } |
107 | 10 | *buffer++ = ':'; |
108 | 10 | int32_t minute = ((int32_t)(time / 60)) % 60; |
109 | 10 | *buffer++ = (char)('0' + (minute / 10)); |
110 | 10 | *buffer++ = (char)('0' + (minute % 10)); |
111 | 10 | *buffer++ = ':'; |
112 | 10 | int32_t second = ((int32_t)time) % 60; |
113 | 10 | *buffer++ = (char)('0' + (second / 10)); |
114 | 10 | *buffer++ = (char)('0' + (second % 10)); |
115 | 10 | return buffer - begin; |
116 | 10 | } |
117 | | |
118 | 0 | int64_t check_over_max_time(int64_t time) { |
119 | 0 | const static int64_t max_time = (int64_t)3020399 * 1000 * 1000; |
120 | 0 | if (time > max_time) { |
121 | 0 | return max_time; |
122 | 0 | } |
123 | 0 | return time; |
124 | 0 | } |
125 | 0 | int32_t timev2_to_buffer_from_double(double time, char* buffer, int scale) { |
126 | 0 | static int pow10[7] = {1, 10, 100, 1000, 10000, 100000, 1000000}; |
127 | |
|
128 | 0 | char* begin = buffer; |
129 | 0 | if (time < 0) { |
130 | 0 | time = -time; |
131 | 0 | *buffer++ = '-'; |
132 | 0 | } |
133 | 0 | auto m_time = int64_t(time); |
134 | | // m_time = hour * 3600 * 1000 * 1000 + minute * 60 * 1000 * 1000 + second * 1000 * 1000 + microsecond |
135 | 0 | m_time = check_over_max_time(m_time); |
136 | 0 | int64_t hour = m_time / ((int64_t)3600 * 1000 * 1000); |
137 | 0 | if (hour >= 100) { |
138 | 0 | buffer = fmt::format_to(buffer, FMT_COMPILE("{}"), hour); |
139 | 0 | } else { |
140 | 0 | *buffer++ = (char)('0' + (hour / 10)); |
141 | 0 | *buffer++ = (char)('0' + (hour % 10)); |
142 | 0 | } |
143 | 0 | *buffer++ = ':'; |
144 | 0 | m_time %= (int64_t)3600 * 1000 * 1000; |
145 | 0 | int64_t minute = m_time / (60 * 1000 * 1000); |
146 | 0 | *buffer++ = (char)('0' + (minute / 10)); |
147 | 0 | *buffer++ = (char)('0' + (minute % 10)); |
148 | 0 | *buffer++ = ':'; |
149 | 0 | m_time %= 60 * 1000 * 1000; |
150 | 0 | int32_t second = m_time / (1000 * 1000); |
151 | 0 | *buffer++ = (char)('0' + (second / 10)); |
152 | 0 | *buffer++ = (char)('0' + (second % 10)); |
153 | 0 | m_time %= 1000 * 1000; |
154 | 0 | if (scale == 0) { |
155 | 0 | return buffer - begin; |
156 | 0 | } |
157 | 0 | *buffer++ = '.'; |
158 | 0 | memset(buffer, '0', scale); |
159 | 0 | buffer += scale; |
160 | 0 | int32_t micosecond = m_time % (1000 * 1000); |
161 | 0 | micosecond /= pow10[6 - scale]; |
162 | 0 | auto it = buffer - 1; |
163 | 0 | while (micosecond) { |
164 | 0 | *it = (char)('0' + (micosecond % 10)); |
165 | 0 | micosecond /= 10; |
166 | 0 | it--; |
167 | 0 | } |
168 | 0 | return buffer - begin; |
169 | 0 | } |
170 | | |
171 | 0 | std::string time_to_buffer_from_double(double time) { |
172 | 0 | fmt::memory_buffer buffer; |
173 | 0 | if (time < 0) { |
174 | 0 | time = -time; |
175 | 0 | fmt::format_to(buffer, "-"); |
176 | 0 | } |
177 | 0 | if (time > 3020399) { |
178 | 0 | time = 3020399; |
179 | 0 | } |
180 | 0 | int64_t hour = (int64_t)(time / 3600); |
181 | 0 | int32_t minute = ((int32_t)(time / 60)) % 60; |
182 | 0 | int32_t second = ((int32_t)time) % 60; |
183 | 0 | if (hour >= 100) { |
184 | 0 | fmt::format_to(buffer, fmt::format("{}", hour)); |
185 | 0 | } else { |
186 | 0 | fmt::format_to(buffer, fmt::format("{:02d}", hour)); |
187 | 0 | } |
188 | 0 | fmt::format_to(buffer, fmt::format(":{:02d}:{:02d}", minute, second)); |
189 | 0 | return fmt::to_string(buffer); |
190 | 0 | } |
191 | | |
192 | 0 | std::string timev2_to_buffer_from_double(double time, int scale) { |
193 | 0 | static int pow10[7] = {1, 10, 100, 1000, 10000, 100000, 1000000}; |
194 | 0 | fmt::memory_buffer buffer; |
195 | 0 | if (time < 0) { |
196 | 0 | time = -time; |
197 | 0 | fmt::format_to(buffer, "-"); |
198 | 0 | } |
199 | 0 | auto m_time = int64_t(time); |
200 | 0 | m_time = check_over_max_time(m_time); |
201 | | // m_time = hour * 3600 * 1000 * 1000 + minute * 60 * 1000 * 1000 + second * 1000 * 1000 + microsecond |
202 | 0 | int64_t hour = m_time / ((int64_t)3600 * 1000 * 1000); |
203 | 0 | if (hour >= 100) { |
204 | 0 | fmt::format_to(buffer, fmt::format("{}", hour)); |
205 | 0 | } else { |
206 | 0 | fmt::format_to(buffer, fmt::format("{:02d}", hour)); |
207 | 0 | } |
208 | 0 | m_time %= (int64_t)3600 * 1000 * 1000; |
209 | 0 | int64_t minute = m_time / (60 * 1000 * 1000); |
210 | 0 | m_time %= 60 * 1000 * 1000; |
211 | 0 | int32_t second = m_time / (1000 * 1000); |
212 | 0 | int32_t micosecond = m_time % (1000 * 1000); |
213 | 0 | micosecond /= pow10[6 - scale]; |
214 | 0 | switch (scale) { |
215 | 0 | case 0: |
216 | 0 | fmt::format_to(buffer, fmt::format(":{:02d}:{:02d}", minute, second, micosecond)); |
217 | 0 | break; |
218 | 0 | case 1: |
219 | 0 | fmt::format_to(buffer, fmt::format(":{:02d}:{:02d}.{:01d}", minute, second, micosecond)); |
220 | 0 | break; |
221 | 0 | case 2: |
222 | 0 | fmt::format_to(buffer, fmt::format(":{:02d}:{:02d}.{:02d}", minute, second, micosecond)); |
223 | 0 | break; |
224 | 0 | case 3: |
225 | 0 | fmt::format_to(buffer, fmt::format(":{:02d}:{:02d}.{:03d}", minute, second, micosecond)); |
226 | 0 | break; |
227 | 0 | case 4: |
228 | 0 | fmt::format_to(buffer, fmt::format(":{:02d}:{:02d}.{:04d}", minute, second, micosecond)); |
229 | 0 | break; |
230 | 0 | case 5: |
231 | 0 | fmt::format_to(buffer, fmt::format(":{:02d}:{:02d}.{:05d}", minute, second, micosecond)); |
232 | 0 | break; |
233 | 0 | case 6: |
234 | 0 | fmt::format_to(buffer, fmt::format(":{:02d}:{:02d}.{:06d}", minute, second, micosecond)); |
235 | 0 | break; |
236 | 0 | } |
237 | | |
238 | 0 | return fmt::to_string(buffer); |
239 | 0 | } |
240 | | } // namespace doris |