time.cpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 // SPDX-FileCopyrightText: Czech Technical University in Prague
3 
4 #include <limits>
5 
6 #include <gnsstk/CommonTime.hpp>
7 #include <gnsstk/YDSTime.hpp>
8 
9 #include <gnsstk_ros/time.h>
10 #include <ros/duration.h>
11 #include <ros/time.h>
12 
13 namespace gnsstk_ros
14 {
15 
16 // Statically initialize the number of days since 1 Jan 1970 in gnsstk::CommonTime.
18 const long UnixEpoch::gnsstkDaysOffset {getDays(UnixEpoch::gnsstkCommonTime)}; // NOLINT(runtime/int)
19 
20 long getDays(const gnsstk::CommonTime& time) // NOLINT(runtime/int)
21 {
22  long days, secs; // NOLINT(runtime/int)
23  double frac;
24  time.get(days, secs, frac);
25  return days;
26 }
27 
29 {
30  long days, secs; // NOLINT(runtime/int)
31  double frac;
32  commonTime.get(days, secs, frac);
33 
34  const auto daysSecs = ros::Duration::DAY.sec * (days - UnixEpoch::gnsstkDaysOffset);
35  const auto timeSecs = daysSecs + secs;
36 
37  using SecType = decltype(ros::Duration::sec);
38  if (timeSecs < std::numeric_limits<SecType>::min() || timeSecs > std::numeric_limits<SecType>::max())
39  throw std::runtime_error("Duration is out of dual 32-bit range");
40 
41  return ros::Time(static_cast<SecType>(timeSecs), 0) + ros::Duration(frac);
42 }
43 
45 {
46  auto commonTime = UnixEpoch::gnsstkCommonTime;
47  commonTime.addDays(rosTime.sec / ros::Duration::DAY.sec);
48  commonTime.addSeconds(static_cast<long>(rosTime.sec % ros::Duration::DAY.sec)); // NOLINT(runtime/int)
49  commonTime.addSeconds(rosTime.nsec * 1e-9);
50  return commonTime;
51 }
52 
53 }
gnsstk::CommonTime::addSeconds
CommonTime & addSeconds(double seconds)
gnsstk::YDSTime
gnsstk_ros
Definition: constellations.h:15
DurationBase< Duration >::DAY
static const Duration DAY
gnsstk_ros::convert
gnsstk::Position convert(const geographic_msgs::GeoPoint &position)
Convert the ROS GeoPoint message to gnsstk Position with Geodetic type.
Definition: position.cpp:13
duration.h
gnsstk_ros::UnixEpoch::gnsstkDaysOffset
static const long gnsstkDaysOffset
Number of days between CommonTime beginning of time and UNIX epoch start.
Definition: time.h:20
time.h
time
time
gnsstk_ros::getDays
long getDays(const gnsstk::CommonTime &time)
Get the number of days stored in the given time object.
Definition: time.cpp:20
gnsstk::CommonTime
gnsstk::CommonTime::get
void get(long &day, long &sod, double &fsod, TimeSystem &timeSystem) const
TimeBase< Time, Duration >::sec
uint32_t sec
TimeBase< Time, Duration >::nsec
uint32_t nsec
gnsstk::YDSTime::convertToCommonTime
virtual CommonTime convertToCommonTime() const
ros::Time
gnsstk_ros::UnixEpoch::gnsstkCommonTime
static const gnsstk::CommonTime gnsstkCommonTime
Definition: time.h:18
ros::Duration
gnsstk::CommonTime::addDays
CommonTime & addDays(long days)
DurationBase< Duration >::sec
int32_t sec


gnsstk_ros
Author(s): Martin Pecka
autogenerated on Fri Nov 24 2023 03:50:23