Coverage Report

Created: 2025-04-11 13:52

/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
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
// 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
421
int64_t check_over_max_time(int64_t time) {
119
    // refer to https://dev.mysql.com/doc/refman/5.7/en/time.html
120
    // the time value between '-838:59:59' and '838:59:59'
121
421
    const static int64_t max_time = (int64_t)3020399 * 1000 * 1000;
122
421
    if (time > max_time) {
123
0
        return max_time;
124
0
    }
125
421
    return time;
126
421
}
127
84
int32_t timev2_to_buffer_from_double(double time, char* buffer, int scale) {
128
84
    static int pow10[7] = {1, 10, 100, 1000, 10000, 100000, 1000000};
129
130
84
    char* begin = buffer;
131
84
    if (time < 0) {
132
0
        time = -time;
133
0
        *buffer++ = '-';
134
0
    }
135
84
    auto m_time = int64_t(time);
136
    // m_time = hour * 3600 * 1000 * 1000 + minute * 60 * 1000 * 1000 + second * 1000 * 1000 + microsecond
137
84
    m_time = check_over_max_time(m_time);
138
84
    int64_t hour = m_time / ((int64_t)3600 * 1000 * 1000);
139
84
    if (hour >= 100) {
140
63
        buffer = fmt::format_to(buffer, FMT_COMPILE("{}"), hour);
141
63
    } else {
142
21
        *buffer++ = (char)('0' + (hour / 10));
143
21
        *buffer++ = (char)('0' + (hour % 10));
144
21
    }
145
84
    *buffer++ = ':';
146
84
    m_time %= (int64_t)3600 * 1000 * 1000;
147
84
    int64_t minute = m_time / (60 * 1000 * 1000);
148
84
    *buffer++ = (char)('0' + (minute / 10));
149
84
    *buffer++ = (char)('0' + (minute % 10));
150
84
    *buffer++ = ':';
151
84
    m_time %= 60 * 1000 * 1000;
152
84
    int32_t second = m_time / (1000 * 1000);
153
84
    *buffer++ = (char)('0' + (second / 10));
154
84
    *buffer++ = (char)('0' + (second % 10));
155
84
    m_time %= 1000 * 1000;
156
84
    if (scale == 0) {
157
28
        return buffer - begin;
158
28
    }
159
56
    *buffer++ = '.';
160
56
    memset(buffer, '0', scale);
161
56
    buffer += scale;
162
56
    int32_t micosecond = m_time % (1000 * 1000);
163
56
    micosecond /= pow10[6 - scale];
164
56
    auto it = buffer - 1;
165
281
    while (micosecond) {
166
225
        *it = (char)('0' + (micosecond % 10));
167
225
        micosecond /= 10;
168
225
        it--;
169
225
    }
170
56
    return buffer - begin;
171
84
}
172
173
0
std::string time_to_buffer_from_double(double time) {
174
0
    fmt::memory_buffer buffer;
175
0
    if (time < 0) {
176
0
        time = -time;
177
0
        fmt::format_to(buffer, "-");
178
0
    }
179
0
    if (time > 3020399) {
180
0
        time = 3020399;
181
0
    }
182
0
    int64_t hour = (int64_t)(time / 3600);
183
0
    int32_t minute = ((int32_t)(time / 60)) % 60;
184
0
    int32_t second = ((int32_t)time) % 60;
185
0
    if (hour >= 100) {
186
0
        fmt::format_to(buffer, fmt::format("{}", hour));
187
0
    } else {
188
0
        fmt::format_to(buffer, fmt::format("{:02d}", hour));
189
0
    }
190
0
    fmt::format_to(buffer, fmt::format(":{:02d}:{:02d}", minute, second));
191
0
    return fmt::to_string(buffer);
192
0
}
193
194
337
std::string timev2_to_buffer_from_double(double time, int scale) {
195
337
    static int pow10[7] = {1, 10, 100, 1000, 10000, 100000, 1000000};
196
337
    fmt::memory_buffer buffer;
197
337
    if (time < 0) {
198
0
        time = -time;
199
0
        fmt::format_to(buffer, "-");
200
0
    }
201
337
    auto m_time = int64_t(time);
202
337
    m_time = check_over_max_time(m_time);
203
    // m_time = hour * 3600 * 1000 * 1000 + minute * 60 * 1000 * 1000 + second * 1000 * 1000 + microsecond
204
337
    int64_t hour = m_time / ((int64_t)3600 * 1000 * 1000);
205
337
    if (hour >= 100) {
206
252
        fmt::format_to(buffer, fmt::format("{}", hour));
207
252
    } else {
208
85
        fmt::format_to(buffer, fmt::format("{:02d}", hour));
209
85
    }
210
337
    m_time %= (int64_t)3600 * 1000 * 1000;
211
337
    int64_t minute = m_time / (60 * 1000 * 1000);
212
337
    m_time %= 60 * 1000 * 1000;
213
337
    int32_t second = m_time / (1000 * 1000);
214
337
    int32_t micosecond = m_time % (1000 * 1000);
215
337
    micosecond /= pow10[6 - scale];
216
337
    switch (scale) {
217
113
    case 0:
218
113
        fmt::format_to(buffer, fmt::format(":{:02d}:{:02d}", minute, second));
219
113
        break;
220
0
    case 1:
221
0
        fmt::format_to(buffer, fmt::format(":{:02d}:{:02d}.{:01d}", minute, second, micosecond));
222
0
        break;
223
0
    case 2:
224
0
        fmt::format_to(buffer, fmt::format(":{:02d}:{:02d}.{:02d}", minute, second, micosecond));
225
0
        break;
226
0
    case 3:
227
0
        fmt::format_to(buffer, fmt::format(":{:02d}:{:02d}.{:03d}", minute, second, micosecond));
228
0
        break;
229
0
    case 4:
230
0
        fmt::format_to(buffer, fmt::format(":{:02d}:{:02d}.{:04d}", minute, second, micosecond));
231
0
        break;
232
112
    case 5:
233
112
        fmt::format_to(buffer, fmt::format(":{:02d}:{:02d}.{:05d}", minute, second, micosecond));
234
112
        break;
235
112
    case 6:
236
112
        fmt::format_to(buffer, fmt::format(":{:02d}:{:02d}.{:06d}", minute, second, micosecond));
237
112
        break;
238
337
    }
239
240
337
    return fmt::to_string(buffer);
241
337
}
242
} // namespace doris