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 <ros/exception.h>
46 #include <ros/time.h>
47 #include <boost/date_time/posix_time/posix_time.hpp>
48 
49 /*********************************************************************
50 ** Cross Platform Headers
51 *********************************************************************/
52 
53 #if defined(_WIN32)
54  #include <sys/timeb.h>
55 #else
56  #include <sys/time.h>
57 #endif
58 
59 namespace ros
60 {
61 
62  template<class T, class D>
63  T& TimeBase<T, D>::fromNSec(uint64_t t)
64  {
65  uint64_t sec64 = 0;
66  uint64_t nsec64 = t;
67 
68  normalizeSecNSec(sec64, nsec64);
69 
70  sec = static_cast<uint32_t>(sec64);
71  nsec = static_cast<uint32_t>(nsec64);
72 
73  return *static_cast<T*>(this);
74  }
75 
76  template<class T, class D>
77  T& TimeBase<T, D>::fromSec(double t) {
78  int64_t sec64 = static_cast<int64_t>(floor(t));
79  if (sec64 < 0 || sec64 > std::numeric_limits<uint32_t>::max())
80  throw std::runtime_error("Time is out of dual 32-bit range");
81  sec = static_cast<uint32_t>(sec64);
82  nsec = static_cast<uint32_t>(boost::math::round((t-sec) * 1e9));
83  // avoid rounding errors
84  sec += (nsec / 1000000000ul);
85  nsec %= 1000000000ul;
86  return *static_cast<T*>(this);
87  }
88 
89  template<class T, class D>
90  D TimeBase<T, D>::operator-(const T &rhs) const
91  {
92  D d;
93  return d.fromNSec(toNSec() - rhs.toNSec());
94  }
95 
96  template<class T, class D>
97  T TimeBase<T, D>::operator-(const D &rhs) const
98  {
99  return *static_cast<const T*>(this) + ( -rhs);
100  }
101 
102  template<class T, class D>
103  T TimeBase<T, D>::operator+(const D &rhs) const
104  {
105  int64_t sec_sum = static_cast<uint64_t>(sec) + static_cast<uint64_t>(rhs.sec);
106  int64_t nsec_sum = static_cast<uint64_t>(nsec) + static_cast<uint64_t>(rhs.nsec);
107 
108  // Throws an exception if we go out of 32-bit range
109  normalizeSecNSecUnsigned(sec_sum, nsec_sum);
110 
111  // now, it's safe to downcast back to uint32 bits
112  return T(static_cast<uint32_t>(sec_sum), static_cast<uint32_t>(nsec_sum));
113  }
114 
115  template<class T, class D>
116  T& TimeBase<T, D>::operator+=(const D &rhs)
117  {
118  *this = *this + rhs;
119  return *static_cast<T*>(this);
120  }
121 
122  template<class T, class D>
123  T& TimeBase<T, D>::operator-=(const D &rhs)
124  {
125  *this += (-rhs);
126  return *static_cast<T*>(this);
127  }
128 
129  template<class T, class D>
130  bool TimeBase<T, D>::operator==(const T &rhs) const
131  {
132  return sec == rhs.sec && nsec == rhs.nsec;
133  }
134 
135  template<class T, class D>
136  bool TimeBase<T, D>::operator<(const T &rhs) const
137  {
138  if (sec < rhs.sec)
139  return true;
140  else if (sec == rhs.sec && nsec < rhs.nsec)
141  return true;
142  return false;
143  }
144 
145  template<class T, class D>
146  bool TimeBase<T, D>::operator>(const T &rhs) const
147  {
148  if (sec > rhs.sec)
149  return true;
150  else if (sec == rhs.sec && nsec > rhs.nsec)
151  return true;
152  return false;
153  }
154 
155  template<class T, class D>
156  bool TimeBase<T, D>::operator<=(const T &rhs) const
157  {
158  if (sec < rhs.sec)
159  return true;
160  else if (sec == rhs.sec && nsec <= rhs.nsec)
161  return true;
162  return false;
163  }
164 
165  template<class T, class D>
166  bool TimeBase<T, D>::operator>=(const T &rhs) const
167  {
168  if (sec > rhs.sec)
169  return true;
170  else if (sec == rhs.sec && nsec >= rhs.nsec)
171  return true;
172  return false;
173  }
174 
175  template<class T, class D>
176  boost::posix_time::ptime
178  {
179  namespace pt = boost::posix_time;
180 #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
181  return pt::from_time_t(sec) + pt::nanoseconds(nsec);
182 #else
183  return pt::from_time_t(sec) + pt::microseconds(nsec/1000);
184 #endif
185  }
186 }
187 
188 #endif // ROS_IMPL_TIME_H_INCLUDED
bool operator>(const T &rhs) const
Definition: impl/time.h:146
T & operator-=(const D &rhs)
Definition: impl/time.h:123
T & fromNSec(uint64_t t)
Definition: impl/time.h:63
T & operator+=(const D &rhs)
Definition: impl/time.h:116
T operator+(const D &rhs) const
Definition: impl/time.h:103
ROSTIME_DECL void normalizeSecNSecUnsigned(int64_t &sec, int64_t &nsec)
Definition: src/time.cpp:496
bool operator==(const T &rhs) const
Definition: impl/time.h:130
T & fromSec(double t)
Definition: impl/time.h:77
bool operator<=(const T &rhs) const
Definition: impl/time.h:156
bool operator>=(const T &rhs) const
Definition: impl/time.h:166
bool operator<(const T &rhs) const
Definition: impl/time.h:136
D operator-(const T &rhs) const
Definition: impl/time.h:90
ROSTIME_DECL void normalizeSecNSec(uint64_t &sec, uint64_t &nsec)
Definition: src/time.cpp:473
boost::posix_time::ptime toBoost() const
Definition: impl/time.h:177


rostime
Author(s): Josh Faust
autogenerated on Mon Feb 28 2022 23:31:37