2021-07-16 19:36:30 +03:00
|
|
|
module Timestamps exposing (Timestamps, data, format)
|
|
|
|
|
2023-01-02 01:44:11 +03:00
|
|
|
import BackendTask exposing (BackendTask)
|
2023-01-17 18:04:18 +03:00
|
|
|
import BackendTask.Custom
|
2021-07-16 19:36:30 +03:00
|
|
|
import DateFormat
|
2022-08-25 21:56:03 +03:00
|
|
|
import Json.Decode as Decode
|
2022-01-26 22:05:55 +03:00
|
|
|
import Json.Decode.Extra
|
2021-07-16 19:36:30 +03:00
|
|
|
import Json.Encode
|
|
|
|
import List.Extra
|
|
|
|
import Result.Extra
|
|
|
|
import Time
|
|
|
|
|
|
|
|
|
|
|
|
type alias Timestamps =
|
|
|
|
{ updated : Time.Posix
|
|
|
|
, created : Time.Posix
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-02 01:44:11 +03:00
|
|
|
data : String -> BackendTask Timestamps
|
2021-07-16 19:36:30 +03:00
|
|
|
data filePath =
|
2023-01-17 18:14:03 +03:00
|
|
|
BackendTask.Custom.run "gitTimestamps"
|
2021-07-16 19:36:30 +03:00
|
|
|
(Json.Encode.string filePath)
|
|
|
|
(Decode.string
|
|
|
|
|> Decode.map (String.trim >> String.split "\n")
|
|
|
|
|> Decode.map (List.map secondsStringToPosix)
|
|
|
|
|> Decode.map Result.Extra.combine
|
2021-08-13 18:29:05 +03:00
|
|
|
|> Decode.map
|
|
|
|
(Result.withDefault
|
|
|
|
[ Time.millisToPosix 0
|
|
|
|
, Time.millisToPosix 0
|
|
|
|
]
|
|
|
|
)
|
2021-07-16 19:36:30 +03:00
|
|
|
|> Decode.map (firstAndLast Timestamps >> Result.fromMaybe "Error")
|
2022-01-26 22:05:55 +03:00
|
|
|
|> Decode.andThen Json.Decode.Extra.fromResult
|
2021-07-16 19:36:30 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
firstAndLast : (a -> a -> b) -> List a -> Maybe b
|
|
|
|
firstAndLast constructor list =
|
|
|
|
Maybe.map2 constructor
|
|
|
|
(List.head list)
|
|
|
|
(List.Extra.last list)
|
|
|
|
|
|
|
|
|
|
|
|
secondsStringToPosix : String -> Result String Time.Posix
|
|
|
|
secondsStringToPosix posixTime =
|
|
|
|
posixTime
|
|
|
|
|> String.trim
|
|
|
|
|> String.toInt
|
|
|
|
|> Maybe.map (\unixTimeInSeconds -> (unixTimeInSeconds * 1000) |> Time.millisToPosix)
|
|
|
|
|> Result.fromMaybe "Expected int"
|
|
|
|
|
|
|
|
|
|
|
|
format : Time.Posix -> String
|
|
|
|
format posix =
|
|
|
|
DateFormat.format
|
|
|
|
[ DateFormat.monthNameFull
|
|
|
|
, DateFormat.text " "
|
|
|
|
, DateFormat.dayOfMonthNumber
|
|
|
|
, DateFormat.text ", "
|
|
|
|
, DateFormat.yearNumber
|
|
|
|
]
|
|
|
|
pacificZone
|
|
|
|
posix
|
|
|
|
|
|
|
|
|
|
|
|
pacificZone : Time.Zone
|
|
|
|
pacificZone =
|
|
|
|
Time.customZone (-60 * 7) []
|