LoginSignup
2
1

More than 1 year has passed since last update.

Time.zone.todayってTimeクラスに話しかけたのになぜDateクラスが返ってくるのか?

Last updated at Posted at 2021-08-26

たまげたこと

こんなRailsのコードがあります。

Time.zone.today

Timeにtodayなんて名前のメソッドを使えるからなんか違和感を感じておりまして。
興味本位で、このインスタンスに「君のクラスは?」と尋ねてみたら、Dateクラスのインスタンスが返ってきてたまげました

time_current_time_zone_today.png

僕はTimeクラスさんに話しかけたはずなのに。。なぜなんだ!?
いつの間にDateクラスさんにすり替わってるのだ!?

順を追って整理したいと思います。

環境

ruby 3.0.0
Rails 6.0.3

1、Time#zoneとは?

Time#zoneはTimeZoneクラスのインスタンスを生成するそうな。

zone()
Returns the TimeZone for the current request, if this has been set (via Time.zone=). If Time.zone has not been set for the current request, returns the TimeZone specified in config.time_zone.
https://api.rubyonrails.org/classes/Time.html#method-c-zone

抜粋したドキュメントによれば、Railsだとここを見に行くそう↓。

config/application.rb
module AppName
  class Application < Rails::Application
    # 中略
    config.time_zone = 'Tokyo'

確かに「いかにも、わしはTimeZoneクラスじゃが?」と返事が返ってきた。

[2] pry(main)> y = Time.zone
=> #<ActiveSupport::TimeZone:0x00007ff4a889c548 @name="Tokyo", @tzinfo=#<TZInfo::DataTimezone: Asia/Tokyo>, @utc_offset=nil>
[3] pry(main)> y.class
=> ActiveSupport::TimeZone

これで生成されるTimeZoneクラス
https://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html

2、TimeZone#todayとは?

TimeZoneクラスはインスタンスメソッドtodayを持っている。
ソースコードを覗いてみたら...↓
https://github.com/rails/rails/blob/83217025a171593547d1268651b446d3533e2019/activesupport/lib/active_support/values/time_zone.rb#L491

抜粋↓

time_zone.rb
    # Returns the current date in this time zone.
    def today
      tzinfo.now.to_date
    end

to_dateしている!
Time.zone.todayの返り値を最終的にDateクラスにしていたのは君だったのか!

納得。

ちなみに、to_dateメソッドは↓
https://docs.ruby-lang.org/ja/latest/method/Date/i/to_date.html

最後に

ちっちゃいけど、なぜか地味にこういうのがおもしろい。

これを書き終えて、「あれ、ていうか今日の情報が欲しいなら、シンプルにDate.todayでもいいんじゃ...」との疑問が湧いてきました。
@jnchito さんの記事で後で整理しよう...。
https://qiita.com/jnchito/items/cae89ee43c30f5d6fa2c#time%E3%82%AF%E3%83%A9%E3%82%B9

そして、実はこの記事は今下書き中のもっとエモい記事の副産物記事であります。

追記:書きました!↓

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