Goby v2
time.cpp
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 //
5 //
6 // This file is part of the Goby Underwater Autonomy Project Binaries
7 // ("The Goby Binaries").
8 //
9 // The Goby Binaries are free software: you can redistribute them and/or modify
10 // them under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // The Goby Binaries are distributed in the hope that they will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
21 
22 #include "goby/common/time.h"
23 #include "stdint.h"
24 
25 using goby::uint64;
27 using goby::util::as;
28 using namespace boost::posix_time;
29 using namespace boost::gregorian;
30 
31 // 2011-08-16 19:36:57.523456 UTC
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));
36 
37 bool double_cmp(double a, double b, int precision)
38 {
39  long long a_whole = a;
40  long long b_whole = b;
41 
42  int a_part = (a - a_whole) * pow(10.0, precision);
43  int b_part = (b - b_whole) * pow(10.0, precision);
44 
45  return (a_whole == b_whole) && (a_part == b_part);
46 }
47 
48 uint64 fake_time() { return TEST_MICROSEC_TIME; }
49 
50 int main()
51 {
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;
55 
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;
59 
60  assert(!double_cmp(5.4, 2.3, 1));
61 
62  assert(double_cmp(goby::common::ptime2unix_double(TEST_PTIME), TEST_DOUBLE_TIME, 6));
63  assert(double_cmp(as<double>(TEST_PTIME), TEST_DOUBLE_TIME, 6)); // same as previous line
64 
65  std::cout << "goby::common::unix_double2ptime(TEST_DOUBLE_TIME) "
66  << goby::common::unix_double2ptime(TEST_DOUBLE_TIME) << std::endl;
67 
68  assert(goby::common::unix_double2ptime(TEST_DOUBLE_TIME) == TEST_PTIME);
69  assert(as<ptime>(TEST_DOUBLE_TIME) == TEST_PTIME); // same as previous line
70 
71  std::cout << "goby::common::ptime2unix_microsec(TEST_PTIME) "
72  << goby::common::ptime2unix_microsec(TEST_PTIME) << std::endl;
73 
74  assert(goby::common::ptime2unix_microsec(TEST_PTIME) == TEST_MICROSEC_TIME);
75  assert(as<uint64>(TEST_PTIME) == TEST_MICROSEC_TIME); // same as previous line
76 
77  assert(goby::common::unix_microsec2ptime(TEST_MICROSEC_TIME) == TEST_PTIME);
78  assert(as<ptime>(TEST_MICROSEC_TIME) == TEST_PTIME); // same as previous line
79 
80  std::cout << "as<string>(as<uint64>(ptime): "
81  << goby::util::as<std::string>(goby::util::as<uint64>(TEST_PTIME)) << std::endl;
82 
83  assert(as<ptime>(std::numeric_limits<uint64>::max()) == ptime(not_a_date_time));
84 
85  std::cout << goby_time() << std::endl;
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));
94 
95  goby::common::goby_time_function = &fake_time;
96 
97  std::cout << goby_time() << std::endl;
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);
103 
104  const ptime FAR_FUTURE_COMPARISON_PTIME(date(2391, 10, 8),
105  time_duration(9, 50, 9) + microseconds(399860));
106 
107  // test dates in the next century
108  const double FAR_FUTURE_COMPARISON = 13309696209.39986;
109  ptime far_future_ptime = goby::common::unix_double2ptime(FAR_FUTURE_COMPARISON);
110  double far_future_time = goby::common::ptime2unix_double(far_future_ptime);
111  std::cout << FAR_FUTURE_COMPARISON_PTIME << "=?" << far_future_ptime << "=?" << far_future_time
112  << "=?" << FAR_FUTURE_COMPARISON << std::endl;
113 
114  assert(double_cmp(far_future_time, FAR_FUTURE_COMPARISON, 5));
115 
116  const uint64 FAR_FUTURE_COMPARISON_UINT64 = FAR_FUTURE_COMPARISON * 1e6;
118  FAR_FUTURE_COMPARISON_UINT64)) == FAR_FUTURE_COMPARISON_UINT64);
119 
120  std::cout << "all tests passed" << std::endl;
121 
122  return 0;
123 }
ReturnType goby_time()
Returns current UTC time as a boost::posix_time::ptime.
Definition: time.h:104
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"...
Definition: time.cpp:71
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"...
Definition: time.cpp:93
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...
Definition: time.cpp:28
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...
Definition: time.cpp:47