TimeStamp.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2014 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00022 //----------------------------------------------------------------------
00023 
00024 #include "icl_core/os.h"
00025 #include "icl_core/TimeStamp.h"
00026 
00027 #if defined _SYSTEM_LXRT_
00028 /*# include "icl_core/tLxrt.h"*/
00029 /* TODO: LXRT implementation */
00030 #endif
00031 
00032 #if defined(_IC_BUILDER_BOOST_DATE_TIME_)
00033 #include "boost/date_time/posix_time/posix_time.hpp"
00034 #include "boost/date_time/posix_time/posix_time_types.hpp"
00035 #endif
00036 
00037 #ifdef _IC_BUILDER_OPENSPLICEDDS_
00038 # include <ccpp_dds_dcps.h>
00039 # include "icl_core/iTimeStamp.h"
00040 #endif
00041 
00042 namespace icl_core {
00043 
00044 const TimeStamp TimeStamp::cZERO(0, 0);
00045 
00046 #ifdef _IC_BUILDER_OPENSPLICEDDS_
00047 TimeStamp::TimeStamp(const DDS::Time_t& time_stamp)
00048   : TimeBase(time_stamp.sec, time_stamp.nanosec)
00049 {
00050 }
00051 
00052 TimeStamp::TimeStamp(const iTimeStamp& time_stamp)
00053   : TimeBase(time_stamp.sec, time_stamp.nsec)
00054 {
00055 }
00056 
00057 TimeStamp& TimeStamp::operator = (const DDS::Time_t& time_stamp)
00058 {
00059   secs = time_stamp.sec;
00060   nsecs = time_stamp.nanosec;
00061   return *this;
00062 }
00063 
00064 TimeStamp& TimeStamp::operator = (const iTimeStamp& time_stamp)
00065 {
00066   secs = time_stamp.sec;
00067   nsecs = time_stamp.nsec;
00068   return *this;
00069 }
00070 
00071 TimeStamp::operator DDS::Time_t ()
00072 {
00073   DDS::Time_t result;
00074   result.sec = secs;
00075   result.nanosec = nsecs;
00076   return result;
00077 }
00078 
00079 TimeStamp::operator iTimeStamp ()
00080 {
00081   iTimeStamp result;
00082   result.sec = secs;
00083   result.nsec = nsecs;
00084   return result;
00085 }
00086 #endif
00087 
00088 #if defined(_IC_BUILDER_BOOST_DATE_TIME_)
00089 TimeStamp::TimeStamp(const boost::posix_time::ptime& ptime_stamp)
00090 {
00091   boost::posix_time::ptime unix_time_base(boost::gregorian::date(1970, 1, 1));
00092 
00093   secs = boost::posix_time::time_period(unix_time_base, ptime_stamp).length().total_seconds();
00094   nsecs = int32_t(boost::posix_time::time_period(unix_time_base, ptime_stamp).
00095   length().total_nanoseconds());
00096   normalizeTime();
00097 }
00098 
00099 TimeStamp& TimeStamp::operator = (const boost::posix_time::ptime& ptime_stamp)
00100 {
00101   boost::posix_time::ptime unix_time_base(boost::gregorian::date(1970, 1, 1));
00102 
00103   secs = boost::posix_time::time_period(unix_time_base, ptime_stamp).length().total_seconds();
00104   nsecs = int32_t(boost::posix_time::time_period(unix_time_base, ptime_stamp).
00105   length().total_nanoseconds());
00106   normalizeTime();
00107   return *this;
00108 }
00109 #endif
00110 
00111 TimeStamp TimeStamp::now()
00112 {
00113   struct timespec ts;
00114   os::gettimeofday(&ts);
00115   return TimeStamp(ts);
00116 }
00117 
00118 TimeStamp TimeStamp::futureMSec(uint64_t msec)
00119 {
00120   TimeStamp result(msec / 1000, uint32_t((msec % 1000) * 1000000));
00121   result += now();
00122   return result;
00123 }
00124 
00125 TimeStamp TimeStamp::fromIso8601BasicUTC(const String& str)
00126 {
00127   int32_t tm_sec = 0;
00128   int32_t tm_min = 0;
00129   int32_t tm_hour = 0;
00130   int32_t tm_mday = 1;
00131   int32_t tm_mon = 1;
00132   int32_t tm_year = 1970;
00133   if (str.size() >= 4)
00134   {
00135     tm_year = boost::lexical_cast<int32_t>(str.substr(0,4));
00136   }
00137   if (str.size() >= 6)
00138   {
00139     tm_mon = boost::lexical_cast<int32_t>(str.substr(4,2));
00140   }
00141   if (str.size() >= 8)
00142   {
00143     tm_mday = boost::lexical_cast<int32_t>(str.substr(6,2));
00144   }
00145   // Here comes the 'T', which we ignore and skip
00146   if (str.size() >= 11)
00147   {
00148     tm_hour = boost::lexical_cast<int32_t>(str.substr(9,2));
00149   }
00150   if (str.size() >= 13)
00151   {
00152     tm_min = boost::lexical_cast<int32_t>(str.substr(11,2));
00153   }
00154   if (str.size() >= 15)
00155   {
00156     tm_sec = boost::lexical_cast<int32_t>(str.substr(13,2));
00157   }
00158   int32_t nsecs = 0;
00159   // Here comes the comma, which we ignore and skip
00160   if (str.size() > 16)
00161   {
00162     std::string nsec_str = (str.substr(16, 9) + "000000000").substr(0, 9);
00163     nsecs = boost::lexical_cast<int32_t>(nsec_str);
00164   }
00165 
00166   // Jump to beginning of given year
00167   uint64_t days_since_epoch = 0;
00168   for (int32_t y = 1970; y < tm_year; ++y)
00169   {
00170     bool leap_year = (y % 400 == 0
00171                       || (y % 4 == 0
00172                           && y % 100 != 0));
00173     days_since_epoch += leap_year ? 366 : 365;
00174   }
00175   // Now add months in that year
00176   bool leap_year = (tm_year % 400 == 0
00177                     || (tm_year % 4 == 0
00178                         && tm_year % 100 != 0));
00179   int32_t days_per_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
00180   if (leap_year)
00181   {
00182     days_per_month[1] = 29;
00183   }
00184   for (int32_t m = 1; m < tm_mon; ++m)
00185   {
00186     days_since_epoch += days_per_month[m-1];
00187   }
00188   // Add day of month
00189   days_since_epoch += tm_mday-1;
00190   uint64_t secs = 86400 * days_since_epoch + 3600*tm_hour + 60*tm_min + tm_sec;
00191 
00192   return TimeStamp(secs, nsecs);
00193 }
00194 
00195 TimeStamp& TimeStamp::fromNow()
00196 {
00197   struct timespec ts;
00198   os::gettimeofday(&ts);
00199   fromTimespec(ts);
00200   return *this;
00201 }
00202 
00203 void TimeStamp::strfTime(char* dest, size_t max_len, const char *format) const
00204 {
00205   time_t time = tsSec();
00206   struct tm *newtime;
00207   newtime = gmtime(&time);
00208   strftime(dest, max_len, format, newtime);
00209 }
00210 
00211 void TimeStamp::strfLocaltime(char* dest, size_t max_len, const char *format) const
00212 {
00213   time_t time = tsSec();
00214   struct tm *newtime = localtime(&time);
00215   if (newtime)
00216   {
00217     strftime(dest, max_len, format, newtime);
00218   }
00219 }
00220 
00221 String TimeStamp::formatIso8601() const
00222 {
00223   char date_time_sec[20];
00224   strfLocaltime(date_time_sec, 20, "%Y-%m-%d %H:%M:%S");
00225   return String(date_time_sec);
00226 }
00227 
00228 String TimeStamp::formatIso8601UTC() const
00229 {
00230   char date_time_sec[20];
00231   strfTime(date_time_sec, 20, "%Y-%m-%d %H:%M:%S");
00232   return String(date_time_sec);
00233 }
00234 
00235 String TimeStamp::formatIso8601Basic() const
00236 {
00237   char date_time_sec[16], date_time_nsec[10];
00238   TimeStamp adjust_nsec(*this);
00239   while (adjust_nsec.nsecs < 0)
00240   {
00241     --adjust_nsec.secs;
00242     adjust_nsec.nsecs += 1000000000;
00243   }
00244   adjust_nsec.strfLocaltime(date_time_sec, 16, "%Y%m%dT%H%M%S");
00245   ::icl_core::os::snprintf(date_time_nsec, 10, "%09i", adjust_nsec.tsNSec());
00246   return String(date_time_sec) + "," + String(date_time_nsec);
00247 }
00248 
00249 String TimeStamp::formatIso8601BasicUTC() const
00250 {
00251   char date_time_sec[16], date_time_nsec[10];
00252   TimeStamp adjust_nsec(*this);
00253   while (adjust_nsec.nsecs < 0)
00254   {
00255     --adjust_nsec.secs;
00256     adjust_nsec.nsecs += 1000000000;
00257   }
00258   adjust_nsec.strfTime(date_time_sec, 16, "%Y%m%dT%H%M%S");
00259   ::icl_core::os::snprintf(date_time_nsec, 10, "%09i", adjust_nsec.tsNSec());
00260   return String(date_time_sec) + "," + String(date_time_nsec);
00261 }
00262 
00264 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
00265 
00270   TimeStamp TimeStamp::Now()
00271   {
00272     return now();
00273   }
00274 
00278   TimeStamp TimeStamp::FutureMSec(uint64_t msec)
00279   {
00280     return futureMSec(msec);
00281   }
00282 
00286   TimeStamp& TimeStamp::FromNow()
00287   {
00288     return fromNow();
00289   }
00290 
00291   void TimeStamp::Strftime(char* dest, size_t max_len, const char *format) const
00292   {
00293     strfTime(dest, max_len, format);
00294   }
00295 
00296   void TimeStamp::StrfLocaltime(char* dest, size_t max_len, const char *format) const
00297   {
00298     strfLocaltime(dest, max_len, format);
00299   }
00300 
00301   String TimeStamp::FormatIso8601() const
00302   {
00303     return formatIso8601();
00304   }
00305 
00306   uint64_t TimeStamp::TsSec() const
00307   {
00308     return tsSec();
00309   }
00310   uint32_t TimeStamp::TsUSec() const
00311   {
00312     return tsUSec();
00313   }
00314   uint32_t TimeStamp::TsNSec() const
00315   {
00316     return tsNSec();
00317   }
00318 
00319   TimeStamp TimeStamp::MaxTime()
00320   {
00321     return maxTime();
00322   }
00323 
00324 #endif
00325 
00326 
00327 TimeStamp& TimeStamp::operator += (const TimeStamp& other)
00328 {
00329   secs += other.secs;
00330   nsecs += other.nsecs;
00331   normalizeTime();
00332   return *this;
00333 }
00334 
00335 }


schunk_svh_driver
Author(s): Georg Heppner
autogenerated on Fri Aug 28 2015 12:59:19