[ruby-core:122925] [Ruby Feature#21533] Introduce `Time#am?` and `Time#pm?`

Issue #21533 has been reported by matheusrich (Matheus Richard). ---------------------------------------- Feature #21533: Introduce `Time#am?` and `Time#pm?` https://bugs.ruby-lang.org/issues/21533 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- This proposal adds two predicate methods to `Time`: ```ruby 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: ```ruby 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](https://github.com/ruby/ruby/pull/14133). -- https://bugs.ruby-lang.org/

Issue #21533 has been updated by nobu (Nobuyoshi Nakada). I'm not against the methods themselves, since there are similar methods such as `monday?`, `tuesday?`, and so on. (no `january?`, `february?`, etc though) But your example, selecting by `am?`, seems less convincing. What will you do if the criteria is changed, or more options are added? Add `morning?`, `daytime?`, and others? ```ruby def reminder_deferral_options now = Time.now options = [] options << ["Around noon", "around_noon"] if now.morning? options << ["Later today", "later_today"] if now.am? options << ["Tonight", "tonight"] if now.daytime? options << ["Tomorrow morning", "tomorrow_morning"] options << ["Pick a date/time…", "custom"] options end ``` ---------------------------------------- Feature #21533: Introduce `Time#am?` and `Time#pm?` https://bugs.ruby-lang.org/issues/21533#change-114235 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- This proposal adds two predicate methods to `Time`: ```ruby 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: ```ruby 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](https://github.com/ruby/ruby/pull/14133). -- https://bugs.ruby-lang.org/

Issue #21533 has been updated by matheusrich (Matheus Richard). @nobu totally fair questioning. In fact, my initial idea was to propose Time#morning? (hour between 6...12) but I wasn't sure that would be true for all cultures. `Time#am?/pm?` are enough for my use case, and I think they have value in themselves. ---------------------------------------- Feature #21533: Introduce `Time#am?` and `Time#pm?` https://bugs.ruby-lang.org/issues/21533#change-114236 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- This proposal adds two predicate methods to `Time`: ```ruby 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: ```ruby 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](https://github.com/ruby/ruby/pull/14133). -- https://bugs.ruby-lang.org/

Issue #21533 has been updated by nobu (Nobuyoshi Nakada). matheusrich (Matheus Richard) wrote in #note-2:
@nobu (Nobuyoshi Nakada) totally fair questioning. In fact, my initial idea was to propose Time#morning? (hour between 6...12) but I wasn't sure that would be true for all cultures.
It varies by cultures.
I'd be happy to introduce any other methods like daytime?/nighttime?, but I think those are prone to bikeshedding for what day/night/morning means.
Agree. We would need methods to calculate the sunrise/sunset times for a specified date and location.
Am/pm are clearly defined and might be enough most of the time?
Cleary defined -- probably yes. Enough most of the time -- depends on cultures probably. In any case, I'm not against (neutral or slightly positive) this proposal itself, as wrote previously. ---------------------------------------- Feature #21533: Introduce `Time#am?` and `Time#pm?` https://bugs.ruby-lang.org/issues/21533#change-114240 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- This proposal adds two predicate methods to `Time`: ```ruby 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: ```ruby 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](https://github.com/ruby/ruby/pull/14133). -- https://bugs.ruby-lang.org/

Issue #21533 has been updated by mame (Yusuke Endoh). I'm not strongly opposed, but I have a slight doubt about `noon.pm?` returning `true`. According to NIST, designating noon as either a.m. or p.m. is technically incorrect: https://www.nist.gov/pml/time-and-frequency-division/times-day-faqs
To illustrate this, consider that "a.m." and "p.m." are abbreviations for "ante meridiem" and "post meridiem," which mean "before noon" and "after noon," respectively. Since noon is neither before noon nor after noon, a designation of either a.m. or p.m. is incorrect. Also, midnight is both twelve hours before noon and twelve hours after noon.
A quick search for "12 p.m. noon" also brings up many articles and discussions arguing against the usage. I'm curious what others think. ---------------------------------------- Feature #21533: Introduce `Time#am?` and `Time#pm?` https://bugs.ruby-lang.org/issues/21533#change-114241 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- This proposal adds two predicate methods to `Time`: ```ruby 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: ```ruby 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](https://github.com/ruby/ruby/pull/14133). -- https://bugs.ruby-lang.org/

Issue #21533 has been updated by matheusrich (Matheus Richard). The current implementation is consistent with strftime: ```rb noon = Time.new(2025, 1, 1, 12, 0, 0) midnight = Time.new(2025, 1, 1, 0, 0, 0) puts noon.strftime("%I:%M %p").downcase # => 12:00 pm puts midnight.strftime("%I:%M %p").downcase # => 12:00 am ``` ---------------------------------------- Feature #21533: Introduce `Time#am?` and `Time#pm?` https://bugs.ruby-lang.org/issues/21533#change-114243 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- This proposal adds two predicate methods to `Time`: ```ruby 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: ```ruby 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](https://github.com/ruby/ruby/pull/14133). -- https://bugs.ruby-lang.org/

Issue #21533 has been updated by duerst (Martin Dürst). Using 12pm for noon is indeed bad practice. Using 12am for midnight is even worse practice, because without context it's unclear whether it's at the beginning or the end of a certain date. But it is practice, at least in certain parts of the world. See e.g. the Caltrain schedule at https://www.caltrain.com/media/34716. There's e.g. a northbound train that leaves South San Francisco (S. San Francisco) at 12:00pm on the first page. On page 2, there's also a train that leaves Menlo Park at 12:00am. In both cases, the context make it clear what is meant. I don't think introducing the two methods is a good idea. Formatting times should be done with an appropriately internationalized library. In other cases, what is in the morning or the afternoon may vary widely. The usage example given by the OP is spurious; the possibility to do something "Later today" may end at 11:00 or at 13:00 or at some other time somewhere around noon. ---------------------------------------- Feature #21533: Introduce `Time#am?` and `Time#pm?` https://bugs.ruby-lang.org/issues/21533#change-114246 * Author: matheusrich (Matheus Richard) * Status: Open ---------------------------------------- This proposal adds two predicate methods to `Time`: ```ruby 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: ```ruby 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](https://github.com/ruby/ruby/pull/14133). -- https://bugs.ruby-lang.org/
participants (4)
-
duerst
-
mame (Yusuke Endoh)
-
matheusrich (Matheus Richard)
-
nobu (Nobuyoshi Nakada)