/root/doris/be/src/util/date_func.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 | | #include "util/date_func.h" |
19 | | |
20 | | #include <fmt/compile.h> |
21 | | #include <fmt/format.h> |
22 | | #include <glog/logging.h> |
23 | | |
24 | | #include <cstring> |
25 | | #include <ctime> |
26 | | |
27 | | #include "vec/runtime/time_value.h" |
28 | | #include "vec/runtime/vdatetime_value.h" |
29 | | |
30 | | namespace doris { |
31 | | |
32 | 18 | VecDateTimeValue timestamp_from_datetime(const std::string& datetime_str) { |
33 | 18 | tm time_tm; |
34 | 18 | char* res = strptime(datetime_str.c_str(), "%Y-%m-%d %H:%M:%S", &time_tm); |
35 | | |
36 | 18 | uint64_t value = 0; |
37 | 18 | if (nullptr != res) { |
38 | 17 | value = ((time_tm.tm_year + 1900) * 10000L + (time_tm.tm_mon + 1) * 100L + |
39 | 17 | time_tm.tm_mday) * |
40 | 17 | 1000000L + |
41 | 17 | time_tm.tm_hour * 10000L + time_tm.tm_min * 100L + time_tm.tm_sec; |
42 | 17 | } else { |
43 | | // 1400 - 01 - 01 |
44 | 1 | value = 14000101000000; |
45 | 1 | } |
46 | | |
47 | 18 | return VecDateTimeValue::create_from_olap_datetime(value); |
48 | 18 | } |
49 | | |
50 | 18 | VecDateTimeValue timestamp_from_date(const std::string& date_str) { |
51 | 18 | tm time_tm; |
52 | 18 | char* res = strptime(date_str.c_str(), "%Y-%m-%d", &time_tm); |
53 | | |
54 | 18 | uint32_t value = 0; |
55 | 18 | if (nullptr != res) { |
56 | 17 | value = (uint32_t)((time_tm.tm_year + 1900) * 16 * 32 + (time_tm.tm_mon + 1) * 32 + |
57 | 17 | time_tm.tm_mday); |
58 | 17 | } else { |
59 | 1 | LOG(WARNING) << "Invalid date string: " << date_str; |
60 | | // 1400 - 01 - 01 |
61 | 1 | value = 716833; |
62 | 1 | } |
63 | | |
64 | 18 | return VecDateTimeValue::create_from_olap_date(value); |
65 | 18 | } |
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 | | |
89 | | //FIXME: try to remove or refactor all those time input/output functions. |
90 | 86 | int32_t timev2_to_buffer_from_double(double time, char* buffer, int scale) { |
91 | 86 | static int pow10[7] = {1, 10, 100, 1000, 10000, 100000, 1000000}; |
92 | | |
93 | 86 | char* begin = buffer; |
94 | 86 | if (time < 0) { |
95 | 0 | time = -time; |
96 | 0 | *buffer++ = '-'; |
97 | 0 | } |
98 | 86 | auto m_time = (int64_t)TimeValue::limit_with_bound(time); |
99 | 86 | int64_t hour = m_time / ((int64_t)3600 * 1000 * 1000); |
100 | 86 | if (hour >= 100) { |
101 | 63 | buffer = fmt::format_to(buffer, FMT_COMPILE("{}"), hour); |
102 | 63 | } else { |
103 | 23 | *buffer++ = (char)('0' + (hour / 10)); |
104 | 23 | *buffer++ = (char)('0' + (hour % 10)); |
105 | 23 | } |
106 | 86 | *buffer++ = ':'; |
107 | 86 | m_time %= (int64_t)3600 * 1000 * 1000; |
108 | 86 | int64_t minute = m_time / (60 * 1000 * 1000); |
109 | 86 | *buffer++ = (char)('0' + (minute / 10)); |
110 | 86 | *buffer++ = (char)('0' + (minute % 10)); |
111 | 86 | *buffer++ = ':'; |
112 | 86 | m_time %= 60 * 1000 * 1000; |
113 | 86 | int32_t second = m_time / (1000 * 1000); |
114 | 86 | *buffer++ = (char)('0' + (second / 10)); |
115 | 86 | *buffer++ = (char)('0' + (second % 10)); |
116 | 86 | m_time %= 1000 * 1000; |
117 | 86 | if (scale == 0) { |
118 | 28 | return buffer - begin; |
119 | 28 | } |
120 | 58 | *buffer++ = '.'; |
121 | 58 | memset(buffer, '0', scale); |
122 | 58 | buffer += scale; |
123 | 58 | int32_t micosecond = m_time % (1000 * 1000); |
124 | 58 | micosecond /= pow10[6 - scale]; |
125 | 58 | auto* it = buffer - 1; |
126 | 283 | while (micosecond) { |
127 | 225 | *it = (char)('0' + (micosecond % 10)); |
128 | 225 | micosecond /= 10; |
129 | 225 | it--; |
130 | 225 | } |
131 | 58 | return buffer - begin; |
132 | 86 | } |
133 | | |
134 | 338 | std::string timev2_to_buffer_from_double(double time, int scale) { |
135 | 338 | fmt::memory_buffer buffer; |
136 | 338 | if (time < 0) { |
137 | 0 | time = -time; |
138 | 0 | fmt::format_to(buffer, "-"); |
139 | 0 | } |
140 | 338 | auto m_time = TimeValue::limit_with_bound(time); |
141 | 338 | auto hour = TimeValue::hour(m_time); |
142 | 338 | if (hour >= 100) { |
143 | 252 | fmt::format_to(buffer, fmt::format("{}", hour)); |
144 | 252 | } else { |
145 | 86 | fmt::format_to(buffer, fmt::format("{:02d}", hour)); |
146 | 86 | } |
147 | 338 | auto minute = TimeValue::minute(m_time); |
148 | 338 | auto second = TimeValue::second(m_time); |
149 | 338 | auto micosecond = TimeValue::microsecond(m_time); |
150 | 338 | micosecond /= common::exp10_i32(6 - scale); |
151 | 338 | switch (scale) { |
152 | 113 | case 0: |
153 | 113 | fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}"), minute, second)); |
154 | 113 | break; |
155 | 0 | case 1: |
156 | 0 | fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}.{:01d}"), minute, second, |
157 | 0 | micosecond)); |
158 | 0 | break; |
159 | 0 | case 2: |
160 | 0 | fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}.{:02d}"), minute, second, |
161 | 0 | micosecond)); |
162 | 0 | break; |
163 | 0 | case 3: |
164 | 0 | fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}.{:03d}"), minute, second, |
165 | 0 | micosecond)); |
166 | 0 | break; |
167 | 1 | case 4: |
168 | 1 | fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}.{:04d}"), minute, second, |
169 | 1 | micosecond)); |
170 | 1 | break; |
171 | 112 | case 5: |
172 | 112 | fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}.{:05d}"), minute, second, |
173 | 112 | micosecond)); |
174 | 112 | break; |
175 | 112 | case 6: |
176 | 112 | fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}.{:06d}"), minute, second, |
177 | 112 | micosecond)); |
178 | 112 | break; |
179 | 338 | } |
180 | | |
181 | 338 | return fmt::to_string(buffer); |
182 | 338 | } |
183 | | } // namespace doris |