haskell / time

A time library

Home Page:http://hackage.haskell.org/package/time

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`systemToUTCTime` can produce invalid UTCTime

carbolymer opened this issue · comments

λ systemToUTCTime $ MkSystemTime 8728097517656726399 3556790486
276582401868-12-21 23:59:62.556790486 UTC

Take a look at the seconds value: 62.556790486
Reproduced on time 1.9.3.

As a workaround I can use truncateSystemTimeLeapSecond which produces a "better" value.

That's an invalid SystemTime, systemNanoseconds should be in the range 0 to 2E9-1.

@AshleyYakeley You're right. Thanks for pointing that out. I've stumbled upon this issue in our test generator:

genUTCTime =
 systemToUTCTime <$>
   (MkSystemTime <$> Gen.int64 Range.constantBounded
                 <*> Gen.word32 Range.constantBounded)

which obviously isn't correct, because it generates nanoseconds all the way up to 2^32-1.

I'm wondering if it wouldn't be beneficial here to introduce a smart constructor or a pattern synonym, which would prevent a user from creating invalid SystemTime values. For example it could clip the value up to 2E9-1. Would it make sense?

Not really. It's rare people want leap seconds in any case, so most of the time they'd want to clip to 1E9-1 instead. Far better to keep it simple.