🚀

[Feature #21533] AM / PM を判定する Time#am?, pm? メソッドを追加する提案

に公開

[Feature #21533] Introduce Time#am? and Time#pm?

  • AM / PM を判定する Time#am?, pm? メソッドを追加する提案
    • #am? は時が 12 未満の場合に true を返す
    • #pm? は時が 12 以上の場合に true を返す
Time.utc(2000, 1, 1, 11, 59, 59).am? # => true
Time.utc(2000, 1, 1, 12, 0, 0).pm?   # => true
  • ユースケースとしては以下のようなコードが提示されている
    • AM かどうかで文言を追加する
def reminder_deferral_options
  options = []
  options << ["Later today", "later_today"] if Time.now.am?
  options << ["Tomorrow morning", "tomorrow_morning"]
  options << ["Pick a date/time…", "custom"]
  options
end
noon = Time.new(2025, 1, 1, 12, 0, 0)
midnight = Time.new(2025, 1, 1, 0, 0, 0)

puts noon.strftime("%I:%M %p")     # => 12:00 PM
puts midnight.strftime("%I:%M %p") # => 12:00 AM
  • 別案として 正午(12時) / 深夜(0時) のときに am? / pm? 両方とも false を返す、みたいな話もでていますね
  • これなんですが最終的には 0時 と 12時 が AM なのか PM なのか合意が得られなかったみたいですね
  • また U.S. Government Publishing Office では最近(2008年)に定義を変えたみたいでまた不安定、という理由も提示されている
  • パッと見、あると便利そうと思ったりしたんですが時間関連はやっぱりエッジケースが難しい…
GitHubで編集を提案

Discussion