summaryrefslogtreecommitdiff
path: root/lib/spdlog/fmt/bundled/time.h
blob: 1d4210c86fb2492fdd16a32322b476119a84a71e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 Formatting library for C++ - time formatting

 Copyright (c) 2012 - 2016, Victor Zverovich
 All rights reserved.

 For the license information refer to format.h.
 */

#ifndef FMT_TIME_H_
#define FMT_TIME_H_

#include "format.h"
#include <ctime>

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4702) // unreachable code
#pragma warning(disable : 4996) // "deprecated" functions
#endif

namespace fmt {
template<typename ArgFormatter>
void format_arg(BasicFormatter<char, ArgFormatter> &f, const char *&format_str, const std::tm &tm)
{
    if (*format_str == ':')
        ++format_str;
    const char *end = format_str;
    while (*end && *end != '}')
        ++end;
    if (*end != '}')
        FMT_THROW(FormatError("missing '}' in format string"));
    internal::MemoryBuffer<char, internal::INLINE_BUFFER_SIZE> format;
    format.append(format_str, end + 1);
    format[format.size() - 1] = '\0';
    Buffer<char> &buffer = f.writer().buffer();
    std::size_t start = buffer.size();
    for (;;)
    {
        std::size_t size = buffer.capacity() - start;
        std::size_t count = std::strftime(&buffer[start], size, &format[0], &tm);
        if (count != 0)
        {
            buffer.resize(start + count);
            break;
        }
        if (size >= format.size() * 256)
        {
            // If the buffer is 256 times larger than the format string, assume
            // that `strftime` gives an empty result. There doesn't seem to be a
            // better way to distinguish the two cases:
            // https://github.com/fmtlib/fmt/issues/367
            break;
        }
        const std::size_t MIN_GROWTH = 10;
        buffer.reserve(buffer.capacity() + (size > MIN_GROWTH ? size : MIN_GROWTH));
    }
    format_str = end + 1;
}

namespace internal {
inline Null<> localtime_r(...)
{
    return Null<>();
}
inline Null<> localtime_s(...)
{
    return Null<>();
}
inline Null<> gmtime_r(...)
{
    return Null<>();
}
inline Null<> gmtime_s(...)
{
    return Null<>();
}
} // namespace internal

// Thread-safe replacement for std::localtime
inline std::tm localtime(std::time_t time)
{
    struct LocalTime
    {
        std::time_t time_;
        std::tm tm_;

        LocalTime(std::time_t t)
            : time_(t)
        {
        }

        bool run()
        {
            using namespace fmt::internal;
            return handle(localtime_r(&time_, &tm_));
        }

        bool handle(std::tm *tm)
        {
            return tm != FMT_NULL;
        }

        bool handle(internal::Null<>)
        {
            using namespace fmt::internal;
            return fallback(localtime_s(&tm_, &time_));
        }

        bool fallback(int res)
        {
            return res == 0;
        }

        bool fallback(internal::Null<>)
        {
            using namespace fmt::internal;
            std::tm *tm = std::localtime(&time_);
            if (tm)
                tm_ = *tm;
            return tm != FMT_NULL;
        }
    };
    LocalTime lt(time);
    if (lt.run())
        return lt.tm_;
    // Too big time values may be unsupported.
    FMT_THROW(fmt::FormatError("time_t value out of range"));
    return std::tm();
}

// Thread-safe replacement for std::gmtime
inline std::tm gmtime(std::time_t time)
{
    struct GMTime
    {
        std::time_t time_;
        std::tm tm_;

        GMTime(std::time_t t)
            : time_(t)
        {
        }

        bool run()
        {
            using namespace fmt::internal;
            return handle(gmtime_r(&time_, &tm_));
        }

        bool handle(std::tm *tm)
        {
            return tm != FMT_NULL;
        }

        bool handle(internal::Null<>)
        {
            using namespace fmt::internal;
            return fallback(gmtime_s(&tm_, &time_));
        }

        bool fallback(int res)
        {
            return res == 0;
        }

        bool fallback(internal::Null<>)
        {
            std::tm *tm = std::gmtime(&time_);
            if (tm != FMT_NULL)
                tm_ = *tm;
            return tm != FMT_NULL;
        }
    };
    GMTime gt(time);
    if (gt.run())
        return gt.tm_;
    // Too big time values may be unsupported.
    FMT_THROW(fmt::FormatError("time_t value out of range"));
    return std::tm();
}
} // namespace fmt

#ifdef _MSC_VER
#pragma warning(pop)
#endif

#endif // FMT_TIME_H_