2
1

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.

備忘: Railsで古い日付を JST の TimeWithZoneに変換すると時刻がずれる

Last updated at Posted at 2024-04-09

追記

image.png

コメントで教えていただきました!


以下、元記事

:pencil: ふかぼりきれておらず事象だけのメモ

環境

ruby: ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin22]
rails: 7.1.2

現象

image.png

 TZ=UTC rails c
Loading development environment (Rails 7.1.2)
irb(main):001> Rails.application.config.time_zone
=> "UTC"
irb(main):002> Time.now.zone
=> "UTC"
irb(main):003> Time.zone.parse("1800/01/01 00:00:00").iso8601
=> "1800-01-01T00:00:00Z"
irb(main):004> Time.zone.parse("1800/01/01 00:00:00").utc.iso8601
=> "1800-01-01T00:00:00Z"
irb(main):005> Time.zone.parse("1800/01/01 00:00:00").in_time_zone("UTC").iso8601
=> "1800-01-01T00:00:00Z"
irb(main):006> Time.zone.parse("1800/01/01 00:00:00").in_time_zone("Tokyo").iso8601
=> "1800-01-01T09:18:59+09:18"
irb(main):007> Time.parse("1800/01/01 00:00:00").iso8601
=> "1800-01-01T00:00:00+00:00"
irb(main):008> Time.parse("1800/01/01 00:00:00").utc.iso8601
=> "1800-01-01T00:00:00Z"
irb(main):009> Time.parse("1800/01/01 00:00:00").in_time_zone("UTC").iso8601
=> "1800-01-01T00:00:00Z"
irb(main):010> Time.parse("1800/01/01 00:00:00").in_time_zone("Tokyo").iso8601
=> "1800-01-01T09:18:59+09:18"

image.png

 rails c
Loading development environment (Rails 7.1.2)
irb(main):001> Rails.application.config.time_zone
=> "Tokyo"
irb(main):002> Time.now.zone
=> "JST"
irb(main):003> Time.zone.parse("1800/01/01 00:00:00").iso8601
=> "1800-01-01T00:00:00+09:18"
irb(main):004> Time.zone.parse("1800/01/01 00:00:00").utc.iso8601
=> "1799-12-31T14:41:01Z"
irb(main):005> Time.zone.parse("1800/01/01 00:00:00").in_time_zone("UTC").iso8601
=> "1799-12-31T14:41:01Z"
irb(main):006> Time.zone.parse("1800/01/01 00:00:00").in_time_zone("Tokyo").iso8601
=> "1800-01-01T00:00:00+09:18"
irb(main):007> Time.parse("1800/01/01 00:00:00").iso8601
=> "1800-01-01T00:00:00+09:00"
irb(main):008> Time.parse("1800/01/01 00:00:00").utc.iso8601
=> "1799-12-31T15:00:00Z"
irb(main):009> Time.parse("1800/01/01 00:00:00").in_time_zone("UTC").iso8601
=> "1799-12-31T15:00:00Z"
irb(main):010> Time.parse("1800/01/01 00:00:00").in_time_zone("Tokyo").iso8601
=> "1800-01-01T00:18:59+09:18"
2
1
2

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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?