haskell / time

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

Week type #253

Closed NorfairKing closed 7 months ago

NorfairKing commented 7 months ago

It would be like Month but for absolute weeks, wrapping an integer.

AshleyYakeley commented 7 months ago

Starting which day?

NorfairKing commented 7 months ago

@AshleyYakeley Oh bother, good point.

I'd be happy with it being Monday and leaving it at that, but you could also put it at the type level or make its interpretation configurable I guess.

AshleyYakeley commented 7 months ago

Well, what's your anticipated use for the type?

AshleyYakeley commented 7 months ago

Are there any standard or pre-existing numberings of weeks?

NorfairKing commented 7 months ago

I want to resolve Monday to "the soonest monday after today" or "the most recently past monday". So I'd like to do something like:

resolve :: Day -> DayOfWeek -> Maybe Day
resolve today dow = 
  let (w, _) = toAbsoluteWeekDay today
      guessThisWeek = fromAbsoluteWeekDay (w, dow)
      guessNextWeek = fromAbsoluteWeekDay (succ w, dow)
  in if guessThisWeek > today then guessThisWeek else guessNextWeek

Currently I have to go via Year and WeekOfYear unlike I have to do when I want to do something similar for "the soonest 15th of the month after today".

Relevant code: https://hackage.haskell.org/package/fuzzy-time

NorfairKing commented 7 months ago

Are there any standard or pre-existing numberings of weeks?

Not that I can find here: https://en.wikipedia.org/wiki/ISO_week_date

In hindsight this would probably cause more confusion than it will help with, because of the "which start day?" problem.

AshleyYakeley commented 7 months ago

Can't you do this?

"the soonest monday after today" = firstDayOfWeekOnAfter Monday $ succ today

"the most recently past monday" = firstDayOfWeekOnAfter Monday $ addDays (negate 7) today

NorfairKing commented 7 months ago

Oh ehm, I didn't know about those functions Whoops, and Thanks!