从午夜到日期时间的utc秒 [英] utc seconds since midnight to datetime

查看:161
本文介绍了从午夜到日期时间的utc秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将雷达数据作为轨道,轨道数据表示自上一个午夜以来的UTC秒数,显然。这不是自1970年1月1日以来的秒数。



现在我想将它转换为日期时间,因为知道计算机上的时钟可能稍微出现与雷达上的时钟同步。我将假设雷达的秒是参考,而不是计算机。
我想将这些秒转换为完整的日期时间。事情似乎有点棘手的
午夜。



有任何建议吗?我有一些想法,但我不想错过任何东西。



我使用C ++ Qt。

解决方案

  //函数扩展截断时间,给定了时间和周期,所有
//以秒为单位。
//
//示例:假设截断周期为一个小时,并且您是
//给定一小时后25分钟的截断时间。然后:
//
// o实际时间07:40:00结果在07:25:00(07:40 + -15)
// o实际时间07: 10:00结果在07:25:00(07:10 + +15)
// o实际时间07:56:00结果在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 是秒,但它的基数可以是任意的。


  • p>您需要 #include< math.h> 用于 remaining()


  • extendTruncatedTime()中的期间几乎总是二十四小时,24 * 60 * 60,根据OP的要求。也就是说,考虑到一天中的时间,它会根据墙时间添加年,月和日。


  • 我之前声明的唯一例外是,因为你提到雷达,是在Asterix CAT 1数据项I001 / 141,其中周期为512秒,其中 extendTruncatedTime()


  • 还有另一个重要的情况, extendTruncatedTime $ c>不覆盖。假设您被给予截断时间,包括月份,小时和分钟的日期。如何填写年份和月份?




以下代码段将年份和月份添加到时间源自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日。 (这可能是一个功能,而不是错误。)


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.

解决方案

//  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.)

这篇关于从午夜到日期时间的utc秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