Coverage Report

Created: 2024-11-20 19:28

/root/doris/be/src/util/time.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
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/time.cc
19
// and modified by Doris
20
21
#include "util/time.h"
22
23
// IWYU pragma: no_include <bits/std_abs.h>
24
#include <cmath> // IWYU pragma: keep
25
// IWYU pragma: no_include <bits/chrono.h>
26
#include <chrono> // IWYU pragma: keep
27
#include <cstdlib>
28
#include <iomanip>
29
#include <ratio>
30
#include <sstream>
31
#include <thread>
32
33
#include "common/logging.h"
34
35
using namespace doris;
36
using namespace std::chrono;
37
38
1.00k
void doris::SleepForMs(const int64_t duration_ms) {
39
1.00k
    std::this_thread::sleep_for(milliseconds(duration_ms));
40
1.00k
}
41
42
// Convert the given time_point, 't', into a date-time string in the
43
// UTC time zone if 'utc' is true, or the local time zone if it is false.
44
// The returned string is of the form yyy-MM-dd HH::mm::SS.
45
0
static std::string TimepointToString(const system_clock::time_point& t, bool utc) {
46
0
    char buf[256];
47
0
    struct tm tmp;
48
0
    auto input_time = system_clock::to_time_t(t);
49
50
    // gcc 4.9 does not support C++14 get_time and put_time functions, so we're
51
    // stuck with strftime() for now.
52
0
    if (utc) {
53
0
        strftime(buf, sizeof(buf), "%F %T", gmtime_r(&input_time, &tmp));
54
0
    } else {
55
0
        strftime(buf, sizeof(buf), "%F %T", localtime_r(&input_time, &tmp));
56
0
    }
57
0
    return std::string(buf);
58
0
}
59
60
// Format the sub-second part of the input time point object 't', at the
61
// precision specified by 'p'. The returned string is meant to be appended to
62
// the string returned by TimePointToString() above.
63
// Note the use of abs(). This is to make sure we correctly format negative times,
64
// i.e., times before the Unix epoch.
65
0
static std::string FormatSubSecond(const system_clock::time_point& t, TimePrecision p) {
66
0
    std::stringstream ss;
67
0
    auto frac = t.time_since_epoch();
68
0
    if (p == TimePrecision::Millisecond) {
69
0
        auto subsec = duration_cast<milliseconds>(frac) % MILLIS_PER_SEC;
70
0
        ss << "." << std::setfill('0') << std::setw(3) << abs(subsec.count());
71
0
    } else if (p == TimePrecision::Microsecond) {
72
0
        auto subsec = duration_cast<microseconds>(frac) % MICROS_PER_SEC;
73
0
        ss << "." << std::setfill('0') << std::setw(6) << abs(subsec.count());
74
0
    } else if (p == TimePrecision::Nanosecond) {
75
0
        auto subsec = duration_cast<nanoseconds>(frac) % NANOS_PER_SEC;
76
0
        ss << "." << std::setfill('0') << std::setw(9) << abs(subsec.count());
77
0
    } else {
78
        // 1-second precision or unknown unit. Return empty string.
79
0
        DCHECK_EQ(TimePrecision::Second, p);
80
0
        ss << "";
81
0
    }
82
0
    return ss.str();
83
0
}
84
85
// Convert time point 't' into date-time string at precision 'p'.
86
// Output string is in UTC time zone if 'utc' is true, else it is in the
87
// local time zone.
88
0
static std::string ToString(const system_clock::time_point& t, TimePrecision p, bool utc) {
89
0
    std::stringstream ss;
90
0
    ss << TimepointToString(t, utc);
91
0
    ss << FormatSubSecond(t, p);
92
0
    return ss.str();
93
0
}
94
95
// Convenience function to convert Unix time, specified as seconds since
96
// the Unix epoch, into a C++ time_point object.
97
0
static system_clock::time_point TimepointFromUnix(int64_t s) {
98
0
    return system_clock::time_point(seconds(s));
99
0
}
100
101
// Convenience function to convert Unix time, specified as milliseconds since
102
// the Unix epoch, into a C++ time_point object.
103
0
static system_clock::time_point TimepointFromUnixMillis(int64_t ms) {
104
0
    return system_clock::time_point(milliseconds(ms));
105
0
}
106
107
// Convenience function to convert Unix time, specified as microseconds since
108
// the Unix epoch, into a C++ time_point object.
109
0
static system_clock::time_point TimepointFromUnixMicros(int64_t us) {
110
0
    return system_clock::time_point(microseconds(us));
111
0
}
112
113
0
std::string doris::ToStringFromUnix(int64_t s, TimePrecision p) {
114
0
    system_clock::time_point t = TimepointFromUnix(s);
115
0
    return ToString(t, p, false);
116
0
}
117
118
0
std::string doris::ToUtcStringFromUnix(int64_t s, TimePrecision p) {
119
0
    system_clock::time_point t = TimepointFromUnix(s);
120
0
    return ToString(t, p, true);
121
0
}
122
123
0
std::string doris::ToStringFromUnixMillis(int64_t ms, TimePrecision p) {
124
0
    system_clock::time_point t = TimepointFromUnixMillis(ms);
125
0
    return ToString(t, p, false);
126
0
}
127
128
0
std::string doris::ToUtcStringFromUnixMillis(int64_t ms, TimePrecision p) {
129
0
    system_clock::time_point t = TimepointFromUnixMillis(ms);
130
0
    return ToString(t, p, true);
131
0
}
132
133
0
std::string doris::ToStringFromUnixMicros(int64_t us, TimePrecision p) {
134
0
    system_clock::time_point t = TimepointFromUnixMicros(us);
135
0
    return ToString(t, p, false);
136
0
}
137
138
0
std::string doris::ToUtcStringFromUnixMicros(int64_t us, TimePrecision p) {
139
0
    system_clock::time_point t = TimepointFromUnixMicros(us);
140
0
    return ToString(t, p, true);
141
0
}