[#97086] [Ruby master Bug#16612] Enumerator::ArithmeticSequence#last with float range produces incorrect value — muraken@...
Issue #16612 has been updated by mrkn (Kenta Murata).
4 messages
2020/02/07
[#97307] [Ruby master Feature#16663] Add block or filtered forms of Kernel#caller to allow early bail-out — headius@...
Issue #16663 has been reported by headius (Charles Nutter).
29 messages
2020/02/28
[ruby-core:97180] [Ruby master Feature#16637] Time#to_s and Date#to_s accept strftime format string
From:
ttilberg@...
Date:
2020-02-17 16:43:23 UTC
List:
ruby-core #97180
Issue #16637 has been updated by ttilberg (Tim Tilberg).
[Gerald Bauer pointed out](https://www.reddit.com/r/ruby/comments/f4pcn5/dateformatter_gem_date_formatter_by_example/fhxkn8f/) that this concept already exists in Rails' ActiveSupport: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/time/conversions.rb
Is this a case where we might consider integrating this idea from Rails? I feel like it's very much in the spirit of core Ruby, with attention to developer happiness. It always surprises me how much I have to look up when I'm printing date/time as strings.
----------------------------------------
Feature #16637: Time#to_s and Date#to_s accept strftime format string
https://bugs.ruby-lang.org/issues/16637#change-84288
* Author: ttilberg (Tim Tilberg)
* Status: Open
* Priority: Normal
----------------------------------------
While terms like `strftime` and `strptime` are ubiqutous through the history of computer science, I feel that the terms are very dense. If you are not already in-the-know, they are gibberish. If you are in the know, they are still a bit clunky. While discussing ways to improve the Time and Date formatting APIs for humanity, I thought a quick and easy improvement would be removing the need to use the method `#strftime`. `#format` is already reserved as a private method, but how do people feel about allowing a format string as an argument for `#to_s`?
I'm not comfortable writing C, but the relevant code is [here](https://github.com/ruby/ruby/blob/dcb05179a969a024bbd3b7622f67468ddf07638c/time.c#L4097)
It seems like it would be straightforward to use the current strings as default values, but to allow for a format string to be passed in.
```
time_to_s(VALUE time) // add format arg
{
struct time_object *tobj;
GetTimeval(time, tobj);
if (TZMODE_UTC_P(tobj))
return strftimev("%Y-%m-%d %H:%M:%S UTC", time, rb_usascii_encoding()); // format || "%Y-%m-%d %H:%M:%S UTC"
else
return strftimev("%Y-%m-%d %H:%M:%S %z", time, rb_usascii_encoding()); // format || "%Y-%m-%d %H:%M:%S %z"
}
```
This would allow an API that feels a bit more intuitive. You still have to know the formatting symbols, but it creates a much more expressive statement:
```
# The current time, to string. What kind of string? A Y-m-d string.
Time.now.to_s('%Y-%m-%d')
```
(As an aside for discussion, I feel this way about formatting things like Floats and other numbers also. That API is equally confusing, and a holdover from history in comp-sci.)
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>