Skip to content

Instantly share code, notes, and snippets.

@cschneid
Created April 11, 2015 03:00
Show Gist options
  • Select an option

  • Save cschneid/89fbce33ba9ee1c435b9 to your computer and use it in GitHub Desktop.

Select an option

Save cschneid/89fbce33ba9ee1c435b9 to your computer and use it in GitHub Desktop.
module Data.Time.DayOfWeek where
import Data.Time
import Data.Time.Calendar.WeekDate
data DayOfWeek = Sunday
| Monday
| Tuesday
| Wednesday
| Thursday
| Friday
| Saturday
deriving (Eq, Ord, Enum, Show)
dayOfWeek :: Day -> DayOfWeek
dayOfWeek d = toEnum $ (third $ toWeekDate d) `mod` 7
-- Find the previous Sunday to this date unless it is Sunday, then don't change
beginningOfWeek :: Day -> Day
beginningOfWeek d = addDays offset d
where
offset = negate . fromIntegral $ if third (toWeekDate d) == 7
then 0
else third $ toWeekDate d
first (a, _, _) = a
second (_, b, _) = b
third (_, _, c) = c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment