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