时间戳问题
我得到了一个时间戳字符串,如 3.44063E+08
。如何将其转换为正常时间,例如12:10:22等?
I got a time stamp string as this 3.44063E+08
. How can I convert it to normal time, such as 12:10:22, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
数值是
time_t
,您需要将其转换为本地时间或格林尼治时间,分别使用localtime()
或gmtime()
您可以使用strftime()
以您选择的格式进行打印。这是一个快速而完整的示例:
编辑:当您标记问题 C++ 时,请查看 Boost Date_Time 提供了很多功能。但正如我的示例所示,对于简单的“数字到文本”转换,您不需要它。
The numeric value is a
time_t
, you need to convert it to local or Greenwhich time whichlocaltime()
orgmtime()
, respectively, after which you can usestrftime()
to print in a format you choose.Here is a quick and complete example:
Edit: And as you tagged your question C++, have a look at Boost Date_Time which has a lot to offer to. But as my example showed, for the simple 'number to text' conversion, you do not need it.
指数格式的时间戳意味着有人正在使用浮点时间计数器,并且在编写打印代码时没有注意。如果您拥有的只是这段文本,您将无法将其转换为准确的时间,因为它只能精确到 1000 秒(大约 20 分钟)。如果您确实拥有双精度原件,则可以从中获得准确的时间。如果你把它当作漂浮物,上帝会帮助你。
该数字(可能)是 time_t,它是自 Unix 纪元(1970 年 1 月 1 日)以来的秒数。您可以使用 gmtime() 或 localtime() 将其转换为细分的时间结构(年、月等),然后使用 strftime() 转换为您想要的任何文本格式。如果您只有文本而不是实际数字,则必须首先使用 strtod() 将其解析为数字。
A timestamp in exponential format means that someone was using floating point time counters and wasn't paying attention when they wrote the code to print it. If all you have is that piece of text, you're not going to be able to convert it to an accurate time, since it's only accurate to 1000 seconds (about 20 minutes). If you actually have the original as a double, you can get an accurate time from it. If you have it as a float, god help you.
The number is (probably) a time_t, which is a count of seconds since the Unix epoch (1 January 1970). You can convert it to a broken-down time structure (year, month, etc) using gmtime() or localtime(), and then to whatever text format you want using strftime(). If you only have the text rather than an actual number, you'll have to parse it to a number first using strtod().
更多上下文会很有用,但假设您已经拥有它,3.44063E+08 是 344063000,作为 time_t 转换为 1980-11-26 05:03:20 (utc)。如果您认为这作为时间戳有意义,那么它就是 time_t:自 1970-01-01 以来的秒数。
另一种可能性是,它来自 Excel 或其他 Microsoft Office 产品,这些产品将日期存储为 1900 年(某些情况下为 1899 年)以来的天数。这将转换为 943,893 年的某个日期。如果这是有道理的,比如您正在进行天文或慢速放射性同位素计算等,那么现在是一天的早些时候,大约 03:42。
A lot more context would be useful, but assuming you already have it, 3.44063E+08 is 344063000 which, as a time_t converts to 1980-11-26 05:03:20 (utc). If that makes sense to you as a timestamp, then it is a time_t: number of seconds since 1970-01-01.
Another possibility is that it came from Excel or another Microsoft Office product, which stores dates as days since 1900, or 1899 in some cases. That converts to some date in the year 943,893. If that makes sense, like if you are doing astronomical or slow radioisotope calculations, etc., then it is early in the day, about 03:42.