haskell / time

A time library
http://hackage.haskell.org/package/time
Other
119 stars 79 forks source link

CalendarDiffTime not normalised? #185

Closed tomjaguarpaw closed 2 years ago

tomjaguarpaw commented 2 years ago

ctt below should be a very large number of years, but formatTime shows 0. The cause is probably that CalendarDiffTime is not normalised by calendarTimeTime. I'm not really sure what should happen here. I'm not even sure hat a CalendarDiffTime is supposed to be.

> let ctt = calendarTimeTime (secondsToNominalDiffTime (10 ^ 1000000))
> formatTime defaultTimeLocale "%y" ctt
"0"
AshleyYakeley commented 2 years ago

formatTime isn't expected to work outside the range 9999BCE to 9999CE.

tomjaguarpaw commented 2 years ago

That explanation doesn't seem to apply here. It's a duration, not a point in time. Anyway, here's an example of a CalendarDiffTime that corresponds to roughly 10 years:

> let ctt = calendarTimeTime (secondsToNominalDiffTime (60 * 60 * 24 * 365 * 10))
> formatTime defaultTimeLocale "%y" ctt
"0"
AshleyYakeley commented 2 years ago

Simpler example:

Prelude Data.Time> let cd = scaleCalendarDiffDays 3650 calendarDay
Prelude Data.Time> formatTime defaultTimeLocale "%y" cd
"0"
AshleyYakeley commented 2 years ago

The idea here is that cd is exactly zero months and 3650 days.

tomjaguarpaw commented 2 years ago

The idea here is that cd is exactly zero months and 3650 days.

Right, so are you saying that is expected behaviour? If so then I don't understand what the semantics of a CalendarDiffTime are supposed to be. Could you clarify?

AshleyYakeley commented 2 years ago

A CalendarDiffDays is a number of months together with a number of days. It's needed for some ISO 8601 stuff.

It represents "calendrical" intervals such as "2 years, 5 months, 1 week and 4 days". In this case, since a year is always 12 months, and a week is always 7 days, that is represented as "29 months and 11 days". And given a particular day, you can add this calendrical interval to find a day 29 months and 11 days from then.

The important thing to note is that "365 days" is not the same as "12 months". The %y is going to give you the total number of whole years, i.e. the number of months div 12. So zero months and 3650 days is zero years for %y.