29 #include <boost/asio/time_traits.hpp> 30 #include <boost/date_time.hpp> 31 #include <boost/function.hpp> 32 #include <boost/static_assert.hpp> 34 #include "goby/util/as.h" 35 #include "goby/util/primitive_types.h" 58 template <
typename To,
typename From>
59 typename boost::enable_if<
60 boost::mpl::and_<boost::is_same<To, double>, boost::is_same<From, boost::posix_time::ptime> >,
68 template <
typename To,
typename From>
69 typename boost::enable_if<
70 boost::mpl::and_<boost::is_same<To, boost::posix_time::ptime>, boost::is_same<From, double> >,
78 template <
typename To,
typename From>
79 typename boost::enable_if<
80 boost::mpl::and_<boost::is_same<To, uint64>, boost::is_same<From, boost::posix_time::ptime> >,
88 template <
typename To,
typename From>
89 typename boost::enable_if<
90 boost::mpl::and_<boost::is_same<To, boost::posix_time::ptime>, boost::is_same<From, uint64> >,
106 BOOST_STATIC_ASSERT(
sizeof(ReturnType) == 0);
109 extern boost::function0<uint64> goby_time_function;
110 extern int goby_time_warp_factor;
115 if (!goby_time_function)
121 return whole * 1000000 + micro;
125 return goby_time_function();
135 template <>
inline boost::posix_time::ptime goby_time<boost::posix_time::ptime>()
141 inline boost::posix_time::ptime
goby_time() {
return goby_time<boost::posix_time::ptime>(); }
144 template <>
inline std::string goby_time<std::string>()
146 return goby::util::as<std::string>(goby_time<boost::posix_time::ptime>());
152 return goby::util::as<std::string>(t);
159 return to_iso_string(second_clock::universal_time());
165 return boost::posix_time::from_time_t(t);
171 std::tm out = boost::posix_time::to_tm(t);
181 if (time_of_day.hours() > (0x7FFFFFFF / 3600))
182 return std::numeric_limits<double>::infinity();
184 return (
double(time_of_day.total_seconds()) +
185 double(time_of_day.fractional_seconds()) /
186 double(time_duration::ticks_per_second()));
191 inline boost::posix_time::ptime nmea_time2ptime(
const std::string& mt)
196 std::string::size_type dot_pos = mt.find(
'.');
200 return ptime(not_a_date_time);
203 std::string s_fs =
"0";
205 if (dot_pos != std::string::npos)
206 s_fs = mt.substr(dot_pos + 1);
210 std::string s_hour = mt.substr(dot_pos - 6, 2), s_min = mt.substr(dot_pos - 4, 2),
211 s_sec = mt.substr(dot_pos - 2, 2);
215 int hour = boost::lexical_cast<
int>(s_hour);
216 int min = boost::lexical_cast<
int>(s_min);
217 int sec = boost::lexical_cast<
int>(s_sec);
218 int micro_sec = boost::lexical_cast<
int>(s_fs) * pow(10, 6 - s_fs.size());
220 boost::gregorian::date return_date(boost::gregorian::day_clock::universal_day());
221 boost::posix_time::time_duration return_duration(
222 boost::posix_time::time_duration(hour, min, sec, 0) + microseconds(micro_sec));
223 boost::posix_time::ptime return_time(return_date, return_duration);
226 catch (boost::bad_lexical_cast&)
228 return ptime(not_a_date_time);
246 template <>
struct time_traits<
goby::common::GobyTime>
258 static time_type
add(
const time_type& t,
const duration_type& d) {
return t + d; }
261 static duration_type
subtract(
const time_type& t1,
const time_type& t2) {
return t1 - t2; }
264 static bool less_than(
const time_type& t1,
const time_type& t2) {
return t1 < t2; }
269 return d / goby::common::goby_time_warp_factor;
uint64 goby_time< uint64 >()
Returns current UTC time as integer microseconds since 1970-01-01 00:00:00.
boost::posix_time::ptime time_t2ptime(std::time_t t)
convert to ptime from time_t from time.h (whole seconds since UNIX)
static duration_type subtract(const time_type &t1, const time_type &t2)
Subtract one time from another.
static time_type now()
Get the current time.
std::string goby_time_as_string(const boost::posix_time::ptime &t=goby_time())
Simple string representation of goby_time()
static time_type add(const time_type &t, const duration_type &d)
Add a duration to a time.
double goby_time< double >()
Returns current UTC time as seconds and fractional seconds since 1970-01-01 00:00:00.
ReturnType goby_time()
Returns current UTC time as a boost::posix_time::ptime.
double time_duration2double(boost::posix_time::time_duration time_of_day)
time duration to double number of seconds: good to the microsecond
std::string goby_file_timestamp()
ISO string representation of goby_time()
uint64 ptime2unix_microsec(boost::posix_time::ptime given_time)
convert from boost date_time ptime to the number of microseconds since 1/1/1970 0:00 UTC ("UNIX Time"...
std::time_t ptime2time_t(boost::posix_time::ptime t)
convert from ptime to time_t from time.h (whole seconds since UNIX)
The global namespace for the Goby project.
static bool less_than(const time_type &t1, const time_type &t2)
Test whether one time is less than another.
boost::posix_time::ptime unix_microsec2ptime(uint64 given_time)
convert to boost date_time ptime from the number of microseconds since 1/1/1970 0:00 UTC ("UNIX Time"...
boost::posix_time::time_duration duration_type
The duration type.
double ptime2unix_double(boost::posix_time::ptime given_time)
convert from boost date_time ptime to the number of seconds (including fractional) since 1/1/1970 0:0...
google::protobuf::uint64 uint64
an unsigned 64 bit integer
boost::posix_time::ptime unix_double2ptime(double given_time)
convert to boost date_time ptime from the number of seconds (including fractional) since 1/1/1970 0:0...
static boost::posix_time::time_duration to_posix_duration(const duration_type &d)
Convert to POSIX duration type.
boost::posix_time::ptime time_type
The time type.