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 #ifdef 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 = (uint32_t)sec64;
71  nsec = (uint32_t)nsec64;
72 
73  return *static_cast<T*>(this);
74  }
75 
76  template<class T, class D>
77  D TimeBase<T, D>::operator-(const T &rhs) const
78  {
79  D d;
80  return d.fromNSec(toNSec() - rhs.toNSec());
81  }
82 
83  template<class T, class D>
84  T TimeBase<T, D>::operator-(const D &rhs) const
85  {
86  return *static_cast<const T*>(this) + ( -rhs);
87  }
88 
89  template<class T, class D>
90  T TimeBase<T, D>::operator+(const D &rhs) const
91  {
92  int64_t sec_sum = (int64_t)sec + (int64_t)rhs.sec;
93  int64_t nsec_sum = (int64_t)nsec + (int64_t)rhs.nsec;
94 
95  // Throws an exception if we go out of 32-bit range
96  normalizeSecNSecUnsigned(sec_sum, nsec_sum);
97 
98  // now, it's safe to downcast back to uint32 bits
99  return T((uint32_t)sec_sum, (uint32_t)nsec_sum);
100  }
101 
102  template<class T, class D>
103  T& TimeBase<T, D>::operator+=(const D &rhs)
104  {
105  *this = *this + rhs;
106  return *static_cast<T*>(this);
107  }
108 
109  template<class T, class D>
110  T& TimeBase<T, D>::operator-=(const D &rhs)
111  {
112  *this += (-rhs);
113  return *static_cast<T*>(this);
114  }
115 
116  template<class T, class D>
117  bool TimeBase<T, D>::operator==(const T &rhs) const
118  {
119  return sec == rhs.sec && nsec == rhs.nsec;
120  }
121 
122  template<class T, class D>
123  bool TimeBase<T, D>::operator<(const T &rhs) const
124  {
125  if (sec < rhs.sec)
126  return true;
127  else if (sec == rhs.sec && nsec < rhs.nsec)
128  return true;
129  return false;
130  }
131 
132  template<class T, class D>
133  bool TimeBase<T, D>::operator>(const T &rhs) const
134  {
135  if (sec > rhs.sec)
136  return true;
137  else if (sec == rhs.sec && nsec > rhs.nsec)
138  return true;
139  return false;
140  }
141 
142  template<class T, class D>
143  bool TimeBase<T, D>::operator<=(const T &rhs) const
144  {
145  if (sec < rhs.sec)
146  return true;
147  else if (sec == rhs.sec && nsec <= rhs.nsec)
148  return true;
149  return false;
150  }
151 
152  template<class T, class D>
153  bool TimeBase<T, D>::operator>=(const T &rhs) const
154  {
155  if (sec > rhs.sec)
156  return true;
157  else if (sec == rhs.sec && nsec >= rhs.nsec)
158  return true;
159  return false;
160  }
161 
162  template<class T, class D>
163  boost::posix_time::ptime
165  {
166  namespace pt = boost::posix_time;
167 #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
168  return pt::from_time_t(sec) + pt::nanoseconds(nsec);
169 #else
170  return pt::from_time_t(sec) + pt::microseconds(nsec/1000);
171 #endif
172  }
173 
174 
175 }
176 
177 #endif // ROS_IMPL_TIME_H_INCLUDED
178 
bool operator==(const T &rhs) const
Definition: impl/time.h:117
T & operator-=(const D &rhs)
Definition: impl/time.h:110
T & fromNSec(uint64_t t)
Definition: impl/time.h:63
T & operator+=(const D &rhs)
Definition: impl/time.h:103
ROSTIME_DECL void normalizeSecNSecUnsigned(int64_t &sec, int64_t &nsec)
Definition: src/time.cpp:573
bool operator<(const T &rhs) const
Definition: impl/time.h:123
bool operator<=(const T &rhs) const
Definition: impl/time.h:143
bool operator>(const T &rhs) const
Definition: impl/time.h:133
D operator-(const T &rhs) const
Definition: impl/time.h:77
bool operator>=(const T &rhs) const
Definition: impl/time.h:153
ROSTIME_DECL void normalizeSecNSec(uint64_t &sec, uint64_t &nsec)
Definition: src/time.cpp:550
T operator+(const D &rhs) const
Definition: impl/time.h:90
boost::posix_time::ptime toBoost() const
Definition: impl/time.h:164


rostime
Author(s): Josh Faust
autogenerated on Mon Aug 6 2018 02:22:28