将相同时间转换为不同时区 [英] Convert same time to different time zone
问题描述
我正在尝试将时间转换为不同的时区,但不是您想的那样.例如,我需要将美国东部标准时间上午 9 点的日期时间转换为 UTC 时间上午 9 点.时区是可变的,因此仅添加/减去时间似乎不是使用 NodaTime 的正确方法
I am trying to convert times to different time zones, but not the way you're thinking. I need to convert a DateTime that is 9am EST to 9am CST on the UTC for example. The timezones are variable so just adding/subtracting hours doesn't seem correct way to do it with NodaTime
Fri, 21 Feb 2014 21:00:00 EST = 1393034400 Epoch Timestamp
convert to
Fri, 21 Feb 2014 21:00:00 CST = 1393030800 Epoch Timestamp
推荐答案
如果我正确理解了这个问题,那么听起来您像是在尝试将一个时区中的日期/时间转换为具有相同本地时间的另一个时区和不同的时区;也就是说,不同的时间点.
If I understand the question correctly, it sounds like you're trying to convert a date/time in one time zone to another one that has the same local time and a different time zone; that is, a different point in time.
您可以通过将 LocalDateTime
与新区域结合使用 Noda Time 来实现这一点.例如,给定如下内容:
You can do this with Noda Time by combining the LocalDateTime
with the new zone. For example, given something like the following:
Instant now = SystemClock.Instance.Now;
DateTimeZone eastern = DateTimeZoneProviders.Tzdb["America/New_York"];
ZonedDateTime nowEastern = now.InZone(eastern);
nowEastern
是 America/New_York
时区的现在时间.如果我们将 nowEastern
直接打印到控制台,我们会看到类似 2014-02-22T05:18:50 America/New_York (-05)
的内容.
nowEastern
is the time now in the America/New_York
time zone. If we print nowEastern
directly to the console, we'll see something like 2014-02-22T05:18:50 America/New_York (-05)
.
顺便说一句,EST"和CST"不是时区:它们是时区内特定偏移量的非唯一缩写;America/New_York
和 America/Chicago
可能代表了我们认为的东部"和中部"(或者你可以使用类似 UTC-05:00
如果您真的想要 EST,即使在夏令时生效的情况下).
As an aside, "EST" and "CST" aren't time zones: they're non-unique abbreviations for a particular offset within a time zone; America/New_York
and America/Chicago
are probably representative of what we think of as "Eastern" and "Central", though (or you could use something like UTC-05:00
if you really wanted EST even when daylight savings time was in effect).
给定一个在任何时区的ZonedDateTime
,我们可以将其转换为一个具有相同本地时间和指定时区的ZonedDateTime
,如下所示:
Given a ZonedDateTime
in any time zone, we can convert it to a ZonedDateTime
with the same local time and a specified time zone as follows:
DateTimeZone central = DateTimeZoneProviders.Tzdb["America/Chicago"];
ZonedDateTime sameLocalTimeCentral = nowEastern.LocalDateTime.InZoneStrictly(central);
这为我们提供了一个具有相同本地时间但不同时区的 ZonedDateTime
.使用上面的输入,结果将是 2014-02-22T05:18:50 America/Chicago (-06)
.
This gives us a ZonedDateTime
with the same local time, but a different time zone. With the input above, the result would be 2014-02-22T05:18:50 America/Chicago (-06)
.
请注意,我使用的是 InZoneStrictly
.如果本地时间不明确或无效(例如,在白天储蓄过渡).如果这是不可接受的,您可以使用 InZoneLeniently
,它在给定的本地时间或之后选择最早的有效 ZonedDateTime
,或 InZone
,它允许您在这些情况下指定您自己的规则.
Note that I'm using InZoneStrictly
. This will throw an exception if the local time is ambiguous or invalid (for example, during daylight savings transitions). If that's unacceptable, you could use InZoneLeniently
, which picks the earliest valid ZonedDateTime
on or after the given local time, or InZone
, which allows you to specify your own rules in those cases.
这篇关于将相同时间转换为不同时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!