我得到雷达数据作为“跟踪”和跟踪数据表明世界协调时的秒数自上个午夜,显然。这不是1970年1月1日以来的秒数。
现在我想把它转换成日期,因为我知道计算机上的时钟可能与雷达上的时钟有点不同步。我假设雷达的秒是参考,而不是计算机的。我想把这些秒转换成一个完整的日期。午夜前后的事情似乎有点棘手。
有什么建议吗?我有一些想法,但我不想错过任何东西。
我正在和C++ Qt合作。
发布于 2012-10-12 13:00:14
// 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 in extendTruncatedTime()几乎总是24小时,24* 60 * 60。也就是说,给定一天中的时间,它扩展了它,根据“墙壁”时间添加了月份的年份、月份和日期。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日。(这可能是一个特性,而不是一个bug。)
发布于 2012-10-12 12:43:33
如果您可以链接到另一个库,我建议使用时间。
您似乎希望从午夜(时代)开始,在几秒钟内获取当前日期,然后将雷达时间添加到其中,然后将和转换回日期时间,并将其转换为字符串。
使用boost可以帮助您:
因为你会有时间间隔和持续时间这样的概念供你支配。您可以使用类似的东西(从boost示例中):
ptime t4(date(2002,May,31), hours(20)); //4 hours b/f midnight NY time
ptime t5 = us_eastern::local_to_utc(t4);
std::cout << to_simple_string(t4) << " in New York is "
<< to_simple_string(t5) << " UTC time "
<< std::endl;如果您想手工计算漂移,您可以很容易地进行时间计算,类似于这样的构造:
ptime t2 = t1 - hours(5)- minutes(4)- seconds(2)- millisec(1);发布于 2022-03-17 20:00:53
我也遇到了同样的问题,但我正在使用C#。如果有人需要C#中的解决方案,我的实现就包含在这里。这不包括任何时钟漂移。
DateTime UTCTime = DateTime.UtcNow.Date.AddSeconds(secondSinceMidnightFromRadar);https://stackoverflow.com/questions/12859258
复制相似问题