4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

日本人だから入力値は日本時間で表現したい!

Posted at

世の中全て日本時間になれ!って一度はそう思ったことありますよね?
そういっても、サーバ上はUTCとかはよく有る話です。

テストするときに、日本時間感覚で検証したい。
そんなわけでみんな大好きTimex

#準備

まずはiexでTimexを使う準備

iex(1)> Mix.install([{:timex, "~> 3.7"}])  
Resolving Hex dependencies...
Dependency resolution completed:
New:
  certifi 2.8.0
  combine 0.10.0
  gettext 0.19.0
〜 省略 〜
==> timex
Compiling 62 files (.ex)
Generated timex app
:ok

#日本時間→UTC

日本時間 09:00を変換してみましょう

iex(2)> ~N[2022-01-01 09:00:00] |> Timex.to_datetime("Asia/Tokyo") |> Timex.to_naive_datetime()
~N[2022-01-01 00:00:00]

UTC時間の00:00になりました

#UTC→日本時間

UTC 00:00を変換してみましょう

iex(3)> ~N[2022-01-01 00:00:00] |>  Timex.to_datetime("Etc/UTC") |> Timex.to_datetime("Asia/Tokyo") 
#DateTime<2022-01-01 09:00:00+09:00 JST Asia/Tokyo>

日本時間09:00になりました

4
0
0

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
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?