From: ttilberg@... Date: 2020-02-17T21:46:19+00:00 Subject: [ruby-core:97184] [Ruby master Feature#16637] Time#to_s and Date#to_s accept strftime format string Issue #16637 has been updated by ttilberg (Tim Tilberg). shevegen (Robert A. Heiler) wrote in #note-2: > I am not sure if this is a good suggestion though, largely because .to_s already having a > distinct meaning, e. g. "to string" (or to a string representation). > > People also typically associate .to_s, if there is an argument, with something like this: > > 37.to_s(2).rjust(8, "0") # => "00100101" > I feel that this directly supports the proposal in that when you call `#to_s` on a `Time` or `Date` object, you are casting this object to a string representation, which could be formatted whichever way. The fact that the current `#to_s` already includes a default `strftime` format further suggests that this feels like the right place. Finally, I feel that casting an integer to string, and being able to specify that it should be represented as binary further supports this cause. I should note, I have no desire to introduce something that doesn't slot in naturally, but I think since this method previously did not take any args, it _should_ be able to change without breaking anything, right? If there are edge cases where this may not be true, than I do not want to support `#to_s(format)` and instead would prefer an alternate name. However `#format` is already taken and marked private. sawa (Tsuyoshi Sawada) wrote in #note-3: > at least the time-part in strftime and strptime is redundant. That is for sure. Perhaps aliases like Time#strf, Time.strp may make sense. Oh my. Yes, this seems even more clear that the notion of using `strftime` (and `strf`) is unnatural (with exception to the fact that languages have used these terms forever. However, this is Ruby! We are people, not machines!). I find `#strf` equally unintuitive. Personal anecdote: Whenever I see `strftime` and I always think "string from time" first, and `strptime` as "string print time". Thank you for your comments so far, folks. ---------------------------------------- Feature #16637: Time#to_s and Date#to_s accept strftime format string https://bugs.ruby-lang.org/issues/16637#change-84291 * 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: