MongoDB  2.5.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/date_time/posix_time/posix_time.hpp>
24 
25 #include "mongo/bson/util/misc.h" // Date_t
26 
27 namespace mongo {
28 
29  void time_t_to_Struct(time_t t, struct tm * buf , bool local = false );
30 
39  void curTimeString(char* timeStr);
40 
41  // uses ISO 8601 dates without trailing Z
42  // colonsOk should be false when creating filenames
43  std::string terseCurrentTime(bool colonsOk=true);
44 
45  std::string timeToISOString(time_t time);
46 
47  boost::gregorian::date currentDate();
48 
49  // parses time of day in "hh:mm" format assuming 'hh' is 00-23
50  bool toPointInTime( const std::string& str , boost::posix_time::ptime* timeOfDay );
51 
52  void sleepsecs(int s);
53  void sleepmillis(long long ms);
54  void sleepmicros(long long micros);
55 
56  class Backoff {
57  public:
58 
59  Backoff( int maxSleepMillis, int resetAfter ) :
60  _maxSleepMillis( maxSleepMillis ),
61  _resetAfterMillis( maxSleepMillis + resetAfter ), // Don't reset < the max sleep
62  _lastSleepMillis( 0 ),
63  _lastErrorTimeMillis( 0 )
64  {}
65 
66  void nextSleepMillis();
67 
68  private:
69 
70  // Parameters
71  int _maxSleepMillis;
72  int _resetAfterMillis;
73 
74  // Last sleep information
75  int _lastSleepMillis;
76  unsigned long long _lastErrorTimeMillis;
77  };
78 
79  // DO NOT TOUCH except for testing
80  void jsTimeVirtualSkew( long long skew );
81 
82  void jsTimeVirtualThreadSkew( long long skew );
83  long long getJSTimeVirtualThreadSkew();
84 
86  Date_t jsTime();
87 
89  unsigned curTimeMicros();
90  unsigned long long curTimeMicros64();
91  unsigned long long curTimeMillis64();
92 
93  // these are so that if you use one of them compilation will fail
94  char *asctime(const struct tm *tm);
95  char *ctime(const time_t *timep);
96  struct tm *gmtime(const time_t *timep);
97  struct tm *localtime(const time_t *timep);
98 
99 #if defined(MONGO_BOOST_TIME_UTC_HACK) || (BOOST_VERSION >= 105000)
100 #define MONGO_BOOST_TIME_UTC boost::TIME_UTC_
101 #else
102 #define MONGO_BOOST_TIME_UTC boost::TIME_UTC
103 #endif
104 
105 } // namespace mongo