MongoDB C++ Driver legacy-1.0.1
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
32 typedef boost::posix_time::milliseconds Milliseconds;
33 typedef boost::posix_time::seconds Seconds;
34
35 void time_t_to_Struct(time_t t, struct tm * buf , bool local = false );
36 std::string time_t_to_String(time_t t);
37 std::string time_t_to_String_short(time_t t);
38
39 struct 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&() { return millis; }
45 operator const unsigned long long&() const { return millis; }
46 void toTm (tm *buf);
47 std::string toString() const;
48 time_t toTimeT() const;
49 int64_t asInt64() const {
50 return static_cast<int64_t>(millis);
51 }
52 bool isFormatable() const;
53 };
54
55 // uses ISO 8601 dates without trailing Z
56 // colonsOk should be false when creating filenames
57 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC terseCurrentTime(bool colonsOk=true);
58
65 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC timeToISOString(time_t time);
66
73 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC dateToISOStringUTC(Date_t date);
74
81 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC dateToISOStringLocal(Date_t date);
82
88 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC dateToCtimeString(Date_t date);
89
98 MONGO_CLIENT_API StatusWith<Date_t> MONGO_CLIENT_FUNC dateFromISOString(const StringData& dateString);
99
103 MONGO_CLIENT_API void MONGO_CLIENT_FUNC outputDateAsISOStringUTC(std::ostream& os, Date_t date);
104
108 MONGO_CLIENT_API void MONGO_CLIENT_FUNC outputDateAsISOStringLocal(std::ostream& os, Date_t date);
109
113 MONGO_CLIENT_API void MONGO_CLIENT_FUNC outputDateAsCtime(std::ostream& os, Date_t date);
114
115 boost::gregorian::date currentDate();
116
117 // parses time of day in "hh:mm" format assuming 'hh' is 00-23
118 bool toPointInTime( const std::string& str , boost::posix_time::ptime* timeOfDay );
119
120 MONGO_CLIENT_API void MONGO_CLIENT_FUNC sleepsecs(int s);
121 MONGO_CLIENT_API void MONGO_CLIENT_FUNC sleepmillis(long long ms);
122 MONGO_CLIENT_API void MONGO_CLIENT_FUNC sleepmicros(long long micros);
123
124 class Backoff {
125 public:
126
127 Backoff( int maxSleepMillis, int resetAfter ) :
128 _maxSleepMillis( maxSleepMillis ),
129 _resetAfterMillis( maxSleepMillis + resetAfter ), // Don't reset < the max sleep
130 _lastSleepMillis( 0 ),
131 _lastErrorTimeMillis( 0 )
132 {}
133
134 void nextSleepMillis();
135
139 int getNextSleepMillis(int lastSleepMillis, unsigned long long currTimeMillis,
140 unsigned long long lastErrorTimeMillis) const;
141
142 private:
143
144 // Parameters
145 int _maxSleepMillis;
146 int _resetAfterMillis;
147
148 // Last sleep information
149 int _lastSleepMillis;
150 unsigned long long _lastErrorTimeMillis;
151 };
152
153 // DO NOT TOUCH except for testing
154 void jsTimeVirtualSkew( long long skew );
155
156 void jsTimeVirtualThreadSkew( long long skew );
157 long long getJSTimeVirtualThreadSkew();
158
160 MONGO_CLIENT_API Date_t MONGO_CLIENT_FUNC jsTime();
161
163 unsigned curTimeMicros();
164 unsigned long long curTimeMicros64();
165 unsigned long long curTimeMillis64();
166
167 // these are so that if you use one of them compilation will fail
168 char *asctime(const struct tm *tm);
169 char *ctime(const time_t *timep);
170 struct tm *gmtime(const time_t *timep);
171 struct tm *localtime(const time_t *timep);
172
173#if (BOOST_VERSION >= 105000)
174#define MONGO_BOOST_TIME_UTC boost::TIME_UTC_
175#else
176#define MONGO_BOOST_TIME_UTC boost::TIME_UTC
177#endif
178
179} // namespace mongo
180
Definition time_support.h:124
int getNextSleepMillis(int lastSleepMillis, unsigned long long currTimeMillis, unsigned long long lastErrorTimeMillis) const
testing-only function.
Definition sasl_client_conversation.h:27
the main MongoDB namespace
Definition bulk_operation_builder.h:24
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