Skip to content

Instantly share code, notes, and snippets.

@mrange
Created November 28, 2022 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrange/e8bc1786dff749b2debcee0bbbba3b48 to your computer and use it in GitHub Desktop.
Save mrange/e8bc1786dff749b2debcee0bbbba3b48 to your computer and use it in GitHub Desktop.
Illustrating invalid and ambiguous dates in F#
open System
open System.Globalization
let dtos =
let ts = TimeSpan(2, 0, 0)
[|
// Nothing weird about this date
DateTimeOffset(2022, 6, 22, 2, 30, 0, ts)
// This time is invalid in CEST because clock is set ahead at this date
DateTimeOffset(2022, 3, 27, 2, 30, 0, ts)
// This time is ambiguous in CEST because clock is set back at this date
DateTimeOffset(2022, 10, 30, 2, 30, 0, ts)
|]
let cest = TimeZoneInfo.FindSystemTimeZoneById "Central European Standard Time"
for dto in dtos do
let s = dto.ToString ("s", CultureInfo.InvariantCulture)
if cest.IsInvalidTime dto.DateTime then
printfn "%s is invalid datetime" s
elif cest.IsAmbiguousTime dto then
let tos = cest.GetAmbiguousTimeOffsets dto
printfn "%s is ambiguous datetime (Possible offsets: %A)" s tos
else
printfn "%s is normal" s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment