Goby Underwater Autonomy Project
Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
|
00001 // copyright 2010 t. schneider tes@mit.edu 00002 // 00003 // this file is a collection of time conversion utilities used in goby 00004 // 00005 // This program is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // This software is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with this software. If not, see <http://www.gnu.org/licenses/>. 00017 00018 #ifndef Time20100713H 00019 #define Time20100713H 00020 00021 #include <ctime> 00022 00023 #include <stdint.h> 00024 #include <boost/date_time.hpp> 00025 00027 namespace goby 00028 { 00030 namespace util 00031 { 00033 00034 00035 //Time since unix epoch in microseconds. Signed to make math easy. 00036 inline int64_t microtime() { 00037 struct timeval tv; 00038 gettimeofday(&tv, NULL); 00039 return tv.tv_sec*1e6 + tv.tv_usec; 00040 } 00041 00043 inline boost::posix_time::ptime goby_time() 00044 { 00045 using namespace boost::posix_time; 00046 return ptime(microsec_clock::universal_time()); 00047 } 00048 00050 inline std::string goby_time_as_string(const boost::posix_time::ptime& t = goby_time()) 00051 { return boost::posix_time::to_simple_string(t); } 00052 00054 inline std::string goby_file_timestamp() 00055 { 00056 using namespace boost::posix_time; 00057 return to_iso_string(second_clock::universal_time()); 00058 } 00059 00060 00062 inline double ptime2unix_double(boost::posix_time::ptime given_time) 00063 { 00064 using namespace boost::posix_time; 00065 using namespace boost::gregorian; 00066 00067 if (given_time == not_a_date_time) 00068 return -1; 00069 else 00070 { 00071 ptime time_t_epoch(date(1970,1,1)); 00072 time_duration diff = given_time - time_t_epoch; 00073 00074 return (double(diff.total_seconds()) + double(diff.fractional_seconds()) / double(time_duration::ticks_per_second())); 00075 } 00076 } 00077 00079 inline boost::posix_time::ptime unix_double2ptime(double given_time) 00080 { 00081 using namespace boost::posix_time; 00082 using namespace boost::gregorian; 00083 00084 if (given_time == -1) 00085 return boost::posix_time::ptime(not_a_date_time); 00086 else 00087 { 00088 ptime time_t_epoch(date(1970,1,1)); 00089 00090 double s = floor(given_time); 00091 double micro_s = (given_time - s)*1e6; 00092 return time_t_epoch + seconds(static_cast<int>(s)) + microseconds(static_cast<long long>(micro_s)); 00093 } 00094 } 00095 00096 00098 inline boost::posix_time::ptime time_t2ptime(std::time_t t) 00099 { return boost::posix_time::from_time_t(t); } 00100 00102 inline std::time_t ptime2time_t(boost::posix_time::ptime t) 00103 { 00104 std::tm out = boost::posix_time::to_tm(t); 00105 return mktime(&out); 00106 } 00107 00109 inline double time_duration2double(boost::posix_time::time_duration time_of_day) 00110 { 00111 using namespace boost::posix_time; 00112 return (double(time_of_day.total_seconds()) + double(time_of_day.fractional_seconds()) / double(time_duration::ticks_per_second())); 00113 } 00114 00116 00117 } 00118 } 00119 #endif