23 #include "goby/common/time.h" 25 boost::function0<goby::uint64> goby::common::goby_time_function;
26 int goby::common::goby_time_warp_factor = 1;
33 if (given_time == not_a_date_time)
37 date_duration date_diff = given_time.date() - date(1970, 1, 1);
38 time_duration time_diff = given_time.time_of_day();
40 return static_cast<double>(date_diff.days()) * 24 * 3600 +
41 static_cast<double>(time_diff.total_seconds()) +
42 static_cast<double>(time_diff.fractional_seconds()) /
43 static_cast<double>(time_duration::ticks_per_second());
53 return boost::posix_time::ptime(not_a_date_time);
56 date date_epoch(date(1970, 1, 1));
58 double sec = floor(given_time);
59 long micro_s = (given_time - sec) * 1e6;
60 long d = sec / 3600 / 24;
61 sec -=
static_cast<double>(d) * 3600 * 24;
67 return ptime(date_epoch + days(d), time_duration(h, m, s) + microseconds(micro_s));
76 if (given_time == not_a_date_time)
77 return std::numeric_limits<uint64>::max();
80 const int MICROSEC_IN_SEC = 1000000;
82 date_duration date_diff = given_time.date() - date(1970, 1, 1);
83 time_duration time_diff = given_time.time_of_day();
85 return static_cast<uint64>(date_diff.days()) * 24 * 3600 * MICROSEC_IN_SEC +
86 static_cast<uint64>(time_diff.total_seconds()) * MICROSEC_IN_SEC +
87 static_cast<uint64>(time_diff.fractional_seconds()) /
88 (time_duration::ticks_per_second() / MICROSEC_IN_SEC);
98 if (given_time == std::numeric_limits<uint64>::max())
99 return boost::posix_time::ptime(not_a_date_time);
102 const int MICROSEC_IN_SEC = 1000000;
103 ptime time_t_epoch(date(1970, 1, 1));
104 uint64 m = given_time / MICROSEC_IN_SEC / 60;
105 uint64 s = (given_time / MICROSEC_IN_SEC) - m * 60;
106 uint64 micro_s = (given_time - (s + m * 60) * MICROSEC_IN_SEC);
107 return time_t_epoch + minutes(m) + seconds(s) + microseconds(micro_s);
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"...
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"...
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...