Skip to content
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

Date.today uses server timezone to set the date for "today" » Errors when turning into timezone #1

Closed
jufemaiz opened this issue Aug 17, 2015 · 2 comments

Comments

@jufemaiz
Copy link

Lets say you've got a server running UTC.

For example, let's say the current time is 2015-08-17T22:00:00UTC and you call "Date.today". You're going to get the following result:

Date.today
2015-08-17

Now, let's say you're working in a timezone that's ahead of UTC - for example "Australia/Sydney" (UTC+1000/UTC+1100DST). Now when we run Date.today.in_time_zone("Australia/Sydney").

Date.today.in_time_zone("Australia/Sydney")
Mon, 17 Aug 2015 00:00:00 AEST +10:00

This is due to the fact that the Date.today ignores completely any reference to timezones once created. Hence there is no relative offset between the Date.today and the timezone to reference for the in_time_zone function. Hence it uses it as the base date for the day in that timezone - which would actually be "yesterday" when compared with what "today" is in that timezone.

A corollary exists for timezones lagging UTC. If the current time is 2015-08-18T01:00:00UTC we get the following:

Date.today
2015-08-18
Date.today.in_time_zone("America/Chicago")
Tue, 18 Aug 2015 00:00:00 CDT -05:00

In order to correctly make reference to "today" in the appropriate timezone, you need to have a TZ reference in the creation of the originating "today" object.

For instance in the first example, we would get:

Time.now.in_time_zone("Australia/Sydney")
Tue, 18 Aug 2015 08:00:00 EST +10:00
Time.now.in_time_zone("Australia/Sydney").to_date
Tue, 18 Aug 2015

In the second instance we would have got:

Time.now.in_time_zone("America/Chicago")
Mon, 17 Aug 2015 20:00:00 CDT -05:00
Time.now.in_time_zone("America/Chicago").to_date
Mon, 17 Aug 2015

Hence, where dates are being used, it's better to use Time to initiate them, convert to timezone and then convert back to date to ensure that the "today" or any relative adjustment on "today" is made based on a point in time that has relative timezone differences accounted for.

describe Date do
describe "#in_time_zone" do
it "returns the date as a time in the current time zone" do
expect(Date.today.in_time_zone.iso8601).to eq("2014-03-15T00:00:00+04:30")
end
it "can return the date as a time in in a given time zone" do
expect(Date.today.in_time_zone("Adelaide").iso8601).to eq("2014-03-15T00:00:00+10:30")
end
end
end

@ramhoj
Copy link
Owner

ramhoj commented Aug 27, 2015

Thank you for this details explanation! I've pushed this commit c4fe548 that basically makes the blog post stop mentioning Date.today. Just as you're saying Date.today is just as bad as Time.now.

Please see the test scenarios to see if they are addressing the concern above. Is there any scenario you an think of in Rails where simply replacing Date.today with Date.current would be wrong/not work the way you want?

@ramhoj ramhoj closed this as completed Aug 27, 2015
@jufemaiz
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants