UTC vs Local Time
Paluth opened this issue · 3 comments
Hi,
I testing out this library. It works well for what I'm trying because I can build a Date object directly, instead of getting a union type like Maybe Date or Result Date.
So I have this code in Test.elm
module Test exposing (initialDate)
import Date.Extra as Date
import Date exposing (Month(..))
initialDate =
Date.fromSpec
Date.utc
(Date.atTime 6 0 0 0)
(Date.calendarDate 2015 Jan 1)
And this code in Main.elm
import Html
import Test as Test
main = Html.text (toString Test.initialDate)
When I run it in elm-reactor I get:
<Thu Jan 01 2015 04:00:00 GMT-0200 (E. South America Daylight Time)>
But I expected something like
<Thu Jan 01 2015 06:00:00 GMT-0000 (UTC)>
Why doesn't that work? Is it a bug or is my code wrong?
I want to use this library to control the date/time of a game that is set in the future. The game date/time has to be the same regardless of where the player is located. In other words, his local time is completely irrelevant and should never affect the in game clock. Otherwise the in game character might wake up a 6:00am if the player in from one country, and wake up at 8:00 am if the player is from another one.
Anyways, thanks for the package.
Hi Paluth,
It looks like Test.initialDate
was created as you expected, but Basics.toString
didn't convert it as you expected.
Basics.toString
returns a representation of a Date in your local time rather than UTC time, so its output for the same date will vary depending on the machine. In your example, a time of "04:00 GMT-0200" represents the same moment as "06:00 GMT+0000" (and in another time zone, you could see "01:00 GMT-0500"; all of these are valid representions of the same moment). To convert a Date to a string representing UTC time rather than local time, you can use the toUtcFormattedString
function in Date.Extra
; its output for the same date should be the same, regardless of the local time of a machine.
Date.toUtcFormattedString "EEE MMM dd yyyy HH:mm:ss 'GMT'xx" Test.initialDate
-- "Thu Jan 01 2015 06:00:00 GMT+0000"
You can read more about date format patterns at UTS #35, and you can find the list of symbols supported by toUtcFormattedString
in the docs.
Hope this helps!
That's great.
It didn't occur to me that the problem might be in the Basics.toString
function. I was not going to use anyways, I was just using it to test whether things were working as expected.
I another question, back since this issue is resolved, and my question is completely unrelated, I will close this issue and open another one.
I gave you a star, I hope this project gets more visibility because its pretty nice and you seam to have put quite a lot of work into it. Thanks again.
Thanks, Paluth; I appreciate the support.