22 #include "goby/common/time.h" 32 const double TEST_DOUBLE_TIME = 1313523417.523456;
33 const uint64 TEST_MICROSEC_TIME = TEST_DOUBLE_TIME * 1e6;
34 const boost::posix_time::ptime TEST_PTIME(date(2011, 8, 16),
35 time_duration(19, 36, 57) + microseconds(523456));
37 bool double_cmp(
double a,
double b,
int precision)
39 long long a_whole = a;
40 long long b_whole = b;
42 int a_part = (a - a_whole) * pow(10.0, precision);
43 int b_part = (b - b_whole) * pow(10.0, precision);
45 return (a_whole == b_whole) && (a_part == b_part);
48 uint64 fake_time() {
return TEST_MICROSEC_TIME; }
52 std::cout <<
"current double: " << std::setprecision(16) << goby_time<double>() << std::endl;
53 std::cout <<
"current uint64: " << goby_time<uint64>() << std::endl;
54 std::cout <<
"current ptime: " <<
goby_time() << std::endl;
56 std::cout <<
"double: " << std::setprecision(16) << TEST_DOUBLE_TIME << std::endl;
57 std::cout <<
"uint64: " << TEST_MICROSEC_TIME << std::endl;
58 std::cout <<
"ptime: " << TEST_PTIME << std::endl;
60 assert(!double_cmp(5.4, 2.3, 1));
63 assert(double_cmp(as<double>(TEST_PTIME), TEST_DOUBLE_TIME, 6));
65 std::cout <<
"goby::common::unix_double2ptime(TEST_DOUBLE_TIME) " 69 assert(as<ptime>(TEST_DOUBLE_TIME) == TEST_PTIME);
71 std::cout <<
"goby::common::ptime2unix_microsec(TEST_PTIME) " 75 assert(as<uint64>(TEST_PTIME) == TEST_MICROSEC_TIME);
78 assert(as<ptime>(TEST_MICROSEC_TIME) == TEST_PTIME);
80 std::cout <<
"as<string>(as<uint64>(ptime): " 81 << goby::util::as<std::string>(goby::util::as<uint64>(TEST_PTIME)) << std::endl;
83 assert(as<ptime>(std::numeric_limits<uint64>::max()) == ptime(not_a_date_time));
86 assert(
goby_time() - boost::posix_time::microsec_clock::universal_time() <
87 boost::posix_time::milliseconds(10));
88 std::cout << goby_time<double>() << std::endl;
89 assert(as<ptime>(goby_time<double>()) - boost::posix_time::microsec_clock::universal_time() <
90 boost::posix_time::milliseconds(10));
91 std::cout << goby_time<uint64>() << std::endl;
92 assert(as<ptime>(goby_time<uint64>()) - boost::posix_time::microsec_clock::universal_time() <
93 boost::posix_time::milliseconds(10));
95 goby::common::goby_time_function = &fake_time;
98 std::cout << goby_time<double>() << std::endl;
99 std::cout << goby_time<uint64>() << std::endl;
100 assert(goby_time<uint64>() == TEST_MICROSEC_TIME);
101 assert(double_cmp(goby_time<double>(), TEST_DOUBLE_TIME, 6));
102 assert(goby_time<ptime>() == TEST_PTIME);
104 const ptime FAR_FUTURE_COMPARISON_PTIME(date(2391, 10, 8),
105 time_duration(9, 50, 9) + microseconds(399860));
108 const double FAR_FUTURE_COMPARISON = 13309696209.39986;
111 std::cout << FAR_FUTURE_COMPARISON_PTIME <<
"=?" << far_future_ptime <<
"=?" << far_future_time
112 <<
"=?" << FAR_FUTURE_COMPARISON << std::endl;
114 assert(double_cmp(far_future_time, FAR_FUTURE_COMPARISON, 5));
116 const uint64 FAR_FUTURE_COMPARISON_UINT64 = FAR_FUTURE_COMPARISON * 1e6;
118 FAR_FUTURE_COMPARISON_UINT64)) == FAR_FUTURE_COMPARISON_UINT64);
120 std::cout <<
"all tests passed" << std::endl;
ReturnType goby_time()
Returns current UTC time as a boost::posix_time::ptime.
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...