我正在获取作为“轨迹”的雷达数据,而轨迹数据显然表示自上次午夜以来的 UTC 秒数。这不是自 1970 年 1 月 1 日以来的秒数。
现在我想将其转换为日期时间,因为我知道计算机上的时钟可能与雷达上的时钟略微不同步。我假设雷达的秒数是引用,而不是计算机的。 我想将这些秒数转换为完整的日期时间。周围的事情似乎有点棘手 午夜。
有什么建议吗?我有一些想法,但我不想错过任何东西。
我正在使用 C++ Qt。
最佳答案
// 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)
一些评论:
wall
的单位是秒,但它的基数可以是任意的。在 Unix 中,它通常从 1970 开始。闰秒在这里无关紧要。
你需要
#include <math.h>
对于remainder()
.period
在extendTruncatedTime()
根据 OP 的要求,几乎总是二十四小时,24 * 60 * 60。也就是说,给定一天中的时间,它会根据“墙上”时间添加年、月和月中的日期来扩展它。我知道前面声明的唯一异常(exception)是,因为你提到了雷达,在 Asterix CAT 1 数据项 I001/141 中,其中周期为 512 秒,
extendTruncatedTime()
给定的不太管用。还有一个重要案例
extendTruncatedTime()
不包括。假设给定了一个由月中的第几天、小时和分钟组成的截断时间。年月怎么填?
以下代码片段将年份和月份添加到从 DDHHMM 格式派生的时间:
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);
}
如所写,这不会处理错误的输入。例如,如果今天是 7 月 4 日,那么没有意义的 310000
将悄悄地转换到 7 月 1 日。 (这可能是一项功能,而不是错误。)
关于c++ - 从午夜到日期时间的 utc 秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12859258/