Created
November 28, 2022 06:37
-
-
Save mrange/e8bc1786dff749b2debcee0bbbba3b48 to your computer and use it in GitHub Desktop.
Illustrating invalid and ambiguous dates in F#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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