From 8e3ea14c9406f3f3e5fe613bfe59906447a195e0 Mon Sep 17 00:00:00 2001
From: Martin Braun <martin.braun@ettus.com>
Date: Thu, 16 Aug 2018 14:25:24 -0700
Subject: uhd: Remove usage of time_t (except when required)

The C/C++ standards don't define what time_t is, only that it is
arithmetic (and real for C11, and integral for C++). It should not be
used in portable software and is only used as the return value for some
libc calls.
A common definition for time_t is int64_t, so we'll switch to that
permanently in our own APIs. System APIs will of course stick with
time_t.
---
 host/docs/c_api.dox                  |  2 +-
 host/examples/rx_samples_c.c         |  4 ++--
 host/examples/sync_to_gps.cpp        |  4 ++--
 host/examples/test_clock_synch.cpp   |  6 +++---
 host/include/uhd/types/metadata.h    |  8 ++++----
 host/include/uhd/types/time_spec.hpp | 12 +++++++-----
 host/include/uhd/usrp/usrp.h         | 14 +++++++-------
 host/lib/types/metadata_c.cpp        |  8 ++++----
 host/lib/types/time_spec.cpp         |  8 ++++----
 host/lib/usrp/gps_ctrl.cpp           |  2 +-
 host/lib/usrp/gpsd_iface.cpp         |  2 +-
 host/lib/usrp/usrp_c.cpp             | 12 ++++++------
 host/lib/utils/system_time.cpp       |  2 +-
 13 files changed, 43 insertions(+), 41 deletions(-)

(limited to 'host')

diff --git a/host/docs/c_api.dox b/host/docs/c_api.dox
index 5b5790f21..c314960d3 100644
--- a/host/docs/c_api.dox
+++ b/host/docs/c_api.dox
@@ -33,7 +33,7 @@ uhd_rx_metadata_handle md;
 uhd_rx_metadata_make(&md);
 
 // Streaming here puts useful information into metadata
-time_t full_secs;
+int64_t full_secs;
 double frac_secs;
 uhd_rx_metadata_time_spec(md, &full_secs, &frac_secs);
 
diff --git a/host/examples/rx_samples_c.c b/host/examples/rx_samples_c.c
index b31450184..1cabafc9d 100644
--- a/host/examples/rx_samples_c.c
+++ b/host/examples/rx_samples_c.c
@@ -223,12 +223,12 @@ int main(int argc, char* argv[])
         // Handle data
         fwrite(buff, sizeof(float) * 2, num_rx_samps, fp);
         if (verbose) {
-            time_t full_secs;
+            int64_t full_secs;
             double frac_secs;
             uhd_rx_metadata_time_spec(md, &full_secs, &frac_secs);
             fprintf(stderr, "Received packet: %zu samples, %.f full secs, %f frac secs\n",
                     num_rx_samps,
-                    difftime(full_secs, (time_t) 0),
+                    difftime(full_secs, (int64_t) 0),
                     frac_secs);
         }
 
diff --git a/host/examples/sync_to_gps.cpp b/host/examples/sync_to_gps.cpp
index 0b7f030bc..bb0f77f61 100644
--- a/host/examples/sync_to_gps.cpp
+++ b/host/examples/sync_to_gps.cpp
@@ -111,7 +111,7 @@ int UHD_SAFE_MAIN(int argc, char *argv[])
             }
 
             //Set to GPS time
-            uhd::time_spec_t gps_time = uhd::time_spec_t(time_t(usrp->get_mboard_sensor("gps_time", mboard).to_int()));
+            uhd::time_spec_t gps_time = uhd::time_spec_t(int64_t(usrp->get_mboard_sensor("gps_time", mboard).to_int()));
             usrp->set_time_next_pps(gps_time+1.0, mboard);
 
             //Wait for it to apply
@@ -121,7 +121,7 @@ int UHD_SAFE_MAIN(int argc, char *argv[])
             std::this_thread::sleep_for(std::chrono::seconds(2));
 
             //Check times
