我有(我怀疑是)一个 UTC 日期时间。我想将其转换为我的本地时间(以America/New_York
该日期时间命名的时区)。
我试图遵循Timex
文档:
> timezone = Timezone.get("America/Chicago", Timex.now) #<TimezoneInfo(America/Chicago - CDT (-06:00:00))> > Timezone.convert(datetime, timezone) #<DateTime(2016-02-29T06:30:30.120-06:00 America/Chicago)>
为什么下面显示的第三个命令会引发错误?
iex(1)> ~N[2019-12-02 16:27:18]
~N[2019-12-02 16:27:18]
iex(2)> DateTime.from_naive(v(1), "Etc/UTC")
{:ok, #DateTime<2019-12-02 16:27:18Z>}
iex(3)> timezone = Timex.Timezone.get("America/New_York", v(2))
** (FunctionClauseError) no function clause matching in Timex.Timezone.resolve/3
The following arguments were given to Timex.Timezone.resolve/3:
# 1
"America/New_York"
# 2
{:error, :invalid_date}
# 3
:wall
Attempted function clauses (showing 1 out of 1):
def resolve(name, seconds_from_zeroyear, utc_or_wall) when is_binary(name) and is_integer(seconds_from_zeroyear) and (utc_or_wall === :wall or utc_or_wall === :utc)
(timex) lib/timezone/timezone.ex:356: Timex.Timezone.resolve/3
iex(3)> timezone = Timex.Timezone.get("America/New_York")
#<TimezoneInfo(America/New_York - EST (-05:00:00))>
第四个命令有效,但“时区”(同名)现在并不总是任何日期时间的“相同”时区。尽管这似乎模棱两可。列出tz
数据库时区的维基百科文章指出:
UTC DST 偏移量不同于观察夏令时的区域的 UTC 偏移量(有关详细信息,请参阅各个时区页面)。
这意味着时区(相对)是“固定的”;任何给定时区的任何特定日期时间的偏移量都会有所不同。
但如果这是真的,那么Timex.Timezone.get/2
即使接受日期时间值作为参数也会令人困惑。为什么时区的名称不仅足够而且完全全面?通过名称和日期时间检索时区有什么意义?