Coverage Report

Created: 2026-04-11 13:34

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