impl/time.h
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 #ifndef ROS_TIME_IMPL_H_INCLUDED
36 #define ROS_TIME_IMPL_H_INCLUDED
37 
38 /*********************************************************************
39 ** Headers
40 *********************************************************************/
41 
42 #include <ros/platform.h>
43 #include <iostream>
44 #include <cmath>
45 #include <limits>
46 #include <ros/exception.h>
47 #include <ros/time.h>
48 #include <boost/date_time/posix_time/posix_time.hpp>
49 
50 /*********************************************************************
51 ** Cross Platform Headers
52 *********************************************************************/
53 
54 #if defined(_WIN32)
55  #include <sys/timeb.h>
56 #else
57  #include <sys/time.h>
58 #endif
59 
60 namespace ros
61 {
62 
63  template<class T, class D>
64  T& TimeBase<T, D>::fromNSec(uint64_t t)
65  {
66  uint64_t sec64 = 0;
67  uint64_t nsec64 = t;
68 
69  normalizeSecNSec(sec64, nsec64);
70 
71  sec = static_cast<uint32_t>(sec64);
72  nsec = static_cast<uint32_t>(nsec64);
73 
74  return *static_cast<T*>(this);
75  }
76 
77  template<class T, class D>
78  T& TimeBase<T, D>::fromSec(double t) {
79  if (t < 0)
80  throw std::runtime_error("Time cannot be negative.");
81  if (!std::isfinite(t))
82  throw std::runtime_error("Time has to be finite.");
83  constexpr double maxInt64AsDouble = static_cast<double>(std::numeric_limits<int64_t>::max());
84  if (t >= maxInt64AsDouble)
85  throw std::runtime_error("Time is out of 64-bit integer range");
86  int64_t sec64 = static_cast<int64_t>(floor(t));
87  if (sec64 > std::numeric_limits<uint32_t>::max())
88  throw std::runtime_error("Time is out of dual 32-bit range");
89  sec = static_cast<uint32_t>(sec64);
90  nsec = static_cast<uint32_t>(boost::math::round((t-sec) * 1e9));
91  // avoid rounding errors
92  sec += (nsec / 1000000000ul);
93  nsec %= 1000000000ul;
94  return *static_cast<T*>(this);
95  }
96 
97  template<class T, class D>
98  D TimeBase<T, D>::operator-(const T &rhs) const
99  {
100  D d;
101  return d.fromNSec(toNSec() - rhs.toNSec());
102  }
103 
104  template<class T, class D>
105  T TimeBase<T, D>::operator-(const D &rhs) const
106  {
107  return *static_cast<const T*>(this) + ( -rhs);
108  }
109 
110  template<class T, class D>
111  T TimeBase<T, D>::operator+(const D &rhs) const
112  {
113  int64_t sec_sum = static_cast<uint64_t>(sec) + static_cast<uint64_t>(rhs.sec);
114  int64_t nsec_sum = static_cast<uint64_t>(nsec) + static_cast<uint64_t>(rhs.nsec);
115 
116  // Throws an exception if we go out of 32-bit range
117  normalizeSecNSecUnsigned(sec_sum, nsec_sum);
118 
119  // now, it's safe to downcast back to uint32 bits
120  return T(static_cast<uint32_t>(sec_sum), static_cast<uint32_t>(nsec_sum));
121  }
122 
123  template<class T, class D>
124  T& TimeBase<T, D>::operator+=(const D &rhs)
125  {
126  *this = *this + rhs;
127  return *static_cast<T*>(this);
128  }
129 
130  template<class T, class D>
131  T& TimeBase<T, D>::operator-=(const D &rhs)
132  {
133  *this += (-rhs);
134  return *static_cast<T*>(this);
135  }
136 
137  template<class T, class D>
138  bool TimeBase<T, D>::operator==(const T &rhs) const
139  {
140  return sec == rhs.sec && nsec == rhs.nsec;
141  }
142 
143  template<class T, class D>
144  bool TimeBase<T, D>::operator<(const T &rhs) const
145  {
146  if (sec < rhs.sec)
147  return true;
148  else if (sec == rhs.sec && nsec < rhs.nsec)
149  return true;
150  return false;
151  }
152 
153  template<class T, class D>
154  bool TimeBase<T, D>::operator>(const T &rhs) const
155  {
156  if (sec > rhs.sec)
157  return true;
158  else if (sec == rhs.sec && nsec > rhs.nsec)
159  return true;
160  return false;
161  }
162 
163  template<class T, class D>
164  bool TimeBase<T, D>::operator<=(const T &rhs) const
165  {
166  if (sec < rhs.sec)
167  return true;
168  else if (sec == rhs.sec && nsec <= rhs.nsec)
169  return true;
170  return false;
171  }
172 
173  template<class T, class D>
174  bool TimeBase<T, D>::operator>=(const T &rhs) const
175  {
176  if (sec > rhs.sec)
177  return true;
178  else if (sec == rhs.sec && nsec >= rhs.nsec)
179  return true;
180  return false;
181  }
182 
183  template<class T, class D>
184  boost::posix_time::ptime
186  {
187  namespace pt = boost::posix_time;
188 #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
189  return pt::from_time_t(sec) + pt::nanoseconds(nsec);
190 #else
191  return pt::from_time_t(sec) + pt::microseconds(nsec/1000);
192 #endif
193  }
194 }
195 
196 #endif // ROS_IMPL_TIME_H_INCLUDED
ros::normalizeSecNSecUnsigned
ROSTIME_DECL void normalizeSecNSecUnsigned(int64_t &sec, int64_t &nsec)
Definition: src/time.cpp:537
ros::TimeBase::operator<
bool operator<(const T &rhs) const
Definition: impl/time.h:180
ros::TimeBase::fromSec
T & fromSec(double t)
Definition: impl/time.h:114
ros
time.h
ros::TimeBase::toBoost
boost::posix_time::ptime toBoost() const
Definition: impl/time.h:221
ros::TimeBase::operator+
T operator+(const D &rhs) const
Definition: impl/time.h:147
ros::TimeBase::operator<=
bool operator<=(const T &rhs) const
Definition: impl/time.h:200
ros::TimeBase::operator-=
T & operator-=(const D &rhs)
Definition: impl/time.h:167
ros::TimeBase::operator>
bool operator>(const T &rhs) const
Definition: impl/time.h:190
ros::TimeBase::operator>=
bool operator>=(const T &rhs) const
Definition: impl/time.h:210
ros::TimeBase::operator-
D operator-(const T &rhs) const
Definition: impl/time.h:134
ros::TimeBase::fromNSec
T & fromNSec(uint64_t t)
Definition: impl/time.h:100
boost::posix_time
Definition: duration.h:91
platform.h
ros::TimeBase::operator+=
T & operator+=(const D &rhs)
Definition: impl/time.h:160
ros::normalizeSecNSec
ROSTIME_DECL void normalizeSecNSec(uint64_t &sec, uint64_t &nsec)
Definition: src/time.cpp:514
exception.h
ros::TimeBase::operator==
bool operator==(const T &rhs) const
Definition: impl/time.h:174


rostime
Author(s): Josh Faust, Dirk Thomas
autogenerated on Sat Jun 17 2023 02:32:37