haskell / time

A time library

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CalendarDiffTime not normalised?

tomjaguarpaw opened this issue · comments

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"

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

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"

Simpler example:

Prelude Data.Time> let cd = scaleCalendarDiffDays 3650 calendarDay
Prelude Data.Time> formatTime defaultTimeLocale "%y" cd
"0"

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

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?

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.