LoginSignup
13
12

More than 5 years have passed since last update.

Elixir 1.3から入る時間に関するsigil

Posted at

ブログから転載


Elixir 1.3(正確には1.3.0-rc.0)から時間に関する構造体がいくつか定義されました。そのうちの3つ、Date, Time, NaiveDateTimeをつくるsigilが追加されています。それぞれ~D, ~T, ~Nです。

iexで~D(2016-06-04)と打っても、そのまま表示されてしまい何が作られているのかよくわからないので、|> inspect(structs: false)としています

iex> ~D(2016-06-04) |> inspect(structs: false)
"%{__struct__: Date, calendar: Calendar.ISO, day: 4, month: 6, year: 2016}"

iex> ~T(22:05:33) |> inspect(structs: false)
"%{__struct__: Time, hour: 22, microsecond: {0, 0}, minute: 5, second: 33}"

iex> ~N(2016-06-04 22:05:33) |> inspect(structs: false)
"%{__struct__: NaiveDateTime, calendar: Calendar.ISO, day: 4, hour: 22, microsecond: {0, 0}, minute: 5, month: 6, second: 33, year: 2016}"

6月や5分などの1桁の数字は、06のようにゼロ埋めしていないとエラーになります。

13
12
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
12