You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Behaviour change when comparing Date and Time between previous versions and 3.3.0 #86
I'm not sure this is the right place to put such issue, but I found an issue after updating from 3.0.1 to 3.3.3.
I tracked the change in behaviour to start appearing between versions 3.2.2 and 3.3.0, here's and example of the change:
with version 3.2.2
require"time"require"date"Time.parse("1000-01-01T00:00:00Z")# date v3.2.2 => "1000-01-01T00:00:00.000Z"Time.parse("1000-01-01T00:00:00Z").to_date# date v3.2.2 => Wed, 27 Dec 0999Date.parse("1000-01-01") == Time.parse("1000-01-01T00:00:00Z")# date v3.2.2 => trueTime.parse("1000-01-01T00:00:00Z") == Date.parse("1000-01-01")# date v3.2.2 => true
with version 3.3.0
require"time"require"date"Time.parse("1000-01-01T00:00:00Z")# date v3.3.3 => "1000-01-01T00:00:00.000Z"Time.parse("1000-01-01T00:00:00Z").to_date# date v3.3.0 => Wed, 27 Dec 0999Date.parse("1000-01-01") == Time.parse("1000-01-01T00:00:00Z")# date v3.3.3 => falseTime.parse("1000-01-01T00:00:00Z") == Date.parse("1000-01-01")# date v3.3.3 => false
As you can see I'm working with date in the Julian calendar and comparing them used to work before 3.3.0.
Now I'm not sure if and what is the bug there, as this was maybe already an issue:
Time.parse("1000-01-01T00:00:00Z").to_date# => Wed, 27 Dec 0999
But in the end, this change of behaviour surfaced and I wanted to be sure it's reported.
I am not sure about the overall topic, but it deserves noting that Time and Date aren't comparable by default, as far as I remember, and never were. E.g. in pure Ruby, without additions like ActiveSupport, you should expect this:
...was NOT Ruby's core Date or Time. It might've been ActiveSupport:
require'active_support/all'd == t#=> trued <=> t#=> 0Date.parse("1000-01-01") == Time.parse("1000-01-01T00:00:00Z")#=> true for me in Ruby 2.7#=> false for me in Ruby 3.2
ActiveSupport uses Time.to_datetime coercion underneath, and it indeed have changed:
Time.parse("1000-01-01T00:00:00Z").to_datetime#=> Mon, 01 Jan 1000 00:00:00 +0000 on 2.7#=> Wed, 27 Dec 0999 00:00:00 +0000 on 3.2
...which is now consistent with to_date (which is weird, but was weird the same way in Ruby 2.7 and Ruby 3.2).
I don't feel qualified or have enough time right now to analyze this behavior.
Time has used the proleptic Gregorian calendar from the beginning (more precisely, it has never considered the Julian calendar), and Date uses the Italian reform date by default.
That means these two dates are actually different days.
@zverok Thanks for checking this out. I intentionaly reduced my test case to ruby core/stdlib only, no rails lib in there. Also my issue is a difference even with the same Ruby version, but different date gem versions.
@nobu Thanks for your answer, I get the difference but then isn't it an inconsistency between Time and Date that should be sorted out?
Let's say I have a db (or json, csv, etc.) with:
date
time
1000-01-01
1000-01-01T00:00:00Z
How am I supposed to parse them so they are considered to be on the same day?
Thanks for checking this out. I intentionaly reduced my test case to ruby core/stdlib only, no rails lib in there.
If it would be so, == between Date and Time would never return true. Unless I am missing something important (but I checked thoroughly before writing).
isn't it an inconsistency between Time and Date that should be sorted out?
Basically, date standard library is considered by core devs as a "scientific" library (while the core Time class is more simple/pragmatic).
Thus, core Time doesn't support non-Gregorian calendars and shouldn't be used for dates where it is inappropriate.
This situation is not ideal and not obvious, especially coming from Rails which liberally use Date for all dates representation, but it is what it is.
date standard library, on the other hand, has a calendar-aware DateTime class. So,
How am I supposed to parse them so they are considered to be on the same day?
Thanks for checking this out. I intentionaly reduced my test case to ruby core/stdlib only, no rails lib in there.
If it would be so, == between Date and Time would never return true. Unless I am missing something important (but I checked thoroughly before writing).
Indeed, I checked with a clean slate docker ruby:
> docker run -it --rm ruby:3.0-slim gem list | grep date
date (default: 3.1.3)
Ok, for it to work, everything has to use only Date and DateTime.
Yes, date standard library promises and provides no direct compatibility (other than conversion) with Time core class. As I've said above, this situation is not ideal and not obvious, but it is what it is.
One last example which is then also confusing:
I am not sure what it demonstrates?.. For me on Ruby 3.2 it is
Activity
zverok commentedon Jan 23, 2023
I am not sure about the overall topic, but it deserves noting that
TimeandDatearen't comparable by default, as far as I remember, and never were. E.g. in pure Ruby, without additions like ActiveSupport, you should expect this:...so, whatever produced
...was NOT Ruby's core
DateorTime. It might've been ActiveSupport:ActiveSupport uses
Time.to_datetimecoercion underneath, and it indeed have changed:...which is now consistent with
to_date(which is weird, but was weird the same way in Ruby 2.7 and Ruby 3.2).I don't feel qualified or have enough time right now to analyze this behavior.
zverok commentedon Jan 23, 2023
...but it seems to be related to this PR: #73
nobu commentedon Jan 24, 2023
Timehas used the proleptic Gregorian calendar from the beginning (more precisely, it has never considered the Julian calendar), andDateuses the Italian reform date by default.That means these two dates are actually different days.
JonathanTron commentedon Jan 24, 2023
@zverok Thanks for checking this out. I intentionaly reduced my test case to ruby core/stdlib only, no rails lib in there. Also my issue is a difference even with the same Ruby version, but different
dategem versions.@nobu Thanks for your answer, I get the difference but then isn't it an inconsistency between Time and Date that should be sorted out?
Let's say I have a db (or json, csv, etc.) with:
How am I supposed to parse them so they are considered to be on the same day?
zverok commentedon Jan 24, 2023
@JonathanTron
If it would be so,
==betweenDateandTimewould never returntrue. Unless I am missing something important (but I checked thoroughly before writing).Basically,
datestandard library is considered by core devs as a "scientific" library (while the coreTimeclass is more simple/pragmatic).Thus, core
Timedoesn't support non-Gregorian calendars and shouldn't be used for dates where it is inappropriate.This situation is not ideal and not obvious, especially coming from Rails which liberally use
Datefor all dates representation, but it is what it is.datestandard library, on the other hand, has a calendar-awareDateTimeclass. So,JonathanTron commentedon Jan 24, 2023
@zverok
Indeed, I checked with a clean slate docker ruby:
Ok, for it to work, everything has to use only
DateandDateTime.One last example which is then also confusing:
So going from
TimetoDateis not the same as going fromTimetoDateTime.zverok commentedon Jan 24, 2023
Yes,
datestandard library promises and provides no direct compatibility (other than conversion) withTimecore class. As I've said above, this situation is not ideal and not obvious, but it is what it is.I am not sure what it demonstrates?.. For me on Ruby 3.2 it is
...which is consistent with
parse-based examples above.Before Ruby 3.2, it was:
...which was a calendar-ignoring bug fixed by #73, the situation is no different for what we discussed about
#parseJonathanTron commentedon Jan 24, 2023
@zverok Indeed, my last example is something which worked also before but not after the fix from #73.
Thanks again for taking the time to dig and explain.