Thoth.Json icon indicating copy to clipboard operation
Thoth.Json copied to clipboard

vNext - Encode/Decode the date as they are

Open njlr opened this issue 4 years ago • 3 comments

When I decode a date-time like "2021-05-25T19:54:14.4020920", the time gets shifted an hour earlier. I guess this is related to time-zones.

Is this a bug? I was expecting the decoded value to be "as-is", so without time-zone information.

Probably best explained with code...

#r "nuget: Thoth.Json.Net, 5.0"

open System
open Thoth.Json.Net

let dt = DateTime.Parse "2021-05-25T19:54:14.4020920"

printfn "%A" dt

let jsonString =
  Encode.datetime dt
  |> Encode.toString 2

printfn "%s" jsonString

let decoded =
  jsonString
  |> Decode.fromString Decode.datetime

printfn "%A" decoded // Ok 25/05/2021 18:54:14

// Work-around
let decodeDateTime2 : Decoder<DateTime> =
  Decode.string
  |> Decode.andThen (fun s ->
    match DateTime.TryParse s with
    | (true, x) -> Decode.succeed x
    | _ -> Decode.fail (sprintf "\"%s\" is not a valid date-time" s))

let decoded2 =
  jsonString
  |> Decode.fromString decodeDateTime2

printfn "%A" decoded2 // Ok 25/05/2021 19:54:14

njlr avatar Jun 11 '21 14:06 njlr

Thoth.Json(.Net) decoder are forcing the date to be converted into UTC time.

https://github.com/thoth-org/Thoth.Json/blob/9b24792ef99ada96aff4dd5eb57c11d98397396e/src/Decode.fs#L295-L302

My ideas for that was because Thoth.Json encode the date as UTC it makes sense to keep it as UTC when decoding it.

I never was sure of this decision and never were am able to decide if it was a good idea or not.

One idea, I had was to mark datetime osbelete and create datetimeUTC and datetimeLocal functions but I am unsure if that's really a benefit... or if I should encode and decode the date as they are without forcing the UTC.

Handling dates really is a nighmare... 🤣 (for me at least)

MangelMaxime avatar Jun 11 '21 16:06 MangelMaxime

Handling dates really is a nighmare... rofl (for me at least)

Yes agreed!

My expectation was that dates would be encoded/decoded as they are... The user has more context than the library, so should be up to them to convert it after decoding.

Would be happy if the library provided an "as-is" variant, even if datetime doesn't change :slightly_smiling_face:

njlr avatar Jun 11 '21 16:06 njlr

In the next version of Thoth.Json I already have a lot of breaking changes planned I will make the datetime not touch the date.

For now, if you are using manual decoders, you can write your own datetime functions

module Thoth.Json.Extra 

[<RequiredQualifiedAccess>]
module Decoder =
 let datetime : Decoder<System.DateTime> = 
     fun path value -> 
         if Helpers.isString value then 
             match System.DateTime.TryParse (Helpers.asString value) with 
             | true, x -> x |> Ok 
             | _ -> (path, BadPrimitive("a datetime", value)) |> Error 
         else 
             (path, BadPrimitive("a datetime", value)) |> Error 

MangelMaxime avatar Jun 12 '21 19:06 MangelMaxime

Will be included in the next release

MangelMaxime avatar Nov 09 '22 19:11 MangelMaxime