Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

utc seconds since midnight to datetime

Tags:

c++

datetime

utc

qt

I'm getting radar data as "tracks" and the track data indicates the number of UTC seconds since the last midnight, apparently. This is not the number of seconds since the 1st of jan 1970.

Now I want to convert that to date time, knowing that the clock on the computer could be slightly out of sync with the clock on the radar. I'll assume the radar's seconds are the reference, not the computer's. I want to convert these seconds to a full date time. Things seem to be a little tricky around midnight.

Any suggestions? I've got some ideas, but I don't want to miss anything.

I'm working with C++ Qt.

like image 977
user1741137 Avatar asked Nov 13 '22 20:11

user1741137


1 Answers

//  Function to extend truncated time, given the wall time and period, all
//  in units of seconds.
//
//  Example: Suppose the truncated period was one hour, and you were
//  given a truncated time of 25 minutes after the hour. Then:
//
//  o Actual time of 07:40:00 results in 07:25:00 (07:40 + -15)
//  o Actual time of 07:10:00 results in 07:25:00 (07:10 + +15)
//  o Actual time of 07:56:00 results in 08:25:00 (07:56 + +29)

double extendTruncatedTime(double trunc, double wall, int period) {
   return wall + remainder(trunc - wall, period);
}

#define extendTruncatedTime24(t) extendTruncatedTime(t, time(0), 24 * 60 * 60)

Some commentary:

  • The units of wall are seconds, but its base can be arbitrary. In Unix, it typically starts at 1970.

  • Leap seconds are not relevant here.

  • You need #include <math.h> for remainder().

  • The period in extendTruncatedTime() is almost always twenty-four hours, 24 * 60 * 60, as per the OP's request. That is, given the time of day, it extends it by adding the year, month, and day of month, based on the 'wall' time.

  • The only exception I know to the previous statement is, since you mention radar, is in the Asterix CAT 1 data item I001/141, where the period is 512 seconds, and for which extendTruncatedTime() as given doesn't quite work.

  • And there is another important case which extendTruncatedTime() doesn't cover. Suppose you are given a truncated time consisting of the day of month, hour, and minute. How can you fill in the year and the month?

The following code snippet adds the year and month to a time derived from a DDHHMM format:

time_t extendTruncatedTimeDDHHMM(time_t trunc, time_t wall) {
   struct tm retval = *gmtime_r(&trunc, &retval);
   struct tm now    = *gmtime_r(&wall,  &now);
   retval.tm_year = now.tm_year;
   retval.tm_mon  = now.tm_mon;
   retval.tm_mon += now.tm_mday - retval.tm_mday >  15; // 15 = half-month
   retval.tm_mon -= now.tm_mday - retval.tm_mday < -15;
   return timegm(&retval);
}

As written, this doesn't handle erroneous inputs. For example, if today is July 4th, then the non-nonsensical 310000 will be quietly converted to July 1st. (This may be a feature, not a bug.)

like image 155
Joseph Quinsey Avatar answered Nov 15 '22 11:11

Joseph Quinsey