junohm410 / fjord-flea-market

A closed flea market app for FjordBootCamp members to easily buy and sell items within the community, featuring automated buyer selection and Discord notifications.
0 stars 0 forks source link

タイムゾーンの設定を行う #62

Closed junohm410 closed 2 months ago

junohm410 commented 2 months ago

Railsにおける日時の扱い方の確認

// デフォルトではタイムゾーンは UTC
irb(main):001> Time.zone
=> #<ActiveSupport::TimeZone:0x00000001224527c0 @name="UTC", @tzinfo=#<TZInfo::DataTimezone: Etc/UTC>, @utc_offset=nil>

// UTCの時間が表示される
irb(main):002> Time.zone.now
=> Sat, 06 Jul 2024 12:37:55.787084000 UTC +00:00

// クラスの確認
irb(main):003> Time.zone.now.class
=> ActiveSupport::TimeWithZone

// 商品につけたdeadlineのカラムの中身のクラスも ActiveSupport::TimeZone
irb(main):004> Item.last.deadline.class
  Item Load (1.0ms)  SELECT "items".* FROM "items" ORDER BY "items"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> ActiveSupport::TimeWithZone

irb(main):005> Item.last.deadline
  Item Load (0.6ms)  SELECT "items".* FROM "items" ORDER BY "items"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> Thu, 11 Jul 2024 20:18:00.000000000 UTC +00:00
junohm410 commented 2 months ago

タイムゾーンをJSTに設定できたのclose。

irb(main):001> item = Item.find(6)
  Item Load (0.8ms)  SELECT "items".* FROM "items" WHERE "items"."id" = $1 LIMIT $2  [["id", 6], ["LIMIT", 1]]
=> 
#<Item:0x000000012b3de8a8
...
irb(main):002> item.created_at
=> Thu, 04 Jul 2024 23:53:23.560299000 JST +09:00
irb(main):003> item.updated_at
=> Sat, 06 Jul 2024 21:50:50.424930000 JST +09:00
irb(main):004> item.deadline
=> Thu, 11 Jul 2024 12:56:00.000000000 JST +09:00

Image