Code Monkey home page Code Monkey logo

time-zone-article's People

Contributors

bhfailor avatar dependabot[bot] avatar lazylester avatar ramhoj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

talyric

time-zone-article's Issues

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

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.