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 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Goby Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 #include "goby/common/time.h"
24 
25 boost::function0<goby::uint64> goby::common::goby_time_function;
26 int goby::common::goby_time_warp_factor = 1;
27 
28 double goby::common::ptime2unix_double(boost::posix_time::ptime given_time)
29 {
30  using namespace boost::posix_time;
31  using namespace boost::gregorian;
32 
33  if (given_time == not_a_date_time)
34  return -1;
35  else
36  {
37  date_duration date_diff = given_time.date() - date(1970, 1, 1);
38  time_duration time_diff = given_time.time_of_day();
39 
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());
44  }
45 }
46 
47 boost::posix_time::ptime goby::common::unix_double2ptime(double given_time)
48 {
49  using namespace boost::posix_time;
50  using namespace boost::gregorian;
51 
52  if (given_time == -1)
53  return boost::posix_time::ptime(not_a_date_time);
54  else
55  {
56  date date_epoch(date(1970, 1, 1));
57 
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;
62  long h = sec / 3600;
63  sec -= h * 3600;
64  long m = sec / 60;
65  sec -= m * 60;
66  long s = sec;
67  return ptime(date_epoch + days(d), time_duration(h, m, s) + microseconds(micro_s));
68  }
69 }
70 
71 goby::uint64 goby::common::ptime2unix_microsec(boost::posix_time::ptime given_time)
72 {
73  using namespace boost::posix_time;
74  using namespace boost::gregorian;
75 
76  if (given_time == not_a_date_time)
77  return std::numeric_limits<uint64>::max();
78  else
79  {
80  const int MICROSEC_IN_SEC = 1000000;
81 
82  date_duration date_diff = given_time.date() - date(1970, 1, 1);
83  time_duration time_diff = given_time.time_of_day();
84 
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);
89  }
90 }
91 
93 boost::posix_time::ptime goby::common::unix_microsec2ptime(uint64 given_time)
94 {
95  using namespace boost::posix_time;
96  using namespace boost::gregorian;
97 
98  if (given_time == std::numeric_limits<uint64>::max())
99  return boost::posix_time::ptime(not_a_date_time);
100  else
101  {
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);
108  }
109 }
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