From: duerst Date: 2022-10-21T08:39:29+00:00 Subject: [ruby-core:110458] [Ruby master Feature#19071] Add Time#deconstruct, #deconstruct_keys, and #to_h Issue #19071 has been updated by duerst (Martin D��rst). sawa (Tsuyoshi Sawada) wrote in #note-3: > I have another reason against `deconstruct`. The sequence `[year, month, mday, hour, min, sec, subsec]` is culturally biased and is arbitrarily selected. There's a bit of cultural bias, but it's not an arbitrary selection, it's very clearly ordered from larger units to smaller, and this is a very widely used convention not only for time, but also for other units including currencies. Also, it's the order used in ISO 8601, the international standard for date and time formats. So very far from arbitrary. > For example, following the fact that month is a highly irregular concept, some people avoid using it, and express the day with a combination of the week number and the day of week. Or, since the week number can be large, some people use the combination of the week-based quarter year, the week number within the quarter, and the day of the week. There may be such people (I know that week number/weekday is very popular in Germany for business meetings), but overall this is a very small minority, and virtually everybody is familiar with (western) months. ---------------------------------------- Feature #19071: Add Time#deconstruct, #deconstruct_keys, and #to_h https://bugs.ruby-lang.org/issues/19071#change-99776 * Author: zverok (Victor Shepelev) * Status: Open * Priority: Normal ---------------------------------------- I believe that `Time` being suitable for pattern-matching is a reasonable feature with many possible usages, which will increase usability of `Time` and would be a good show case for pattern matching. **Implementation decisions** `Time#deconstruct`: * returns time components in order `[year, month, mday, hour, min, sec, subsec]` * I believe the highest-to-lowest order is the only reasonable/guessable, and there is no point to put into the array _all_ of the time information available (e.g. zone, wday, yday) * I am not sure (and open to discussion) about `subsec`. It seems to me the most basic sub-second unit of Time, but I might be wrong; also, it might be not that useful for array deconstruction. Possible usage: ```ruby case tm in [...2022, *] puts "previous year" in [2022, 1..6, *] puts "Q1-2" in [2022, 7..9, *] puts "Q3" in [2022, month, day, *] puts "Current quarter, #{day}/#{month}" end ``` `Time#deconstruct_keys`: * chosen keys: `[:year, :month, :day, :yday, :wday, :hour, :min, :sec, :subsec, :dst, :zone]` * I am open to discussing whether we should include other subsecond units (or any whatsoever) * It might be useful (but too loose interface) to support `mon` as a synonym for `month`?.. But might be confusing if somebody will unpack the `**rest` * `day`, not `mday`, seems most reasonable Possible usages: ```ruby case t in year: ...2022 puts "too old" in month: ..9 puts "quarter 1-3" in wday: 1..5, month: puts "working day in month #{month}" end if t in Time(wday: 3, day: ..7) puts "first Wednesday of the month" end ``` `Time#to_h`: * added on a "why not" basis :) As we already have "convert to hash" in the form of `deconstruct_keys(nil)`, having a canonic form seems harmles. Open for discussion. * keys are the same as for `deconstruct_keys` Pull request: https://github.com/ruby/ruby/pull/6594 -- https://bugs.ruby-lang.org/ Unsubscribe: