RubyとRailsには日時・日付の表記方法がいくつも存在する。
英語的に違和感がないものを選ぶのがベストではありますが、正直、ネイティブじゃないと分からないものも多い。
という事で、一般的かつ可読性が高い表記に統一できるようにまとめてみました。

まずはタイムゾーンに対応した表記に

0時〜9時前までに実行するとバグる(テストが落ちる)原因と対策

# \.to_datetime
.to_datetime → ※あえて、+0000にしたい場合は除く
 (日時型と比較する場合).in_time_zone
 (日付型と比較する場合).to_date
# \.to_time
.to_time → .in_time_zone

# Date\.(today|current)
Date.today, Date.current, Time.current.to_date → Time.zone.today
# Date\.(yesterday|tomorrow)
Date.yesterday → Time.zone.yesterday
Date.tomorrow → Time.zone.tomorrow

# Time\.(now|new)
Time.now, Time.new(パラメータなし) → Time.current
# Time\.new\(
Time.new(パラメータあり) → Time.zone.local

日時・日付表記を統一

# (day等) = (second|minute|hour|day|week|month|year)(s)
# [+-]\s*\d*\.(second|minute|hour|day|week|month|year)(|s)

# Time.current\s*-
Time.current - n.(day等) → n.(day等).ago
# Time.current\s*\+
Time.current + n.(day等) → n.(day等).from_now
# Time.zone.today\s*-
Time.zone.today - n.(day等) → n.(day等).ago.to_date
# Time.zone.today\s*\+
Time.zone.today + n.(day等) → n.(day等).from_now.to_date

# \.(before|until)
xxx.before, xxx.until → xxx.ago
# \.(after|since)
xxx.after, xxx.since → xxx.from_now

# \.ago\(
n.(day等).ago(xxx) → xxx - n.(day等)
# \.from_now\(
n.(day等).from_now(xxx) → xxx + n.(day等)

# \.next_day
xxx.next_day → xxx.tomorrow
# \.prev_day
xxx.prev_day → xxx.yesterday
# \.days_ago\(
xxx.days_ago(n) → xxx - n.day(s)
# \.days_since\(
xxx.days_since(n) → xxx + n.day(s)

xxx.next_week
# \.last_week
xxx.last_week → xxx.prev_week
xxx.beginning_of_week
xxx.end_of_week

xxx.next_month
# \.last_month
xxx.last_month → xxx.prev_month
# \.months_ago\(
xxx.months_ago(n) → xxx - n.month(s)
# \.months_since\(
xxx.months_since(n) → xxx + n.month(s)
xxx.beginning_of_month
xxx.end_of_month

xxx.beginning_of_quarter
xxx.end_of_quarter

xxx.next_year
xxx.last_year → xxx.prev_year
# \.years_ago\(
xxx.years_ago(n) → xxx - n.year(s)
# \.years_since\(
xxx.years_since(n) → xxx + n.year(s)
xxx.beginning_of_year
xxx.end_of_year

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です