Pay C.
Pay C.

Reputation: 1108

Use Time.now to get time zoned date in Elm 0.18

posixDateTask : Task.Task x Time.Date.Date
posixDateTask = 
    let
        timeZoneDate now =
            Time.Date.date
                (Time.ZonedDateTime.year (Time.ZonedDateTime.fromDateTime (TimeZones.canada_pacific ()) now))
                (Time.ZonedDateTime.month (Time.ZonedDateTime.fromDateTime (TimeZones.canada_pacific ()) now))
                (Time.ZonedDateTime.day (Time.ZonedDateTime.fromDateTime (TimeZones.canada_pacific ()) now))
    in
    Time.now
        |> Task.map timeZoneDate

This gives me an error:

    |> Task.map timeZoneDate

(|>) is expecting the right side to be a:

     Task.Task x Time.Time -> a

But the right side is:

    Task.Task x Time.DateTime.DateTime -> Task.Task x Time.Date.Date

How should I change to return a Task.Task x Time.Date.Date type. I don't know where is the Time.DateTime.DateTime come from.

Upvotes: 0

Views: 104

Answers (1)

glennsl
glennsl

Reputation: 29106

Time.now returns a task with a Time.Time, which is what you pass to timeZoneDate as now. You then pass now to Time.ZonedDateTime.fromDateTime, which expects a Time.DateTime.DateTime (which shouldn't be entirely surprising given its name.) You therefore have to convert now from Time.Time to Time.DateTime.DateTime. It seems you can do that using Time.DateTime.fromTimestamp.

Based on that, something along the liens of this should work:

posixDateTask : Task.Task x Time.Date.Date
posixDateTask = 
    let
        timeZoneDate now =
            let
                dateTime =
                    Time.DateTime.fromTimeStamp now

                timeZone =
                    TimeZones.canada_pacific ()

                zonedDateTime =
                    Time.ZonedDateTime.fromDateTime timeZone dateTime
            in
            Time.Date.date
                (Time.ZonedDateTime.year zonedDateTime)
                (Time.ZonedDateTime.month zonedDateTime)
                (Time.ZonedDateTime.day zonedDateTime)
    in
    Time.now
        |> Task.map timeZoneDate

Upvotes: 1

Related Questions