timestamp.cpp 5.98 KB
Newer Older
Axel Auweter's avatar
Axel Auweter committed
1
2
3
4
5
6
7
8
9
10
11
/*
 * dcdbtimestamp.cpp
 *
 *  Created on: Feb 18, 2015
 *      Author: Axel Auweter
 */

#include <cstring>
#include <cinttypes>

#include "boost/date_time/posix_time/posix_time.hpp"
12
13
#include "boost/date_time/local_time/local_time.hpp"
#include "boost/date_time/c_local_time_adjustor.hpp"
14
#include "boost/regex.hpp"
Axel Auweter's avatar
Axel Auweter committed
15
16
17

#include "timestamp.h"

18
19
/* Use this as our local to utc adjustor */
typedef boost::date_time::c_local_adjustor<boost::posix_time::ptime> local_adj;
Axel Auweter's avatar
Axel Auweter committed
20
21
22
23
24
25

/**
 * This function parses a string and tries to do a best guess at the contained
 * time information. Currently, it detects strings in the format "yyyy-mm-dd hh:mm:ss.000"
 * and posix time.
 */
26
void DCDBTimeStamp::guessFromString(std::string timestr, bool localTime)
Axel Auweter's avatar
Axel Auweter committed
27
28
{
  boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
29
  uint64_t tmp;
Axel Auweter's avatar
Axel Auweter committed
30
31
32
33
34
35
36

  /* First try to match it against a time string */
  try {
      boost::posix_time::ptime ts(boost::posix_time::time_from_string(timestr));
      if (ts != boost::posix_time::not_a_date_time) {
          boost::posix_time::time_duration diff = ts - epoch;
          raw = diff.total_nanoseconds();
37
38
39
          if (localTime) {
              convertFromLocal();
          }
Axel Auweter's avatar
Axel Auweter committed
40
41
42
43
44
45
46
          return;
      }
  }
  catch (std::exception& e) {
      /* Ignore on error */
  }

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  /*
   * Try to match it against a string containing "now".
   * Note that we ignore the localTime flag in this case since
   * we already get "now" in UTC.
   */
  if (timestr.find("now") != std::string::npos) {

      /* If the string is just "now"... */
      if ((timestr.compare("now") == 0) && (timestr.length() == 3)) {
          setNow();
          return;
      }

      /* If the string is of the form "now-X" */
      boost::regex d("now-[0-9]*d", boost::regex::extended);
      boost::regex h("now-[0-9]*h", boost::regex::extended);
      boost::regex m("now-[0-9]*m", boost::regex::extended);
      boost::regex s("now-[0-9]*s", boost::regex::extended);

      if (boost::regex_match(timestr, d)) {
          sscanf(timestr.c_str(), "now-%" PRIu64, &tmp);
          setNow();
          raw -= tmp * 24 * 60 * 60 * 1000 * 1000 * 1000;
          return;
      }
      if (boost::regex_match(timestr, h)) {
          sscanf(timestr.c_str(), "now-%" PRIu64, &tmp);
          setNow();
          raw -= tmp * 60 * 60 * 1000 * 1000 * 1000;
          return;
      }
      if (boost::regex_match(timestr, m)) {
          sscanf(timestr.c_str(), "now-%" PRIu64, &tmp);
          setNow();
          raw -= tmp * 1000 * 1000 * 1000;
          return;
      }
      if (boost::regex_match(timestr, s)) {
          sscanf(timestr.c_str(), "now-%" PRIu64, &tmp);
          setNow();
          raw -= tmp * 1000 * 1000 * 1000;
          return;
      }

      /* "now" keyword is in the timestamp but does not match one of the predefined formats */
      throw DCDBTimeStampConversionException();
  }

Axel Auweter's avatar
Axel Auweter committed
95
96
97
  /* Try to match it against a POSIX time */
  if (sscanf(timestr.c_str(), "%" PRIu64, &tmp) == 1) {
      raw = tmp * 1000 * 1000 * 1000;
98
99
100
      if (localTime) {
          convertFromLocal();
      }
Axel Auweter's avatar
Axel Auweter committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
      return;
  }

  /* Not successful - throw exception */
  throw DCDBTimeStampConversionException();
}

