elixir中的utc_offset返回时区的偏移量不正确



我可能不理解这里的一些内容,但这看起来可能更直接。

iex> {:ok, datetime_with_tz} = DateTime.now("Europe/London", Tzdata.TimeZoneDatabase)
{:ok, #DateTime<2020-05-02 21:57:11.136512+01:00 BST Europe/London>}
iex> DateTime.utc_now
~U[2020-05-02 20:57:21.869835Z]
//correct as it's already in utc
iex> DateTime.utc_now.utc_offset
0 
// incorrect this should be 3600 i.e +01:00 hours of offset and not 0 .. ?
iex> datetime_with_tz.utc_offset
0  

这是因为伦敦通常是UTC+00:00,目前是夏令时+01:00(BST代表英国夏令时(。

DateTime有std_offset来处理这个问题。

iex(1)> {:ok, datetime_with_tz} = DateTime.now("Europe/London", Tzdata.TimeZoneDatabase) 
iex(2)> datetime_with_tz.std_offset
3600

最新更新