MongoDB C++ Driver legacy-1.1.2
Loading...
Searching...
No Matches
time_support.h
1// @file time_support.h
2
3/* Copyright 2010 10gen Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#pragma once
19
20#include <iosfwd>
21#include <ctime>
22#include <string>
23#include <boost/date_time/posix_time/posix_time_types.hpp>
24#include <boost/thread/xtime.hpp>
25#include <boost/version.hpp>
26
27#include "mongo/base/status_with.h"
28#include "mongo/client/export_macros.h"
29
30namespace mongo {
31
32typedef boost::posix_time::milliseconds Milliseconds;
33typedef boost::posix_time::seconds Seconds;
34
35void time_t_to_Struct(time_t t, struct tm* buf, bool local = false);
36std::string time_t_to_String(time_t t);
37std::string time_t_to_String_short(time_t t);
38
39struct MONGO_CLIENT_API Date_t {
40 // TODO: make signed (and look for related TODO's)
41 unsigned long long millis;
42 Date_t() : millis(0) {}
43 Date_t(unsigned long long m) : millis(m) {}
44 operator unsigned long long&() {
45 return millis;
46 }
47 operator const unsigned long long&() const {
48 return millis;
49 }
50 void toTm(tm* buf);
51 std::string toString() const;
52 time_t toTimeT() const;
53 int64_t asInt64() const {
54 return static_cast<int64_t>(millis);
55 }
56 bool isFormatable() const;
57};
58
59// uses ISO 8601 dates without trailing Z
60// colonsOk should be false when creating filenames
61MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC terseCurrentTime(bool colonsOk = true);
62
69MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC timeToISOString(time_t time);
70
77MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC dateToISOStringUTC(Date_t date);
78
85MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC dateToISOStringLocal(Date_t date);
86
92MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC dateToCtimeString(Date_t date);
93
102MONGO_CLIENT_API StatusWith<Date_t> MONGO_CLIENT_FUNC
103dateFromISOString(const StringData& dateString);
104
108MONGO_CLIENT_API void MONGO_CLIENT_FUNC outputDateAsISOStringUTC(std::ostream& os, Date_t date);
109
113MONGO_CLIENT_API void MONGO_CLIENT_FUNC outputDateAsISOStringLocal(std::ostream& os, Date_t date);
114
118MONGO_CLIENT_API void MONGO_CLIENT_FUNC outputDateAsCtime(std::ostream& os, Date_t date);
119
120boost::gregorian::date currentDate();
121
122// parses time of day in "hh:mm" format assuming 'hh' is 00-23
123bool toPointInTime(const std::string& str, boost::posix_time::ptime* timeOfDay);
124
125MONGO_CLIENT_API void MONGO_CLIENT_FUNC sleepsecs(int s);
126MONGO_CLIENT_API void MONGO_CLIENT_FUNC sleepmillis(long long ms);
127MONGO_CLIENT_API void MONGO_CLIENT_FUNC sleepmicros(long long micros);
128
129class Backoff {
130public:
131 Backoff(int maxSleepMillis, int resetAfter)
132 : _maxSleepMillis(maxSleepMillis),
133 _resetAfterMillis(maxSleepMillis + resetAfter), // Don't reset < the max sleep
134 _lastSleepMillis(0),
135 _lastErrorTimeMillis(0) {}
136
137 void nextSleepMillis();
138
142 int getNextSleepMillis(int lastSleepMillis,
143 unsigned long long currTimeMillis,
144 unsigned long long lastErrorTimeMillis) const;
145
146private:
147 // Parameters
148 int _maxSleepMillis;
149 int _resetAfterMillis;
150
151 // Last sleep information
152 int _lastSleepMillis;
153 unsigned long long _lastErrorTimeMillis;
154};
155
156// DO NOT TOUCH except for testing
157void jsTimeVirtualSkew(long long skew);
158
159void jsTimeVirtualThreadSkew(long long skew);
160long long getJSTimeVirtualThreadSkew();
161
163MONGO_CLIENT_API Date_t MONGO_CLIENT_FUNC jsTime();
164
166unsigned curTimeMicros();
167unsigned long long curTimeMicros64();
168unsigned long long curTimeMillis64();
169
170// these are so that if you use one of them compilation will fail
171char* asctime(const struct tm* tm);
172char* ctime(const time_t* timep);
173struct tm* gmtime(const time_t* timep);
174struct tm* localtime(const time_t* timep);
175
176#if (BOOST_VERSION >= 105000)
177#define MONGO_BOOST_TIME_UTC boost::TIME_UTC_
178#else
179#define MONGO_BOOST_TIME_UTC boost::TIME_UTC
180#endif
181
182} // namespace mongo
Definition time_support.h:129
int getNextSleepMillis(int lastSleepMillis, unsigned long long currTimeMillis, unsigned long long lastErrorTimeMillis) const
testing-only function.
Definition status_with.h:43
A StringData object wraps a 'const string&' or a 'const char*' without copying its contents.
Definition string_data.h:43
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:20
MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC timeToISOString(time_t time)
Formats "time" according to the ISO 8601 extended form standard, including date, and time,...
MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC dateToCtimeString(Date_t date)
Formats "date" in fixed width in the local time zone.
MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC dateToISOStringLocal(Date_t date)
Formats "date" according to the ISO 8601 extended form standard, including date, and time with millis...
MONGO_CLIENT_API void MONGO_CLIENT_FUNC outputDateAsISOStringUTC(std::ostream &os, Date_t date)
Like dateToISOStringUTC, except outputs to a std::ostream.
MONGO_CLIENT_API StatusWith< Date_t > MONGO_CLIENT_FUNC dateFromISOString(const StringData &dateString)
Parses a Date_t from an ISO 8601 string representation.
MONGO_CLIENT_API Date_t MONGO_CLIENT_FUNC jsTime()
Date_t is milliseconds since epoch.
MONGO_CLIENT_API void MONGO_CLIENT_FUNC outputDateAsISOStringLocal(std::ostream &os, Date_t date)
Like dateToISOStringLocal, except outputs to a std::ostream.
unsigned curTimeMicros()
warning this will wrap
MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC dateToISOStringUTC(Date_t date)
Formats "date" according to the ISO 8601 extended form standard, including date, and time with millis...
MONGO_CLIENT_API void MONGO_CLIENT_FUNC outputDateAsCtime(std::ostream &os, Date_t date)
Like dateToCtimeString, except outputs to a std::ostream.
Definition time_support.h:39