-            gps_time = uhd::time_spec_t(time_t(usrp->get_mboard_sensor("gps_time", mboard).to_int()));
+            gps_time = uhd::time_spec_t(int64_t(usrp->get_mboard_sensor("gps_time", mboard).to_int()));
             uhd::time_spec_t time_last_pps = usrp->get_time_last_pps(mboard);
             std::cout << "USRP time: " << (boost::format("%0.9f") % time_last_pps.get_real_secs()) << std::endl;
             std::cout << "GPSDO time: " << (boost::format("%0.9f") % gps_time.get_real_secs()) << std::endl;
diff --git a/host/examples/test_clock_synch.cpp b/host/examples/test_clock_synch.cpp
index 38bc4980a..a0b67a95b 100644
--- a/host/examples/test_clock_synch.cpp
+++ b/host/examples/test_clock_synch.cpp
@@ -25,7 +25,7 @@ namespace po = boost::program_options;
 using namespace uhd::usrp_clock;
 using namespace uhd::usrp;
 
-void get_usrp_time(multi_usrp::sptr usrp, size_t mboard, std::vector<time_t> *times){
+void get_usrp_time(multi_usrp::sptr usrp, size_t mboard, std::vector<int64_t> *times){
     (*times)[mboard] = usrp->get_time_now(mboard).get_full_secs();
 }
 
@@ -94,7 +94,7 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
 
     //Get GPS time to initially set USRP devices
     std::cout << std::endl << "Querying Clock for time and setting USRP times..." << std::endl << std::endl;
-    time_t clock_time = clock->get_time();
+    int64_t clock_time = clock->get_time();
     usrp->set_time_next_pps(uhd::time_spec_t(double(clock_time+1)));
     srand((unsigned int)time(NULL));
 
@@ -106,7 +106,7 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
         std::this_thread::sleep_for(std::chrono::milliseconds(wait_time));
 
         //Get all times before output
-        std::vector<time_t> usrp_times(usrp->get_num_mboards());
+        std::vector<int64_t> usrp_times(usrp->get_num_mboards());
         boost::thread_group thread_group;
         clock_time = clock->get_time();
         for(size_t j = 0; j < usrp->get_num_mboards(); j++){
diff --git a/host/include/uhd/types/metadata.h b/host/include/uhd/types/metadata.h
index a711731d4..623adb77f 100644
--- a/host/include/uhd/types/metadata.h
+++ b/host/include/uhd/types/metadata.h
@@ -113,7 +113,7 @@ UHD_API uhd_error uhd_rx_metadata_has_time_spec(
 //! Time of first sample
 UHD_API uhd_error uhd_rx_metadata_time_spec(
     uhd_rx_metadata_handle h,
-    time_t *full_secs_out,
+    int64_t *full_secs_out,
     double *frac_secs_out
 );
 
@@ -212,7 +212,7 @@ UHD_API uhd_error uhd_rx_metadata_last_error(
 UHD_API uhd_error uhd_tx_metadata_make(
     uhd_tx_metadata_handle* handle,
     bool has_time_spec,
-    time_t full_secs,
+    int64_t full_secs,
     double frac_secs,
     bool start_of_burst,
     bool end_of_burst
@@ -236,7 +236,7 @@ UHD_API uhd_error uhd_tx_metadata_has_time_spec(
 //! Get time specification
 UHD_API uhd_error uhd_tx_metadata_time_spec(
     uhd_tx_metadata_handle h,
-    time_t *full_secs_out,
+    int64_t *full_secs_out,
     double *frac_secs_out
 );
 
@@ -316,7 +316,7 @@ UHD_API uhd_error uhd_async_metadata_has_time_spec(
 //! Get time specification
 UHD_API uhd_error uhd_async_metadata_time_spec(
     uhd_async_metadata_handle h,
-    time_t *full_secs_out,
+    int64_t *full_secs_out,
     double *frac_secs_out
 );
 
diff --git a/host/include/uhd/types/time_spec.hpp b/host/include/uhd/types/time_spec.hpp
index 32810c6e9..54fd9d379 100644
--- a/host/include/uhd/types/time_spec.hpp
+++ b/host/include/uhd/types/time_spec.hpp
@@ -40,7 +40,7 @@ namespace uhd{
          * \param full_secs the whole/integer seconds count
          * \param frac_secs the fractional seconds count (default = 0)
          */
-        time_spec_t(time_t full_secs, double frac_secs = 0);
+        time_spec_t(int64_t full_secs, double frac_secs = 0);
 
         /*!
          * Create a time_spec_t from whole seconds and fractional ticks.
@@ -49,7 +49,7 @@ namespace uhd{
          * \param tick_count the fractional seconds tick count
          * \param tick_rate the number of ticks per second
          */
-        time_spec_t(time_t full_secs, long tick_count, double tick_rate);
+        time_spec_t(int64_t full_secs, long tick_count, double tick_rate);
 
         /*!
          * Create a time_spec_t from a 64-bit tick count.
@@ -87,7 +87,7 @@ namespace uhd{
          * Get the whole/integer part of the time in seconds.
          * \return the whole/integer seconds
          */
-        time_t get_full_secs(void) const;
+        int64_t get_full_secs(void) const;
 
         /*!
          * Get the fractional part of the time in seconds.
@@ -104,7 +104,9 @@ namespace uhd{
         time_spec_t &operator-=(const time_spec_t &);
 
     //private time storage details
-    private: time_t _full_secs; double _frac_secs;
+    private:
+        int64_t _full_secs;
+        double _frac_secs;
     };
 
     //! Implement equality_comparable interface
@@ -113,7 +115,7 @@ namespace uhd{
     //! Implement less_than_comparable interface
     UHD_API bool operator<(const time_spec_t &, const time_spec_t &);
 
-    UHD_INLINE time_t time_spec_t::get_full_secs(void) const{
+    UHD_INLINE int64_t time_spec_t::get_full_secs(void) const{
         return this->_full_secs;
     }
 
diff --git a/host/include/uhd/usrp/usrp.h b/host/include/uhd/usrp/usrp.h
index f8f81cc31..0ede389ef 100644
--- a/host/include/uhd/usrp/usrp.h
+++ b/host/include/uhd/usrp/usrp.h
@@ -83,7 +83,7 @@ typedef struct {
     //! Stream now?
     bool stream_now;
     //! If not now, then full seconds into future to stream
-    time_t time_spec_full_secs;
+    int64_t time_spec_full_secs;
     //! If not now, then fractional seconds into future to stream
     double time_spec_frac_secs;
 } uhd_stream_cmd_t;
@@ -416,7 +416,7 @@ UHD_API uhd_error uhd_usrp_get_mboard_name(
 UHD_API uhd_error uhd_usrp_get_time_now(
     uhd_usrp_handle h,
     size_t mboard,
-    time_t *full_secs_out,
+    int64_t *full_secs_out,
     double *frac_secs_out
 );
 
@@ -427,7 +427,7 @@ UHD_API uhd_error uhd_usrp_get_time_now(
 UHD_API uhd_error uhd_usrp_get_time_last_pps(
     uhd_usrp_handle h,
     size_t mboard,
-    time_t *full_secs_out,
+    int64_t *full_secs_out,
     double *frac_secs_out
 );
 
@@ -437,7 +437,7 @@ UHD_API uhd_error uhd_usrp_get_time_last_pps(
  */
 UHD_API uhd_error uhd_usrp_set_time_now(
     uhd_usrp_handle h,
-    time_t full_secs,
+    int64_t full_secs,
     double frac_secs,
     size_t mboard
 );
@@ -448,7 +448,7 @@ UHD_API uhd_error uhd_usrp_set_time_now(
  */
 UHD_API uhd_error uhd_usrp_set_time_next_pps(
     uhd_usrp_handle h,
-    time_t full_secs,
+    int64_t full_secs,
     double frac_secs,
     size_t mboard
 );
@@ -459,7 +459,7 @@ UHD_API uhd_error uhd_usrp_set_time_next_pps(
  */
 UHD_API uhd_error uhd_usrp_set_time_unknown_pps(
     uhd_usrp_handle h,
-    time_t full_secs,
+    int64_t full_secs,
     double frac_secs
 );
 
@@ -475,7 +475,7 @@ UHD_API uhd_error uhd_usrp_get_time_synchronized(
  */
 UHD_API uhd_error uhd_usrp_set_command_time(
     uhd_usrp_handle h,
-    time_t full_secs,
+    int64_t full_secs,
     double frac_secs,
     size_t mboard
 );
diff --git a/host/lib/types/metadata_c.cpp b/host/lib/types/metadata_c.cpp
index 352e5278f..35e3d76c9 100644
--- a/host/lib/types/metadata_c.cpp
+++ b/host/lib/types/metadata_c.cpp
@@ -43,7 +43,7 @@ uhd_error uhd_rx_metadata_has_time_spec(
 
 uhd_error uhd_rx_metadata_time_spec(
     uhd_rx_metadata_handle h,
-    time_t *full_secs_out,
+    int64_t *full_secs_out,
     double *frac_secs_out
 ){
     UHD_SAFE_C_SAVE_ERROR(h,
@@ -149,7 +149,7 @@ uhd_error uhd_rx_metadata_last_error(
 uhd_error uhd_tx_metadata_make(
     uhd_tx_metadata_handle* handle,
     bool has_time_spec,
-    time_t full_secs,
+    int64_t full_secs,
     double frac_secs,
     bool start_of_burst,
     bool end_of_burst
@@ -185,7 +185,7 @@ uhd_error uhd_tx_metadata_has_time_spec(
 
 uhd_error uhd_tx_metadata_time_spec(
     uhd_tx_metadata_handle h,
-    time_t *full_secs_out,
+    int64_t *full_secs_out,
     double *frac_secs_out
 ){
     UHD_SAFE_C_SAVE_ERROR(h,
@@ -265,7 +265,7 @@ uhd_error uhd_async_metadata_has_time_spec(
 
 uhd_error uhd_async_metadata_time_spec(
     uhd_async_metadata_handle h,
-    time_t *full_secs_out,
+    int64_t *full_secs_out,
     double *frac_secs_out
 ){
     UHD_SAFE_C_SAVE_ERROR(h,
diff --git a/host/lib/types/time_spec.cpp b/host/lib/types/time_spec.cpp
index 907e97d1a..403c3336c 100644
--- a/host/lib/types/time_spec.cpp
+++ b/host/lib/types/time_spec.cpp
@@ -14,7 +14,7 @@ using namespace uhd;
  * Time spec constructors
  **********************************************************************/
 #define time_spec_init(full, frac) { \
-    const time_t _full = time_t(full); \
+    const int64_t _full = int64_t(full); \
     const double _frac = double(frac); \
     const int _frac_int = int(_frac); \
     _full_secs = _full + _frac_int; \
@@ -33,11 +33,11 @@ time_spec_t::time_spec_t(double secs){
     time_spec_init(0, secs);
 }
 
-time_spec_t::time_spec_t(time_t full_secs, double frac_secs){
+time_spec_t::time_spec_t(int64_t full_secs, double frac_secs){
     time_spec_init(full_secs, frac_secs);
 }
 
-time_spec_t::time_spec_t(time_t full_secs, long tick_count, double tick_rate){
+time_spec_t::time_spec_t(int64_t full_secs, long tick_count, double tick_rate){
     const double frac_secs = tick_count/tick_rate;
     time_spec_init(full_secs, frac_secs);
 }
@@ -45,7 +45,7 @@ time_spec_t::time_spec_t(time_t full_secs, long tick_count, double tick_rate){
 time_spec_t time_spec_t::from_ticks(long long ticks, double tick_rate){
     const long long rate_i = (long long)(tick_rate);
     const double rate_f = tick_rate - rate_i;
-    const time_t secs_full = time_t(ticks/rate_i);
+    const int64_t secs_full = int64_t(ticks/rate_i);
     const long long ticks_error = ticks - (secs_full*rate_i);
     const double ticks_frac = ticks_error - secs_full*rate_f;
     return time_spec_t(secs_full, ticks_frac/tick_rate);
diff --git a/host/lib/usrp/gps_ctrl.cpp b/host/lib/usrp/gps_ctrl.cpp
index 67803bec6..717654151 100644
--- a/host/lib/usrp/gps_ctrl.cpp
+++ b/host/lib/usrp/gps_ctrl.cpp
@@ -372,7 +372,7 @@ private:
     return gps_time; //keep gcc from complaining
   }
 
-  time_t get_epoch_time(void) {
+  int64_t get_epoch_time(void) {
       return (get_time() - from_time_t(0)).total_seconds();
   }
 
diff --git a/host/lib/usrp/gpsd_iface.cpp b/host/lib/usrp/gpsd_iface.cpp
index 6b47b6be5..b34132fa0 100644
--- a/host/lib/usrp/gpsd_iface.cpp
+++ b/host/lib/usrp/gpsd_iface.cpp
@@ -131,7 +131,7 @@ private: // member functions
         return _gps_data.fix.mode >= MODE_2D;
     }
 
-    std::time_t _epoch_time(void)
+    int64_t _epoch_time(void)
     {
         boost::shared_lock<boost::shared_mutex> l(_d_mutex);
         return (boost::posix_time::from_time_t(_gps_data.fix.time)
diff --git a/host/lib/usrp/usrp_c.cpp b/host/lib/usrp/usrp_c.cpp
index 1eb52d9af..94f0f4eba 100644
--- a/host/lib/usrp/usrp_c.cpp
+++ b/host/lib/usrp/usrp_c.cpp
@@ -435,7 +435,7 @@ uhd_error uhd_usrp_get_mboard_name(
 uhd_error uhd_usrp_get_time_now(
     uhd_usrp_handle h,
     size_t mboard,
-    time_t *full_secs_out,
+    int64_t *full_secs_out,
     double *frac_secs_out
 ){
     UHD_SAFE_C_SAVE_ERROR(h,
@@ -448,7 +448,7 @@ uhd_error uhd_usrp_get_time_now(
 uhd_error uhd_usrp_get_time_last_pps(
     uhd_usrp_handle h,
     size_t mboard,
-    time_t *full_secs_out,
+    int64_t *full_secs_out,
     double *frac_secs_out
 ){
     UHD_SAFE_C_SAVE_ERROR(h,
@@ -460,7 +460,7 @@ uhd_error uhd_usrp_get_time_last_pps(
 
 uhd_error uhd_usrp_set_time_now(
     uhd_usrp_handle h,
-    time_t full_secs,
+    int64_t full_secs,
     double frac_secs,
     size_t mboard
 ){
@@ -472,7 +472,7 @@ uhd_error uhd_usrp_set_time_now(
 
 uhd_error uhd_usrp_set_time_next_pps(
     uhd_usrp_handle h,
-    time_t full_secs,
+    int64_t full_secs,
     double frac_secs,
     size_t mboard
 ){
@@ -484,7 +484,7 @@ uhd_error uhd_usrp_set_time_next_pps(
 
 uhd_error uhd_usrp_set_time_unknown_pps(
     uhd_usrp_handle h,
-    time_t full_secs,
+    int64_t full_secs,
     double frac_secs
 ){
     UHD_SAFE_C_SAVE_ERROR(h,
@@ -505,7 +505,7 @@ uhd_error uhd_usrp_get_time_synchronized(
 
 uhd_error uhd_usrp_set_command_time(
     uhd_usrp_handle h,
-    time_t full_secs,
+    int64_t full_secs,
     double frac_secs,
     size_t mboard
 ){
diff --git a/host/lib/utils/system_time.cpp b/host/lib/utils/system_time.cpp
index 2434eb949..20b6dc429 100644
--- a/host/lib/utils/system_time.cpp
+++ b/host/lib/utils/system_time.cpp
@@ -45,7 +45,7 @@ time_spec_t uhd::get_system_time(void){
     pt::ptime time_now = pt::microsec_clock::universal_time();
     pt::time_duration time_dur = time_now - pt::from_time_t(0);
     return time_spec_t(
-        time_t(time_dur.total_seconds()),
+        int64_t(time_dur.total_seconds()),
         long(time_dur.fractional_seconds()),
         double(pt::time_duration::ticks_per_second())
     );
-- 
cgit v1.2.3