ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "zverok (Victor Shepelev)" <noreply@ruby-lang.org>
To: ruby-core@neon.ruby-lang.org
Subject: [ruby-core:110459] [Ruby master Feature#19071] Add Time#deconstruct, #deconstruct_keys, and #to_h
Date: Fri, 21 Oct 2022 09:16:39 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-99777.20221021091639.710@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-19071.20221019192104.710@ruby-lang.org

Issue #19071 has been updated by zverok (Victor Shepelev).


@matz Understood, thanks, I'll adjust the PR.

@sawa, for the sake of the argument
> The sequence `[year, month, mday, hour, min, sec, subsec]` is culturally biased and is arbitrarily selected.

I don't think this statement is fair. This sequence of units is not natural for some particular culture. E.g. 18:10 at Oct 21 would be written "10/21/22 6:10pm" in US, and "18:10 21.10.2022" in Ukraine, but `"2022-10-21 18:10:00"` is rather a common engineering implementation, logically structured for unambiguous parsing and sorting. 

I understand there are different cultures out there (and for some, it is not "Oct 21, 2022" at all, but maybe "26 Tishrei, 5783" or "25 Rabi ul Awal 1444", or something else), but I believe it can be said with a high confidence that 
1. when seeing a sequence of numbers starting with `2022,` or `year,`, most of **developers** can guess the forthcoming units.
2. when trying to design or guess sequence of units of Time/Date representation _today_ (with widespread standardization, JSON serializations etc.), the best guess would be close to ISO8601 Date & Time format.

----------------------------------------
Feature #19071: Add Time#deconstruct, #deconstruct_keys, and #to_h
https://bugs.ruby-lang.org/issues/19071#change-99777

* 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/

  parent reply	other threads:[~2022-10-21  9:16 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-19 19:21 [ruby-core:110422] [Ruby master Feature#19071] Add Time#deconstruct, #deconstruct_keys, and #to_h zverok (Victor Shepelev)
2022-10-19 19:47 ` [ruby-core:110423] " bdewater (Bart de Water)
2022-10-20 22:42 ` [ruby-core:110453] " matz (Yukihiro Matsumoto)
2022-10-21  5:44 ` [ruby-core:110457] " sawa (Tsuyoshi Sawada)
2022-10-21  8:39 ` [ruby-core:110458] " duerst
2022-10-21  9:16 ` zverok (Victor Shepelev) [this message]
2022-10-21 18:44 ` [ruby-core:110469] " sawa (Tsuyoshi Sawada)
2022-10-22 13:08 ` [ruby-core:110477] " zverok (Victor Shepelev)
2022-11-22 21:11 ` [ruby-core:110860] " zverok (Victor Shepelev)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-99777.20221021091639.710@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    --cc=ruby-core@neon.ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).