MongoDB  2.7.0
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/thread/xtime.hpp>
24 #include <boost/version.hpp>
25 
26 #include "mongo/base/status_with.h"
27 #include "mongo/client/export_macros.h"
28 
29 namespace mongo {
30 
31  void time_t_to_Struct(time_t t, struct tm * buf , bool local = false );
32  std::string time_t_to_String(time_t t);
33  std::string time_t_to_String_short(time_t t);
34 
35  struct Date_t {
36  // TODO: make signed (and look for related TODO's)
37  unsigned long long millis;
38  Date_t(): millis(0) {}
39  Date_t(unsigned long long m): millis(m) {}
40  operator unsigned long long&() { return millis; }
41  operator const unsigned long long&() const { return millis; }
42  void toTm (tm *buf);
43  std::string toString() const;
44  time_t toTimeT() const;
45  int64_t asInt64() const {
46  return static_cast<int64_t>(millis);
47  }
48  };
49 
50  // uses ISO 8601 dates without trailing Z
51  // colonsOk should be false when creating filenames
52  std::string terseCurrentTime(bool colonsOk=true);
53 
60  std::string timeToISOString(time_t time);
61 
68  std::string dateToISOStringUTC(Date_t date);
69 
76  std::string dateToISOStringLocal(Date_t date);
77 
83  std::string dateToCtimeString(Date_t date);
84 
93  StatusWith<Date_t> dateFromISOString(const StringData& dateString);
94 
98  void outputDateAsISOStringUTC(std::ostream& os, Date_t date);
99 
103  void outputDateAsISOStringLocal(std::ostream& os, Date_t date);
104 
108  void outputDateAsCtime(std::ostream& os, Date_t date);
109 
110  boost::gregorian::date currentDate();
111 
112  // parses time of day in "hh:mm" format assuming 'hh' is 00-23
113  bool toPointInTime( const std::string& str , boost::posix_time::ptime* timeOfDay );
114 
115  MONGO_CLIENT_API void sleepsecs(int s);
116  MONGO_CLIENT_API void sleepmillis(long long ms);
117  MONGO_CLIENT_API void sleepmicros(long long micros);
118 
119  class Backoff {
120  public:
121 
122  Backoff( int maxSleepMillis, int resetAfter ) :
123  _maxSleepMillis( maxSleepMillis ),
124  _resetAfterMillis( maxSleepMillis + resetAfter ), // Don't reset < the max sleep
125  _lastSleepMillis( 0 ),
126  _lastErrorTimeMillis( 0 )
127  {}
128 
129  void nextSleepMillis();
130 
134  int getNextSleepMillis(int lastSleepMillis, unsigned long long currTimeMillis,
135  unsigned long long lastErrorTimeMillis) const;
136 
137  private:
138 
139  // Parameters
140  int _maxSleepMillis;
141  int _resetAfterMillis;
142 
143  // Last sleep information
144  int _lastSleepMillis;
145  unsigned long long _lastErrorTimeMillis;
146  };
147 
148  // DO NOT TOUCH except for testing
149  void jsTimeVirtualSkew( long long skew );
150 
151  void jsTimeVirtualThreadSkew( long long skew );
152  long long getJSTimeVirtualThreadSkew();
153 
155  Date_t jsTime();
156 
158  unsigned curTimeMicros();
159  unsigned long long curTimeMicros64();
160  unsigned long long curTimeMillis64();
161 
162  // these are so that if you use one of them compilation will fail
163  char *asctime(const struct tm *tm);
164  char *ctime(const time_t *timep);
165  struct tm *gmtime(const time_t *timep);
166  struct tm *localtime(const time_t *timep);
167 
168 #if defined(MONGO_BOOST_TIME_UTC_HACK) || (BOOST_VERSION >= 105000)
169 #define MONGO_BOOST_TIME_UTC boost::TIME_UTC_
170 #else
171 #define MONGO_BOOST_TIME_UTC boost::TIME_UTC
172 #endif
173 
174 } // namespace mongo
175 
void outputDateAsCtime(std::ostream &os, Date_t date)
Like dateToCtimeString, except outputs to a std::ostream.
Definition: time_support.cpp:213
StatusWith< Date_t > dateFromISOString(const StringData &dateString)
Parses a Date_t from an ISO 8601 string representation.
Definition: time_support.cpp:597
Definition: time_support.h:35
std::string dateToCtimeString(Date_t date)
Formats "date" in fixed width in the local time zone.
Definition: time_support.cpp:194
void outputDateAsISOStringUTC(std::ostream &os, Date_t date)
Like dateToISOStringUTC, except outputs to a std::ostream.
Definition: time_support.cpp:200
Definition: time_support.h:119
int getNextSleepMillis(int lastSleepMillis, unsigned long long currTimeMillis, unsigned long long lastErrorTimeMillis) const
testing-only function.
Definition: time_support.cpp:804
void outputDateAsISOStringLocal(std::ostream &os, Date_t date)
Like dateToISOStringLocal, except outputs to a std::ostream.
Definition: time_support.cpp:206
std::string dateToISOStringUTC(Date_t date)
Formats "date" according to the ISO 8601 extended form standard, including date, and time with millis...
Definition: time_support.cpp:182
Date_t jsTime()
Date_t is milliseconds since epoch.
Definition: time_support.cpp:955
unsigned curTimeMicros()
warning this will wrap
Definition: time_support.cpp:966
string timeToISOString(time_t time)
Formats "time" according to the ISO 8601 extended form standard, including date, and time...
Definition: time_support.cpp:103
std::string dateToISOStringLocal(Date_t date)
Formats "date" according to the ISO 8601 extended form standard, including date, and time with millis...
Definition: time_support.cpp:188