如何将计时`DateTime< UTC>`实例转换为`DateTime< Local>`? [英] How do I convert a chrono `DateTime<UTC>` instance to `DateTime<Local>`?
问题描述
我的目标是将 utc
转换为 loc
:
use chrono::{Local, UTC, TimeZone};
let utc = chrono::UTC::now();
let loc = chrono::Local::now();
println!("{:?}", utc);
println!("{:?}", loc);
println!("{:?}", utc.with_timezone(&Local));
println!("{:?}", Local.from_utc_datetime(&utc.naive_local()));
...产生以下输出:
... which produced the following output:
2015-02-26T16:22:27.873593Z
2015-02-26T17:22:27.873663+01:00
2015-02-26T15:22:27.873593+00:00
2015-02-26T15:22:27.873593+00:00
第二行中显示的 loc
时间是我转换 utc $ c $时要查看的时间c>。
The loc
time shown in the second row is what I want to see when converting utc
.
如何正确地将 DateTime< UTC>
实例转换为 DateTime< ; Local>
?
How do I properly convert a DateTime<UTC>
instance to DateTime<Local>
?
我正在使用 chrono 0.2.2 。在 DateTime中。 from_utc
方法甚至告诉我我应该使用 TimeZone
特性。但是,我缺少了一些东西。
I am using chrono 0.2.2. In the DateTime.from_utc
method it's even telling me I should use the TimeZone
trait. However, I am missing something.
推荐答案
糟糕,感谢您的举报。这是一个错误,并已记录为问题#26 。这应该在Chrono 0.2.3。中修复。
Oops, thank you for reporting. This is a bug and registered as the issue #26. This should be fixed in Chrono 0.2.3.
除了该错误外, utc.with_timezone(& Local)
确实是转换为当地时间的正确方法。有一个重要的身份,就是 utc.with_timezone(& Local).with_timezone(& UTC)
应该等于 utc
(特殊情况下,当地时区已更改)。
Besides from the bug, utc.with_timezone(&Local)
is indeed a correct way to convert to the local time. There is an important identity that utc.with_timezone(&Local).with_timezone(&UTC)
should be equal to utc
(except for the exceptional case, where the local time zone has been changed).
这篇关于如何将计时`DateTime< UTC>`实例转换为`DateTime< Local>`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!