- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个大型数据集,其时间戳为 UTC 时间(以毫秒为单位)。我正在将此数据集与另一个具有每小时微秒时间戳的数据集同步,因此我需要将第一个时间转换为本地时间,以每小时过去的秒数为单位。
我读过的关于这个主题的大多数类似问题都从获取当前时间的 time() 函数中获取 UTC 时间。
我已经尝试实现以下从 C++ Reference 中提取的内容.
我试图转换的时间戳是 double 的,但我不确定如何实际使用这个值。
我的数据集中的一个示例 ts:1512695257869
int main ()
{
double my_utc_ts; //value acquired from the data set
time_t rawtime;
struct tm * ptm;
time ( &rawtime ); //getting current time
//rawtime = my_utc_ts; //this is what I tried and is wrong, and results in a nullptr
ptm = gmtime ( &rawtime );
puts ("Current time around the World:");
printf ("Phoenix, AZ (U.S.) : %2d:%02d\n", (ptm->tm_hour+MST)%24, ptm->tm_min);
return 0;
}
最佳答案
After I'm able to convert it to a usable gmtime object or whatever, I need to get seconds past the hour...
<chrono>
的
Howard Hinnant's, free, open-source, C++11/14/17 timezone library 将表示自 1970-01-01 00:00:00 UTC 以来的毫秒数的 double 数转换为本地小时后的秒数:
#include "date/tz.h"
#include <iostream>
int
main()
{
using namespace std::chrono;
using namespace date;
double my_utc_ts = 1512695257869;
using ms = duration<double, std::milli>;
sys_time<milliseconds> utc_ms{round<milliseconds>(ms{my_utc_ts})};
auto loc_s = make_zoned(current_zone(), floor<seconds>(utc_ms)).get_local_time();
auto sec_past_hour = loc_s - floor<hours>(loc_s);
std::cout << utc_ms << " UTC\n";
std::cout << sec_past_hour << " past the local hour\n";
}
2017-12-08 01:07:37.869 UTC
457s past the local hour
my_utc_ts
开始。
std::chrono::duration
,它以
double
作为表示,以
milliseconds
作为精度。这种类型别名被命名为
ms
。
utc_ms
,它是一个
std::chrono::time_point<system_clock, milliseconds>
,包含 1512695257869,表示时间点 2017-12-08 01:07:37.869 UTC。到目前为止,还没有进行实际的计算。只需将
double 1512695257869
转换为表示自 1970-01-01 00:00:00 UTC 以来整数毫秒的类型。
auto loc_s = make_zoned(current_zone(), floor<seconds>(utc_ms)).get_local_time();
{time_zone, system_time}
对,使用
time_zone
作为该映射。它使用
current_zone()
查找计算机的当前时区,并将时间点
utc_ms
从毫秒精度截断到秒精度。最后,尾随的
.get_local_time()
从此映射中提取本地时间,精度为秒,并映射到当前时区。也就是说,
loc_s
是自 1970-01-01 00:00:00 UTC 以来的秒数,由您的本地时区的 UTC 偏移量抵消,该偏移量在 2017-12-08 01:07:37 UTC 有效。
loc_s
截断为小时的精度,并从
loc_s
中减去该截断的时间点,您将获得本地小时后的秒数:
auto sec_past_hour = loc_s - floor<hours>(loc_s);
utc_ms
和
sec_past_hour
。
457 == 7*60 + 37
sys_time<milliseconds> utc_ms{round<milliseconds>(ms{my_utc_ts})};
auto utc_s = floor<seconds>(utc_ms);
auto sec_past_hour = utc_s - floor<hours>(utc_s);
current_zone()
替换为您的数据库生成时使用的 IANA 时区标识符来考虑这一点,例如:
auto loc_s = make_zoned("America/New_York", floor<seconds>(utc_ms)).get_local_time();
<chrono>
库。
utc_ms
和
loc_s
上面的类型是
std::chrono::time_point
的实例化,并且
sec_past_hour
具有
std::chrono::seconds
类型(它本身就是
std::chrono::duration
的实例化)。
duration
可以使用
.count()
成员函数转换为它们的“表示”类型。对于
seconds
,此表示类型将是带符号的 64 位整数。
<chrono>
的更详细视频教程,请参阅此
Cppcon 2016 presentation 。本演示文稿将鼓励您尽可能避免使用
.count()
成员函数。
sec_past_hour
转换为
long
以便您可以将其与数据集的其他值进行比较,而是将数据集的其他值转换为
std::chrono::duration
以便您可以将它们与
sec_past_hour
进行比较。
long other_data = 123456789; // past the hour in microseconds
if (microseconds{other_data} < sec_past_hour)
// ...
<chrono>
将如何为您处理单位转换。这意味着您不会犯错误,例如当您应该乘以除以 1,000,000 时,或者用错误数量的零拼写“百万”。
关于c++ - 如何将 UTC 时间戳转换为本地时间,一小时后的秒数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47723668/
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 3 年前。 Improve th
当我使用 UTC() 函数获取纪元时间(1970 年 1 月 1 日)的总秒数时。但再次使用“new Date(milliseconds)”构造函数将相同的秒数转换为 Date 并没有给出 UTC d
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 11 年前。 Improve thi
我有一个关于 Swift 类型 TimeZone 的问题。我需要一个 UTC0 时区来格式化我的 Date 对象。我正在像这样创建 TimeZone 对象。 let utcTimeZone = Tim
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 5 年前。 Improve t
我遇到了以下代码: datetime.datetime.utcnow().replace(tzinfo=tzutc()) 我看不到 Replace() 调用正在做什么,从阅读文档来看,它似乎将其转换为
我在应用程序中将时区设置为 UTC,如下所示: // set default time zone to UTC TimeZone.setDefault(TimeZone.getTimeZone(Zon
我需要两件事: 将当前时间转换为 UTC(这样我就可以以 UTC 格式存储日期)--> result = java.util.Date. 将加载日期(UTC 格式)转换为任何时区 --> result
我想将日期从本地转换为 UTC,再将 UTC 转换为本地。 例如。 2015 年 4 月 1 日,12:00 PM 然后是 UTC 2015 年 4 月 1 日下午 5:30 对我来说是本地时间,但我
嗨,我有一个长度为几百万的字符向量( rr ),它以 %Y-%m-%d %H:%M:%S 格式表示时间和日期戳。记录在澳大利亚/悉尼。 如何获得代表这一点的 POSIXct 对象(快速)。 我找到了
static String createUTCTime() { Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC")
我正在尝试将语言环境时间转换为 UTC,然后将 UTC 转换为语言环境时间。但我没有得到结果。 public class DateDemo { public static void main(Stri
我正在寻找用纯 Javascript 替换 moment js 功能 - 我需要重新工作的项目将来不会有 moment.js 可用。我已经有一段时间没有使用 javascript Date 了,所以需
我想获取与 UTC 相关的用户时区,然后将其显示为 UTC +/- 。例如,加利福尼亚用户应显示 UTC -8(或 -7,视情况而定),而巴林用户应显示 UTC +3,等等。 下面的代码并没有告诉我它
我正在尝试将毫秒转换为 UTC 日期对象,如下所示 - var tempDate = new Date(1465171200000); // --> tempDate = Mon Jun 06 201
我一直很困惑为什么下面的代码会导致我的日期从 25 日更改为 24 日 SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy"); DateTi
我需要将字符串转换为 UTC 日期,然后将 UTC 日期转换为本地日期。 这是我的代码: var dateStr = "9/8/2015 12:44:00 PM"; console.log(strto
我正在用 PHP 编写一个 Twitter 网络服务。当用户登录时,我收到此节点: -18000 我必须更改脚本的时区,以便它适应用户的实际时区。我为此找到的唯一 php 函数是: date_defa
在 PHP 文档中,list of supported time zones , UTC 被列出两次: UTC 等/UTC 这两者之间有什么概念上的区别,还是它们只是同义词? 最佳答案 首先回答问题:
在 PHP 文档中,list of supported time zones , UTC 被列出两次: UTC 等/UTC 这两者之间有什么概念上的区别,还是它们只是同义词? 最佳答案 首先回答问题:
我是一名优秀的程序员,十分优秀!