Project

General

Profile

Actions

Feature #21533

open

Introduce `Time#am?` and `Time#pm?`

Added by matheusrich (Matheus Richard) 2 days ago. Updated about 2 hours ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:122925]

Description

This proposal adds two predicate methods to Time:

Time.utc(2000, 1, 1, 11, 59, 59).am? # => true
Time.utc(2000, 1, 1, 12, 0, 0).pm?   # => true
  • am? returns true when the hour is less than 12.
  • pm? returns true when the hour is 12 or greater.

These methods provide a clear and expressive way to branch logic based on time of day. For example:

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

This is a common pattern in applications involving reminders and scheduling. The
method names are intuitive, and the semantics are well understood.

The implementation is in this Pull Request.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0