Coverage Report

Created: 2025-09-15 22:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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 "common/cast_set.h"
28
#include "vec/common/int_exp.h"
29
#include "vec/runtime/time_value.h"
30
#include "vec/runtime/vdatetime_value.h"
31
32
namespace doris {
33
#include "common/compile_check_begin.h"
34
18
VecDateTimeValue timestamp_from_datetime(const std::string& datetime_str) {
35
18
    tm time_tm;
36
18
    char* res = strptime(datetime_str.c_str(), "%Y-%m-%d %H:%M:%S", &time_tm);
37
38
18
    uint64_t value = 0;
39
18
    if (nullptr != res) {
40
17
        value = ((time_tm.tm_year + 1900) * 10000L + (time_tm.tm_mon + 1) * 100L +
41
17
                 time_tm.tm_mday) *
42
17
                        1000000L +
43
17
                time_tm.tm_hour * 10000L + time_tm.tm_min * 100L + time_tm.tm_sec;
44
17
    } else {
45
        // 1400 - 01 - 01
46
1
        value = 14000101000000;
47
1
    }
48
49
18
    return VecDateTimeValue::create_from_olap_datetime(value);
50
18
}
51
52
18
VecDateTimeValue timestamp_from_date(const std::string& date_str) {
53
18
    tm time_tm;
54
18
    char* res = strptime(date_str.c_str(), "%Y-%m-%d", &time_tm);
55
56
18
    uint32_t value = 0;
57
18
    if (nullptr != res) {
58
17
        value = (uint32_t)((time_tm.tm_year + 1900) * 16 * 32 + (time_tm.tm_mon + 1) * 32 +
59
17
                           time_tm.tm_mday);
60
17
    } else {
61
1
        LOG(WARNING) << "Invalid date string: " << date_str;
62
        // 1400 - 01 - 01
63
1
        value = 716833;
64
1
    }
65
66
18
    return VecDateTimeValue::create_from_olap_date(value);
67
18
}
68
69
0
DateV2Value<DateV2ValueType> timestamp_from_date_v2(const std::string& date_str) {
70
0
    tm time_tm;
71
0
    char* res = strptime(date_str.c_str(), "%Y-%m-%d", &time_tm);
72
73
0
    uint32_t value = 0;
74
0
    if (nullptr != res) {
75
0
        value = ((time_tm.tm_year + 1900) << 9) | ((time_tm.tm_mon + 1) << 5) | time_tm.tm_mday;
76
0
    } else {
77
0
        value = MIN_DATE_V2;
78
0
    }
79
80
0
    return DateV2Value<DateV2ValueType>::create_from_olap_date(value);
81
0
}
82
83
0
DateV2Value<DateTimeV2ValueType> timestamp_from_datetime_v2(const std::string& date_str) {
84
0
    DateV2Value<DateTimeV2ValueType> val;
85
0
    std::string date_format = "%Y-%m-%d %H:%i:%s.%f";
86
0
    val.from_date_format_str(date_format.data(), date_format.size(), date_str.data(),
87
0
                             date_str.size());
88
0
    return val;
89
0
}
90
91
//FIXME: try to remove or refactor all those time input/output functions.
92
86
uint8_t timev2_to_buffer_from_double(double time, char* buffer, int scale) {
93
86
    char* begin = buffer;
94
86
    if (time < 0) {
95
0
        time = -time;
96
0
        *buffer++ = '-';
97
0
    }
98
86
    auto m_time = (uint64_t)TimeValue::limit_with_bound(time);
99
100
86
    auto hour = static_cast<uint16_t>(m_time / (3600ULL * 1000 * 1000));
101
86
    if (hour >= 100) {
102
63
        buffer = fmt::format_to(buffer, FMT_COMPILE("{}"), hour);
103
63
    } else {
104
23
        *buffer++ = (char)('0' + (hour / 10));
105
23
        *buffer++ = (char)('0' + (hour % 10));
106
23
    }
107
86
    *buffer++ = ':';
108
86
    m_time %= 3600ULL * 1000 * 1000;
109
110
86
    auto minute = static_cast<uint8_t>(m_time / (60 * 1000 * 1000));
111
86
    *buffer++ = (char)('0' + (minute / 10));
112
86
    *buffer++ = (char)('0' + (minute % 10));
113
86
    *buffer++ = ':';
114
86
    m_time %= 60 * 1000 * 1000;
115
116
86
    auto second = static_cast<uint8_t>(m_time / (1000 * 1000));
117
86
    *buffer++ = (char)('0' + (second / 10));
118
86
    *buffer++ = (char)('0' + (second % 10));
119
86
    m_time %= 1000 * 1000;
120
86
    if (scale == 0) {
121
28
        return static_cast<uint8_t>(buffer - begin);
122
28
    }
123
124
58
    *buffer++ = '.';
125
58
    memset(buffer, '0', scale);
126
58
    buffer += scale;
127
58
    int32_t micosecond = m_time % (1000 * 1000);
128
58
    micosecond /= common::exp10_i32(6 - scale);
129
58
    auto* it = buffer - 1;
130
283
    while (micosecond) {
131
225
        *it = (char)('0' + (micosecond % 10));
132
225
        micosecond /= 10;
133
225
        it--;
134
225
    }
135
58
    DCHECK_LT(scale, 10);
136
58
    return static_cast<uint8_t>(buffer - begin);
137
86
}
138
139
344
std::string timev2_to_buffer_from_double(double time, int scale) {
140
344
    fmt::memory_buffer buffer;
141
344
    if (time < 0) {
142
0
        time = -time;
143
0
        fmt::format_to(buffer, "-");
144
0
    }
145
344
    auto m_time = TimeValue::limit_with_bound(time);
146
344
    auto hour = TimeValue::hour(m_time);
147
344
    if (hour >= 100) {
148
252
        fmt::format_to(buffer, fmt::format("{}", hour));
149
252
    } else {
150
92
        fmt::format_to(buffer, fmt::format("{:02d}", hour));
151
92
    }
152
344
    auto minute = TimeValue::minute(m_time);
153
344
    auto second = TimeValue::second(m_time);
154
344
    auto micosecond = TimeValue::microsecond(m_time);
155
344
    micosecond /= common::exp10_i32(6 - scale);
156
344
    switch (scale) {
157
118
    case 0:
158
118
        fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}"), minute, second));
159
118
        break;
160
0
    case 1:
161
0
        fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}.{:01d}"), minute, second,
162
0
                                           micosecond));
163
0
        break;
164
0
    case 2:
165
0
        fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}.{:02d}"), minute, second,
166
0
                                           micosecond));
167
0
        break;
168
0
    case 3:
169
0
        fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}.{:03d}"), minute, second,
170
0
                                           micosecond));
171
0
        break;
172
1
    case 4:
173
1
        fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}.{:04d}"), minute, second,
174
1
                                           micosecond));
175
1
        break;
176
112
    case 5:
177
112
        fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}.{:05d}"), minute, second,
178
112
                                           micosecond));
179
112
        break;
180
113
    case 6:
181
113
        fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}.{:06d}"), minute, second,
182
113
                                           micosecond));
183
113
        break;
184
344
    }
185
186
344
    return fmt::to_string(buffer);
187
344
}
188
#include "common/compile_check_end.h"
189
} // namespace doris