TimeStamp.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 
24 #include "icl_core/os.h"
25 #include "icl_core/TimeStamp.h"
26 
27 #if defined _SYSTEM_LXRT_
28 /*# include "icl_core/tLxrt.h"*/
29 /* TODO: LXRT implementation */
30 #endif
31 
32 #if defined(_IC_BUILDER_BOOST_DATE_TIME_)
33 #include "boost/date_time/posix_time/posix_time.hpp"
34 #include "boost/date_time/posix_time/posix_time_types.hpp"
35 #endif
36 
37 #ifdef _IC_BUILDER_OPENSPLICEDDS_
38 # include <ccpp_dds_dcps.h>
39 # include "icl_core/iTimeStamp.h"
40 #endif
41 
42 namespace icl_core {
43 
44 const TimeStamp TimeStamp::cZERO(0, 0);
45 
46 #ifdef _IC_BUILDER_OPENSPLICEDDS_
47 TimeStamp::TimeStamp(const DDS::Time_t& time_stamp)
48  : TimeBase(time_stamp.sec, time_stamp.nanosec)
49 {
50 }
51 
52 TimeStamp::TimeStamp(const iTimeStamp& time_stamp)
53  : TimeBase(time_stamp.sec, time_stamp.nsec)
54 {
55 }
56 
57 TimeStamp& TimeStamp::operator = (const DDS::Time_t& time_stamp)
58 {
59  secs = time_stamp.sec;
60  nsecs = time_stamp.nanosec;
61  return *this;
62 }
63 
64 TimeStamp& TimeStamp::operator = (const iTimeStamp& time_stamp)
65 {
66  secs = time_stamp.sec;
67  nsecs = time_stamp.nsec;
68  return *this;
69 }
70 
71 TimeStamp::operator DDS::Time_t ()
72 {
73  DDS::Time_t result;
74  result.sec = secs;
75  result.nanosec = nsecs;
76  return result;
77 }
78 
79 TimeStamp::operator iTimeStamp ()
80 {
81  iTimeStamp result;
82  result.sec = secs;
83  result.nsec = nsecs;
84  return result;
85 }
86 #endif
87 
88 #if defined(_IC_BUILDER_BOOST_DATE_TIME_)
89 TimeStamp::TimeStamp(const boost::posix_time::ptime& ptime_stamp)
90 {
91  boost::posix_time::ptime unix_time_base(boost::gregorian::date(1970, 1, 1));
92 
93  secs = boost::posix_time::time_period(unix_time_base, ptime_stamp).length().total_seconds();
94  nsecs = int32_t(boost::posix_time::time_period(unix_time_base, ptime_stamp).
95  length().total_nanoseconds());
96  normalizeTime();
97 }
98 
99 TimeStamp& TimeStamp::operator = (const boost::posix_time::ptime& ptime_stamp)
100 {
101  boost::posix_time::ptime unix_time_base(boost::gregorian::date(1970, 1, 1));
102 
103  secs = boost::posix_time::time_period(unix_time_base, ptime_stamp).length().total_seconds();
104  nsecs = int32_t(boost::posix_time::time_period(unix_time_base, ptime_stamp).
105  length().total_nanoseconds());
106  normalizeTime();
107  return *this;
108 }
109 #endif
110 
112 {
113  struct timespec ts;
114  os::gettimeofday(&ts);
115  return TimeStamp(ts);
116 }
117 
119 {
120  TimeStamp result(msec / 1000, uint32_t((msec % 1000) * 1000000));
121  result += now();
122  return result;
123 }
124 
126 {
127  int32_t tm_sec = 0;
128  int32_t tm_min = 0;
129  int32_t tm_hour = 0;
130  int32_t tm_mday = 1;
131  int32_t tm_mon = 1;
132  int32_t tm_year = 1970;
133  if (str.size() >= 4)
134  {
135  tm_year = boost::lexical_cast<int32_t>(str.substr(0,4));
136  }
137  if (str.size() >= 6)
138  {
139  tm_mon = boost::lexical_cast<int32_t>(str.substr(4,2));
140  }
141  if (str.size() >= 8)
142  {
143  tm_mday = boost::lexical_cast<int32_t>(str.substr(6,2));
144  }
145  // Here comes the 'T', which we ignore and skip
146  if (str.size() >= 11)
147  {
148  tm_hour = boost::lexical_cast<int32_t>(str.substr(9,2));
149  }
150  if (str.size() >= 13)
151  {
152  tm_min = boost::lexical_cast<int32_t>(str.substr(11,2));
153  }
154  if (str.size() >= 15)
155  {
156  tm_sec = boost::lexical_cast<int32_t>(str.substr(13,2));
157  }
158  int32_t nsecs = 0;
159  // Here comes the comma, which we ignore and skip
160  if (str.size() > 16)
161  {
162  std::string nsec_str = (str.substr(16, 9) + "000000000").substr(0, 9);
163  nsecs = boost::lexical_cast<int32_t>(nsec_str);
164  }
165 
166  // Jump to beginning of given year
167  uint64_t days_since_epoch = 0;
168  for (int32_t y = 1970; y < tm_year; ++y)
169  {
170  bool leap_year = (y % 400 == 0
171  || (y % 4 == 0
172  && y % 100 != 0));
173  days_since_epoch += leap_year ? 366 : 365;
174  }
175  // Now add months in that year
176  bool leap_year = (tm_year % 400 == 0
177  || (tm_year % 4 == 0
178  && tm_year % 100 != 0));
179  int32_t days_per_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
180  if (leap_year)
181  {
182  days_per_month[1] = 29;
183  }
184  for (int32_t m = 1; m < tm_mon; ++m)
185  {
186  days_since_epoch += days_per_month[m-1];
187  }
188  // Add day of month
189  days_since_epoch += tm_mday-1;
190  uint64_t secs = 86400 * days_since_epoch + 3600*tm_hour + 60*tm_min + tm_sec;
191 
192  return TimeStamp(secs, nsecs);
193 }
194 
196 {
197  struct timespec ts;
198  os::gettimeofday(&ts);
199  fromTimespec(ts);
200  return *this;
201 }
202 
203 void TimeStamp::strfTime(char* dest, size_t max_len, const char *format) const
204 {
205  time_t time = tsSec();
206  struct tm *newtime;
207  newtime = gmtime(&time);
208  strftime(dest, max_len, format, newtime);
209 }
210 
211 void TimeStamp::strfLocaltime(char* dest, size_t max_len, const char *format) const
212 {
213  time_t time = tsSec();
214  struct tm *newtime = localtime(&time);
215  if (newtime)
216  {
217  strftime(dest, max_len, format, newtime);
218  }
219 }
220 
222 {
223  char date_time_sec[20];
224  strfLocaltime(date_time_sec, 20, "%Y-%m-%d %H:%M:%S");
225  return String(date_time_sec);
226 }
227 
229 {
230  char date_time_sec[20];
231  strfTime(date_time_sec, 20, "%Y-%m-%d %H:%M:%S");
232  return String(date_time_sec);
233 }
234 
236 {
237  char date_time_sec[16], date_time_nsec[10];
238  TimeStamp adjust_nsec(*this);
239  while (adjust_nsec.nsecs < 0)
240  {
241  --adjust_nsec.secs;
242  adjust_nsec.nsecs += 1000000000;
243  }
244  adjust_nsec.strfLocaltime(date_time_sec, 16, "%Y%m%dT%H%M%S");
245  ::icl_core::os::snprintf(date_time_nsec, 10, "%09i", adjust_nsec.tsNSec());
246  return String(date_time_sec) + "," + String(date_time_nsec);
247 }
248 
250 {
251  char date_time_sec[16], date_time_nsec[10];
252  TimeStamp adjust_nsec(*this);
253  while (adjust_nsec.nsecs < 0)
254  {
255  --adjust_nsec.secs;
256  adjust_nsec.nsecs += 1000000000;
257  }
258  adjust_nsec.strfTime(date_time_sec, 16, "%Y%m%dT%H%M%S");
259  ::icl_core::os::snprintf(date_time_nsec, 10, "%09i", adjust_nsec.tsNSec());
260  return String(date_time_sec) + "," + String(date_time_nsec);
261 }
262 
264 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
265 
270  TimeStamp TimeStamp::Now()
271  {
272  return now();
273  }
274 
278  TimeStamp TimeStamp::FutureMSec(uint64_t msec)
279  {
280  return futureMSec(msec);
281  }
282 
286  TimeStamp& TimeStamp::FromNow()
287  {
288  return fromNow();
289  }
290 
291  void TimeStamp::Strftime(char* dest, size_t max_len, const char *format) const
292  {
293  strfTime(dest, max_len, format);
294  }
295 
296  void TimeStamp::StrfLocaltime(char* dest, size_t max_len, const char *format) const
297  {
298  strfLocaltime(dest, max_len, format);
299  }
300 
301  String TimeStamp::FormatIso8601() const
302  {
303  return formatIso8601();
304  }
305 
306  uint64_t TimeStamp::TsSec() const
307  {
308  return tsSec();
309  }
310  uint32_t TimeStamp::TsUSec() const
311  {
312  return tsUSec();
313  }
314  uint32_t TimeStamp::TsNSec() const
315  {
316  return tsNSec();
317  }
318 
319  TimeStamp TimeStamp::MaxTime()
320  {
321  return maxTime();
322  }
323 
324 #endif
325 
328 {
329  secs += other.secs;
330  nsecs += other.nsecs;
331  normalizeTime();
332  return *this;
333 }
334 
335 }
TimeStamp & fromNow()
Set the timestamp to the current system time.
Definition: TimeStamp.cpp:195
signed int int32_t
Definition: msvc_stdint.h:90
unsigned int uint32_t
Definition: msvc_stdint.h:93
Represents absolute times.
Definition: TimeStamp.h:61
static TimeStamp now()
Definition: TimeStamp.cpp:111
String formatIso8601UTC() const
Definition: TimeStamp.cpp:228
uint32_t tsUSec() const
Definition: TimeStamp.h:282
void normalizeTime()
Definition: TimeBase.cpp:83
void strfLocaltime(char *dest, size_t max_len, const char *format) const
Definition: TimeStamp.cpp:211
uint32_t tsNSec() const
Definition: TimeStamp.h:283
unsigned __int64 uint64_t
Definition: msvc_stdint.h:103
String formatIso8601BasicUTC() const
Definition: TimeStamp.cpp:249
static TimeStamp futureMSec(uint64_t msec)
Returns a time stamp which lies msec ms in the future.
Definition: TimeStamp.cpp:118
void strfTime(char *dest, size_t max_len, const char *format) const
Definition: TimeStamp.cpp:203
Contains global functions, encapsulated into the icl_core::os namespace.
std::string String
Definition: BaseTypes.h:43
static const TimeStamp cZERO
Definition: TimeStamp.h:332
uint64_t tsSec() const
Definition: TimeStamp.h:281
static TimeStamp fromIso8601BasicUTC(const String &str)
Definition: TimeStamp.cpp:125
TimeStamp & operator+=(const TimeSpan &span)
Adds a TimeSpan.
Definition: TimeStamp.h:213
String formatIso8601Basic() const
Definition: TimeStamp.cpp:235
Contains TimeStamp.
TimeStamp()
Standard constructor, creates a null time.
Definition: TimeStamp.h:65
void gettimeofday(struct timespec *time)
Definition: os_time.h:41
String formatIso8601() const
Definition: TimeStamp.cpp:221
static TimeStamp maxTime()
Definition: TimeStamp.h:285
void fromTimespec(const struct timespec &time)
Definition: TimeBase.cpp:253
int snprintf(char *buffer, size_t maxlen, const char *format,...)
Definition: os_string.cpp:28


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58