2017-02-08 3 views
1

Ecto.DateTimeに1つの日付時刻があり、DateTimeに2つ目の日付時刻があります。どのように私はそれらをお互いに変換することができますか?簡単な方法はありません外部の依存関係なし?ドキュメントには何もありません。そのうちの1つにto_erl、別のfrom_unixがありますが、to_unix/from_unixやto_erl/from_erlなどのメソッドに重複はありません。Ecto.DateTimeとDateTimeの変換

答えて

8

Ecto.DateTimeは、DateTimeのいずれもタイムゾーンを保存していないので、NaiveDateTimeに相当します。 Erlang datetimesにはタイムゾーンがありません。そのため、DateTimeにはto_erlfrom_erlが存在しません。

あなたが最初NaiveDateTimeに変換してから日時をしている時間帯と一緒にDateTime.from_naive/2を使用することができます(エリクサーはエリクサー1.4のようEtc/UTCをサポートしています):

iex(1)> Ecto.DateTime.utc |> Ecto.DateTime.to_erl |> NaiveDateTime.from_erl! |> DateTime.from_naive!("Etc/UTC") 
%DateTime{calendar: Calendar.ISO, day: 8, hour: 4, microsecond: {0, 0}, 
minute: 49, month: 2, second: 9, std_offset: 0, time_zone: "Etc/UTC", 
utc_offset: 0, year: 2017, zone_abbr: "UTC"} 
iex(2)> DateTime.utc_now |> DateTime.to_naive |> NaiveDateTime.to_erl |> Ecto.DateTime.from_erl 
#Ecto.DateTime<2017-02-08 04:50:23> 

以前しかしEcto.DateTimeを使用していた場合、あなたはおそらく今すぐNaiveDateTimeを使いたい