Skip to content

Conversation

@pixeltrix
Copy link
Contributor

When assigning a hash to a time attribute that's missing a year component (e.g. a time_select with :ignore_date set to true) then the year defaults to 1970 instead of the expected 2000. This results in the attribute changing as a result of the save.

Before:

event = Event.new(start_time: { 4 => 20, 5 => 30 })
event.start_time # => 1970-01-01 20:00:00 UTC
event.save
event.reload
event.start_time # => 2000-01-01 20:00:00 UTC

After:

event = Event.new(start_time: { 4 => 20, 5 => 30 })
event.start_time # => 2000-01-01 20:00:00 UTC
event.save
event.reload
event.start_time # => 2000-01-01 20:00:00 UTC

Before:
```
event = Event.new(start_time: { 4 => 20, 5 => 30 })
event.start_time # => 1970-01-01 20:00:00 UTC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be great to fix this example: 20:00:00 UTC => 20:30:00 UTC and the same below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bogdanvlviv ahh, good spot

@sikachu
Copy link
Member

sikachu commented Jan 21, 2019

So, I'm just curious about this change. Would you mind guiding me to where the default of year 2000 is being set? Does that come from Ruby or from the database server if you give them partial dates?

@pixeltrix
Copy link
Contributor Author

So, I'm just curious about this change. Would you mind guiding me to where the default of year 2000 is being set? Does that come from Ruby or from the database server if you give them partial dates?

@sikachu It's a longstanding Rails convention - probably chosen when Time didn't support the range of values it does now. DBs like MySQL and PostgreSQL just return the time of day and we have to pick a date for the Time instance and 2000-01-01 is as good as any.

When assigning a hash to a time attribute that's missing a year
component (e.g. a `time_select` with `:ignore_date` set to `true`)
then the year defaults to 1970 instead of the expected 2000. This
results in the attribute changing as a result of the save.

Before:

    event = Event.new(start_time: { 4 => 20, 5 => 30 })
    event.start_time # => 1970-01-01 20:30:00 UTC
    event.save
    event.reload
    event.start_time # => 2000-01-01 20:30:00 UTC

After:

    event = Event.new(start_time: { 4 => 20, 5 => 30 })
    event.start_time # => 2000-01-01 20:30:00 UTC
    event.save
    event.reload
    event.start_time # => 2000-01-01 20:30:00 UTC
@pixeltrix pixeltrix force-pushed the fix-time-multiparameter-casting branch from 9fe85f6 to ccdedeb Compare January 21, 2019 09:24
@pixeltrix pixeltrix merged commit 4f62e75 into master Jan 21, 2019
@pixeltrix pixeltrix deleted the fix-time-multiparameter-casting branch January 21, 2019 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants