uint64 UTC时间 [英] uint64 UTC time
问题描述
我有一个UTC日期时间没有格式存储在uint64,即: 20090520145024798
我需要得到小时,分钟,秒和毫秒的时间。我可以很容易地通过将其转换为字符串和使用子字符串。然而,这个代码需要非常快,所以我想避免字符串操作。有更快的方式,也许使用位操作来做到这一点?
I have a UTC date time without the formatting stored in a uint64, ie: 20090520145024798
I need to get the hours, minutes, seconds and milliseconds out of this time. I can do this very easily by converting it to a string and using substring. However, this code needs to be very fast so I would like to avoid string manipulations. Is there faster way, perhaps using bit manipulation to do this? Oh by the way this needs to be done in C++ on Linux.
推荐答案
uint64 u = 20090520145024798;
unsigned long w = u % 1000000000;
unsigned millisec = w % 1000;
w /= 1000;
unsigned sec = w % 100;
w /= 100;
unsigned min = w % 100;
unsigned hour = w / 100;
unsigned long v = w / 1000000000;
unsigned day = v % 100;
v /= 100;
unsigned month = v % 100;
unsigned year = v / 100;
此解决方案从 uint64 u
到 unsigned long w
(和 v
)是中间的YYYYMMDD和HHMMSSIII适合32位,在某些系统上,32位除法比64位除法更快。
The reason why this solution switches from uint64 u
to unsigned long w
(and v
) in the middle is that the YYYYMMDD and HHMMSSIII fit to 32 bits, and 32-bit division is faster than 64-bit division on some systems.
这篇关于uint64 UTC时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!