0
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.

Pay C.
  • 1,048
  • 1
  • 13
  • 20
  • You can probably do so using a library such as [elm-community/elm-time](https://package.elm-lang.org/packages/elm-community/elm-time/latest/) or [Bogdanp/elm-time](https://package.elm-lang.org/packages/Bogdanp/elm-time/1.4.0) – glennsl Mar 13 '20 at 10:26

1 Answers1

1

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
glennsl
  • 28,186
  • 12
  • 57
  • 75