The new calendar types in Elixir 1.3

6e4dd01aa8fd363cbfe69538eeb030df?s=47 lau
September 02, 2016

The new calendar types in Elixir 1.3

Elixir 1.3 adds new built-in types for date and time.

Hear about the considerations behind them and how they can improve the quality of your data and software.

When should you use a NaiveDateTime instead of a DateTime? Find out the what best practices are for all the new types. Be it with Ecto, with Phoenix or in Elixir in general.

6e4dd01aa8fd363cbfe69538eeb030df?s=128

lau

September 02, 2016
Tweet

Transcript

  1. Calendar types in Elixir 1.3 ElixirConf 2016

  2. Lau Taarnskov Software developer Open source contributor. Creator of Calendar

    for Elixir and tzdata Blog: creativedeletion.com Twitter: @laut Github: lau
  3. Lau Taarnskov - twitter: @LauT What is a type?

  4. Lau Taarnskov - twitter: @LauT Why have types?

  5. Lau Taarnskov - twitter: @LauT iex(1)> 5/2 2.5

  6. Lau Taarnskov - twitter: @LauT iex(1)> 5/2 2.5 iex(2)> 5/"Orlando"

    ** (ArithmeticError) bad argument in arithmetic expression :erlang./(5, "Orlando")
  7. Lau Taarnskov - twitter: @LauT We want errors instead of

    non-sensical results
  8. Lau Taarnskov - twitter: @LauT Explicit data

  9. Lau Taarnskov - twitter: @LauT

  10. Lau Taarnskov - twitter: @LauT Mars Climate Orbiter Mission cost:

    $327.6 million Mission failure after a 286 day journey to Mars
  11. Lau Taarnskov - twitter: @LauT Newton-seconds? Oh, we used pound

    seconds.
  12. Lau Taarnskov - twitter: @LauT 5, 6, 3, 9, 4,

    5…
  13. Lau Taarnskov - twitter: @LauT {5, :lbs_s}, {6, :lbs_s}, {3,

    :lbs_s}, {9, :lbs_s}, {4, :lbs_s}, {5, :lbs_s}…
  14. Lau Taarnskov - twitter: @LauT %ThrusterData{amount: 5, unit: :lbs_s}, %ThrusterData{amount:

    6, unit: :lbs_s}, %ThrusterData{amount: 3, unit: :lbs_s},
  15. Lau Taarnskov - twitter: @LauT def process_thruster_data(%{amount: amount, unit: :n_s})

    do # ... end ** (FunctionClauseError) no function clause matching in ThrustProcessor.process_thruster_data/1 thruster.ex:12: ThrustProcessor.process_thruster_data(%ThrusterData{amou nt: 3, unit: :lbs_s}) thruster.ex:17: (file) (elixir) lib/code.ex:363: Code.require_file/2
  16. Lau Taarnskov - twitter: @LauT Different units of time and

    date
  17. Lau Taarnskov - twitter: @LauT 2016 2016 September 2016 September

    2nd 2016 September 2nd at 14:01:03 2016-09-02 at 14:01:03 in New York 14:01 14:01:03 14:01:03.9472 ….
  18. Lau Taarnskov - twitter: @LauT One type to rule them

    all?
  19. Lau Taarnskov - twitter: @LauT 2016-09-02 2016-09-02 00:00:00.000 UTC

  20. Lau Taarnskov - twitter: @LauT 06:10 2001-01-01 06:10:00.000 UTC

  21. Lau Taarnskov - twitter: @LauT 2001-01-01 00:00:00.000 2001-01-01 06:10:00.000 UTC

  22. Lau Taarnskov - twitter: @LauT 6:10 What year is that

    in? 2001 (!!!!!!!)
  23. Lau Taarnskov - twitter: @LauT 2016-09-02 At what hour and

    minute? 00:00 (!!!)
  24. Lau Taarnskov - twitter: @LauT Year Year with month Year,

    month, day of month Hour Hour and minute Hour and minute and second …
  25. Lau Taarnskov - twitter: @LauT The structs in the Calendar

    library
  26. Lau Taarnskov - twitter: @LauT Types in Calendar in 2014

    Calendar.Date Calendar.Time Calendar.NaiveDateTime Calendar.DateTime
  27. Lau Taarnskov - twitter: @LauT Date: year, month, day

  28. Lau Taarnskov - twitter: @LauT Time: hour, minute, second, microsecond

  29. Lau Taarnskov - twitter: @LauT NaiveDateTime: year, month, day, hour,

    minute, second, microsecond
  30. Lau Taarnskov - twitter: @LauT DateTime: year, month, day, hour,

    minute, second, microsecond, time zone, UTC offset, standard offset, zone abbreviation
  31. Lau Taarnskov - twitter: @LauT Local vs Naive

  32. Lau Taarnskov - twitter: @LauT JAVA 8 Class LocalDate A

    date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.
  33. Lau Taarnskov - twitter: @LauT local |ˈlōk(ə)l| adjective belonging or

    relating to a particular area or neighborhood, typically exclusively so: researching local history | the local post office.
  34. Lau Taarnskov - twitter: @LauT

  35. Lau Taarnskov - twitter: @LauT ~N[1970-01-01 00:00:00] |> DateTime.to_unix **

    (FunctionClauseError) no function clause matching in DateTime.to_unix/2 (elixir) lib/calendar.ex:1078: DateTime.to_unix(~N[1970-01-01 00:00:00], :seconds)
  36. Lau Taarnskov - twitter: @LauT The structs built into Elixir

    1.3
  37. Lau Taarnskov - twitter: @LauT Types in Calendar in 2014

    Types in Elixir 1.3 in 2016 Calendar.Date Date Calendar.Time Time Calendar.NaiveDateTime NaiveDateTime Calendar.DateTime DateTime
  38. Lau Taarnskov - twitter: @LauT Sigils ~D[2016-09-02] ~T[14:15:00] ~N[2016-09-02 14:15:00]

  39. Lau Taarnskov - twitter: @LauT %DateTime{calendar: Calendar.ISO, day: 1, hour:

    0, microsecond: {0, 0}, …
  40. Lau Taarnskov - twitter: @LauT %DateTime{calendar: Calendar.ISO, day: 1, hour:

    0, microsecond: {0, 0}, …
  41. Lau Taarnskov - twitter: @LauT "2016-09-02 14:15:00.1234” |> NaiveDateTime.from_iso8601! ~N[2016-09-02

    14:15:00.1234] "2016-09-02 14:15:00.1” |> NaiveDateTime.from_iso8601! ~N[2016-09-02 14:15:00.1] Significant fractional seconds
  42. Lau Taarnskov - twitter: @LauT "2016-09-02 14:15:00.1234” |> NaiveDateTime.from_iso8601! |>

    Map.get(:microsecond) {123400, 4} "2016-09-02 14:15:00.10” |> NaiveDateTime.from_iso8601! |> Map.get(:microsecond) {100000, 2} "2016-09-02 14:15:00” |> NaiveDateTime.from_iso8601! |> Map.get(:microsecond) {0, 0} Significant fractional seconds
  43. Lau Taarnskov - twitter: @LauT Time zones and more functionality

    with the Calendar library
  44. Lau Taarnskov - twitter: @LauT The Calendar library

  45. Lau Taarnskov - twitter: @LauT ~N[1970-01-01 00:00:00] |> Calendar.NaiveDateTime.to_date_time_utc |>

    DateTime.to_unix 0
  46. Lau Taarnskov - twitter: @LauT DateTime.from_unix!(1472836167) %DateTime{calendar: Calendar.ISO, day: 2,

    hour: 17, microsecond: {0, 0}, minute: 9, month: 9, second: 27, std_offset: 0, time_zone: "Etc/UTC", utc_offset: 0, year: 2016, zone_abbr: "UTC"}
  47. Lau Taarnskov - twitter: @LauT DateTime.from_unix!(1472836167) |> Calendar.DateTime.shift_zone!("America/New_York") %DateTime{calendar: Calendar.ISO,

    day: 2, hour: 13, microsecond: {0, 0}, minute: 9, month: 9, second: 27, std_offset: 3600, time_zone: "America/New_York", utc_offset: -18000, year: 2016, zone_abbr: "EDT"}
  48. Lau Taarnskov - twitter: @LauT DateTime.from_unix!(1472836167) |> Calendar.DateTime.shift_zone(“America/New_York”) |> DateTime.to_iso8601

    "2016-09-02T13:09:27-04:00"
  49. Lau Taarnskov - twitter: @LauT DateTime.from_unix(1472836167) |> Calendar.DateTime.shift_zone!(“America/New_York”) |> Calendar.Strftime.strftime!("%Y

    %B %d, %H:%M:%S %Z") "2016 September 02, 13:09:27 EDT"
  50. Lau Taarnskov - twitter: @LauT ~D[2016-09-02] |> Calendar.Date.friday? true

  51. Lau Taarnskov - twitter: @LauT ~D[2016-09-02] |> Calendar.Date.before? (~D[2016-09-03]) true

  52. Lau Taarnskov - twitter: @LauT ~D[2016-09-02] |>Calendar.Date.next_day! ~D[2016-09-03]

  53. Lau Taarnskov - twitter: @LauT Calendar.Date.today(“Amer ica/New_York") |> Calendar.Date.next_day! ~D[2016-09-03]

  54. Lau Taarnskov - twitter: @LauT ~D[2016-09-02] |> Calendar.Date.add!(7) |> Date.to_iso8601

    "2016-09-09"
  55. Lau Taarnskov - twitter: @LauT Getting the time zone of

    a user
  56. Lau Taarnskov - twitter: @LauT Persistence in Ecto

  57. Lau Taarnskov - twitter: @LauT The Calecto library

  58. Lau Taarnskov - twitter: @LauT You want to select a

    type that fits the data
  59. Lau Taarnskov - twitter: @LauT Thank you

  60. Lau Taarnskov - twitter: @LauT Questions