Coverage Report

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