Issue #10040 has been updated by Nathan Broadbent.


Nobuyoshi Nakada wrote:
> I don't think there is possibilities to introduce `%d` and `%i`, especially using variables.

I understand if the syntax doesn't feel quite right, I'm not sure about it either. But I think it should be quite a simple patch, since we would just need to replace any `%d` tokens with `Date.new`, before evaluating it.

> And yet another idea came to my mind:
> 
> ```ruby
> 20140715T125845     	   #=> Time.new(2014, 7, 15, 12, 58, 45)
> 20140715T125845Z     	   #=> Time.new(2014, 7, 15, 12, 58, 45, "+00:00")
> 990715T125845       	   #=> Time.new(99, 7, 15, 12, 58, 45); 1st century, not 19th century
> 20140715T125845.001.subsec #=> 1/100
> ```

That would be amazing, I really like it. Do you have a suggestion for a `Date` version, without the timestamp? How about: `20140715D`?

----------------------------------------
Feature #10040: `%d` and `%t` shorthands for `Date.new(*args)` and `Time.new(*args)`
https://bugs.ruby-lang.org/issues/10040#change-47801

* Author: Nathan Broadbent
* Status: Open
* Priority: Normal
* Assignee: 
* Category: 
* Target version: 
----------------------------------------
I'm working on a Ruby application where we have to deal with a lot of dates and times, especially in our test suite. We currently have a couple thousand cases of `Date.new(...)`, and I'm finding the syntax to be a little unwieldy.

What do you think about the following shorthand suggestions?

```ruby
%d(2014, 7, 15)                 # Equivalent to: Date.new(2014, 7, 15)
%t(2014, 7, 15, 12, 58, 45)     # Equivalent to: Time.new(2014, 7, 15, 12, 58, 45)
```
I added commas because I think the syntax should also support variables:

```ruby
year = 2014
month = 7
%d(year, month, 15)             # Equivalent to: Date.new(year, month, 15)
```
I understand that this will operate a bit differently to `%w` and `%i`, but I think that supporting variable evaluation would be a lot more useful.

An alternative proposal would be to add `[]` methods to `Date` and `Time`, so that we could call:

```ruby
Date[2014, 7, 15]
Time[2014, 7, 15, 12, 58, 43]
```
This would be still be a little shorter and nicer to use. Please let me know if you would like me to open a feature ticket for `Date.[]` and `Time.[]` methods, instead.

Please let me know your thoughts. Would you find a shorter date/time syntax useful?



-- 
https://bugs.ruby-lang.org/