Coverage Report

Created: 2025-12-30 13:48

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
//FIXME: try to remove or refactor all those time input/output functions.
72
0
uint8_t timev2_to_buffer_from_double(double time, char* buffer, int scale) {
73
0
    char* begin = buffer;
74
0
    if (time < 0) {
75
0
        time = -time;
76
0
        *buffer++ = '-';
77
0
    }
78
0
    auto m_time = (uint64_t)TimeValue::limit_with_bound(time);
79
80
0
    auto hour = static_cast<uint16_t>(m_time / (3600ULL * 1000 * 1000));
81
0
    if (hour >= 100) {
82
0
        buffer = fmt::format_to(buffer, FMT_COMPILE("{}"), hour);
83
0
    } else {
84
0
        *buffer++ = (char)('0' + (hour / 10));
85
0
        *buffer++ = (char)('0' + (hour % 10));
86
0
    }
87
0
    *buffer++ = ':';
88
0
    m_time %= 3600ULL * 1000 * 1000;
89
90
0
    auto minute = static_cast<uint8_t>(m_time / (60 * 1000 * 1000));
91
0
    *buffer++ = (char)('0' + (minute / 10));
92
0
    *buffer++ = (char)('0' + (minute % 10));
93
0
    *buffer++ = ':';
94
0
    m_time %= 60 * 1000 * 1000;
95
96
0
    auto second = static_cast<uint8_t>(m_time / (1000 * 1000));
97
0
    *buffer++ = (char)('0' + (second / 10));
98
0
    *buffer++ = (char)('0' + (second % 10));
99
0
    m_time %= 1000 * 1000;
100
0
    if (scale == 0) {
101
0
        return static_cast<uint8_t>(buffer - begin);
102
0
    }
103
104
0
    *buffer++ = '.';
105
0
    memset(buffer, '0', scale);
106
0
    buffer += scale;
107
0
    int32_t micosecond = m_time % (1000 * 1000);
108
0
    micosecond /= common::exp10_i32(6 - scale);
109
0
    auto* it = buffer - 1;
110
0
    while (micosecond) {
111
0
        *it = (char)('0' + (micosecond % 10));
112
0
        micosecond /= 10;
113
0
        it--;
114
0
    }
115
0
    DCHECK_LT(scale, 10);
116
0
    return static_cast<uint8_t>(buffer - begin);
117
0
}
118
119
344
std::string timev2_to_buffer_from_double(double time, int scale) {
120
344
    fmt::memory_buffer buffer;
121
344
    if (time < 0) {
122
0
        time = -time;
123
0
        fmt::format_to(buffer, "-");
124
0
    }
125
344
    auto m_time = TimeValue::limit_with_bound(time);
126
344
    auto hour = TimeValue::hour(m_time);
127
344
    if (hour >= 100) {
128
252
        fmt::format_to(buffer, fmt::format("{}", hour));
129
252
    } else {
130
92
        fmt::format_to(buffer, fmt::format("{:02d}", hour));
131
92
    }
132
344
    auto minute = TimeValue::minute(m_time);
133
344
    auto second = TimeValue::second(m_time);
134
344
    auto micosecond = TimeValue::microsecond(m_time);
135
344
    micosecond /= common::exp10_i32(6 - scale);
136
344
    switch (scale) {
137
118
    case 0:
138
118
        fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}"), minute, second));
139
118
        break;
140
0
    case 1:
141
0
        fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}.{:01d}"), minute, second,
142
0
                                           micosecond));
143
0
        break;
144
0
    case 2:
145
0
        fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}.{:02d}"), minute, second,
146
0
                                           micosecond));
147
0
        break;
148
0
    case 3:
149
0
        fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}.{:03d}"), minute, second,
150
0
                                           micosecond));
151
0
        break;
152
1
    case 4:
153
1
        fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}.{:04d}"), minute, second,
154
1
                                           micosecond));
155
1
        break;
156
112
    case 5:
157
112
        fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}.{:05d}"), minute, second,
158
112
                                           micosecond));
159
112
        break;
160
113
    case 6:
161
113
        fmt::format_to(buffer, fmt::format(FMT_COMPILE(":{:02d}:{:02d}.{:06d}"), minute, second,
162
113
                                           micosecond));
163
113
        break;
164
344
    }
165
166
344
    return fmt::to_string(buffer);
167
344
}
168
#include "common/compile_check_end.h"
169
} // namespace doris