/**
 * This constructor is implemented by calling setNow().
 */
DCDBTimeStamp::DCDBTimeStamp()
{
  setNow();
}

/**
 * This constructor sets the internal raw value directly to the supplied argument.
 */
DCDBTimeStamp::DCDBTimeStamp(uint64_t ts)
{
  raw = ts;
}

/**
 * This constructor sets the time using the magic implemented in guessFromString.
 */
127
DCDBTimeStamp::DCDBTimeStamp(std::string ts, bool localTime)
Axel Auweter's avatar
Axel Auweter committed
128
{
129
  guessFromString(ts, localTime);
Axel Auweter's avatar
Axel Auweter committed
130
131
}

132
133
134
135
136
137
138
139
140
141
142
143
/**
 * This constructor sets the from a time_t struct.
 */
DCDBTimeStamp::DCDBTimeStamp(time_t ts)
{
  boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
  boost::posix_time::ptime t =  boost::posix_time::from_time_t(ts);
  boost::posix_time::time_duration diff = t - epoch;

  raw = diff.total_nanoseconds();
}

Axel Auweter's avatar
Axel Auweter committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
 * Currently, the destructor doesn't need to do anything.
 */
DCDBTimeStamp::~DCDBTimeStamp()
{
}

/**
 * This function sets the value of raw to the nanoseconds since epoch
 * using some magic help of boost::posix_time.
 */
void DCDBTimeStamp::setNow(void)
{
  boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
  boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
  boost::posix_time::time_duration diff = now - epoch;

  raw = diff.total_nanoseconds();
}

164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
 *
 */
void DCDBTimeStamp::convertFromLocal()
{
  /*
   * Unfortunately BOOST c_local_adjustor doesn't support local-to-utc conversion.
   * Therfore, we first check the offset of "raw" during utc-to-local conversion
   * and then apply this offset in the other direction.
   */

  /* Construct ptime object t from raw */
  boost::posix_time::ptime t(boost::gregorian::date(1970,1,1));
  t += boost::posix_time::nanoseconds(raw);

  /* Convert t to UTC */
  boost::posix_time::ptime t2 = local_adj::utc_to_local(t);

  /* Check the difference of the two times */
  boost::posix_time::time_duration diff = t - t2;

  /* Write back adjusted value to raw */
  raw += diff.total_nanoseconds();
}

/**
 *
 */
void DCDBTimeStamp::convertToLocal()
{
  /* Construct ptime object t from raw */
  boost::posix_time::ptime t(boost::gregorian::date(1970,1,1));
  t += boost::posix_time::nanoseconds(raw);

  /* Convert t to Local */
  t = local_adj::utc_to_local(t);

  /* Write back to raw */
  boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
  boost::posix_time::time_duration diff = t - epoch;
  raw = diff.total_nanoseconds();
}

Axel Auweter's avatar
Axel Auweter committed
207
208
209
210
211
212
213
214
/**
 *
 */
uint64_t DCDBTimeStamp::getRaw(void)
{
  return raw;
}

215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
 *
 */
std::string DCDBTimeStamp::getString(void)
{
#ifndef BOOST_DATE_TIME_HAS_NANOSECONDS
#error Needs nanoseconds support in boost.
#endif
  boost::posix_time::ptime t(boost::gregorian::date(1970,1,1));
  t += boost::posix_time::nanoseconds(raw);
  return boost::posix_time::to_iso_extended_string(t);
}


229
230
231
232
233
/**
 *
 */
uint16_t DCDBTimeStamp::getWeekstamp(void)
{
234
  uint16_t week = raw / 604800000000000;
235
236
  return week;
}
Axel Auweter's avatar
Axel Auweter committed
237