c++ - Setting system time with gps timestamp_t structure in Linux -
setting system time gps timestamp_t structure in linux
hi everyone,
i'm trying write code succesfull gps lock (adafruit ultimate gps w/ gpsd) set system time. right now, can see 'timestamp_t':
typedef double timestamp_t; /* unix time in seconds fractional part */
is part of "gps_data_t*" structure. start, however, function planned on using set system time in linux is:
int settimeofday(const struct timeval *tv, const struct timezone *tz);
i've found information on how convert time_t *timeval, how should handle double convert *timeval?
code:
gpsmm gps_rec("localhost", default_gpsd_port); if (gps_rec.stream(watch_enable|watch_json) == null) { std::cout << "no gpsd running. exiting gps thread." << std::endl; return; } //data structure struct gps_data_t* newdata; //loop until first gps lock set system time while ((newdata = gps_rec.read()) == null) { //wait valid read } //set system time - timestamp_t vs timeval? timeval *tv{?? newdata->time ??}; timezone *tz{ timezone(300, dst_usa)}; settimeofday(tv, tz);
some comments , links have helped me. confusion differentiating between timeval, time_t, , timestamp_t. understand here differences, please correct if not case:
all time in seconds after jan 1st, 1970, but...
timeval structure of (2) long values, tv_sec seconds after 1/1/1970, tv_usec microseconds after that.
time_t long? seconds after 1/1/1970
timestamp_t double seconds after 1/1/1970, decimal part calculate microseconds 'roughly' same precision of timeval.
so conversion between of them go such:
timeval time; time_t timet; timestamp_t timestampt; timet = time.tv_sec; //usec dropped timet = timestampt; //usec dropped timestampt = timet; //usec not given timestampt = time.tv_sec; timestampt += (time.tv_usec / 1000000) //denominator may off few 0's
is painting clearer picture?
to go opposite way, needed application:
//convert gps_data_t* member 'time' timeval timeval tv; double wholeseconds, decimalseconds; decimalseconds = modf(newdata->time, &wholeseconds); tv.sec = static_cast<int32_t>(wholeseconds); tv.usec = static_cast<int32_t>(decimalseconds * 1000000.0); //create timezone timezone tz{ timezone(300, dst_usa)}; //set system time settimeofday(&tv, &tz);
here is:
//get first data set system time struct gps_data_t* firstdata; //loop until first gps lock set system time while (((firstdata = gps_rec.read()) == null) || (firstdata->fix.mode < 1)) { //do nothing } //convert gps_data_t* member 'time' timeval timeval tv; double wholeseconds, decimalseconds, offsettime; offsettime = firstdata->fix.time - (5.0 * 3600.0); //5hr offset gmt decimalseconds = modf(offsettime, &wholeseconds); tv.tv_sec = static_cast<int32_t>(wholeseconds); tv.tv_usec = static_cast<int32_t>(decimalseconds * 1000000.0); //set system time if ( settimeofday(&tv, null) >= 0) { std::cout << "time set succesful!" << '\n'; } else { std::cout << "time set failure!" << '\n'; }
Comments
Post a Comment