New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix year value when casting a multiparameter time hash #34990

Merged
merged 1 commit into from Jan 21, 2019

Conversation

Projects
None yet
3 participants
@pixeltrix
Copy link
Member

pixeltrix commented Jan 20, 2019

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

This comment has been minimized.

@bogdanvlviv

bogdanvlviv Jan 20, 2019

Contributor

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

This comment has been minimized.

@pixeltrix

pixeltrix Jan 21, 2019

Author Member

@bogdanvlviv ahh, good spot

@sikachu

This comment has been minimized.

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

This comment has been minimized.

Copy link
Member Author

pixeltrix 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?

@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.

Fix year value when casting a multiparameter time hash
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 Jan 21, 2019

@pixeltrix pixeltrix merged commit 4f62e75 into master Jan 21, 2019

3 checks passed

codeclimate All good!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

@pixeltrix pixeltrix deleted the fix-time-multiparameter-casting branch Jan 21, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment