-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Month of year type #246
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,7 @@ | ||
{-# LANGUAGE Safe #-} | ||
|
||
module Data.Time.Calendar.MonthDay ( | ||
MonthOfYear, | ||
pattern January, | ||
pattern February, | ||
pattern March, | ||
pattern April, | ||
pattern May, | ||
pattern June, | ||
pattern July, | ||
pattern August, | ||
pattern September, | ||
pattern October, | ||
pattern November, | ||
pattern December, | ||
MonthOfYear(..), | ||
DayOfMonth, | ||
DayOfYear, | ||
monthAndDayToDayOfYear, | ||
|
@@ -30,11 +18,10 @@ import Data.Time.Calendar.Types | |
monthAndDayToDayOfYear :: Bool -> MonthOfYear -> DayOfMonth -> DayOfYear | ||
monthAndDayToDayOfYear isLeap month day = (div (367 * month'' - 362) 12) + k + day' | ||
where | ||
month' = clip 1 12 month | ||
day' = fromIntegral (clip 1 (monthLength' isLeap month') day) | ||
month'' = fromIntegral month' | ||
day' = fromIntegral (clip 1 (monthLength isLeap month) day) | ||
month'' = monthOfYearIndex month | ||
k = | ||
if month' <= 2 | ||
if month <= February | ||
then 0 | ||
else | ||
if isLeap | ||
|
@@ -45,13 +32,12 @@ monthAndDayToDayOfYear isLeap month day = (div (367 * month'' - 362) 12) + k + d | |
-- First arg is leap year flag. | ||
monthAndDayToDayOfYearValid :: Bool -> MonthOfYear -> DayOfMonth -> Maybe DayOfYear | ||
monthAndDayToDayOfYearValid isLeap month day = do | ||
month' <- clipValid 1 12 month | ||
day' <- clipValid 1 (monthLength' isLeap month') day | ||
day' <- clipValid 1 (monthLength isLeap month) day | ||
let | ||
day'' = fromIntegral day' | ||
month'' = fromIntegral month' | ||
month'' = monthOfYearIndex month | ||
k = | ||
if month' <= 2 | ||
if month <= February | ||
then 0 | ||
else | ||
if isLeap | ||
|
@@ -74,35 +60,27 @@ dayOfYearToMonthAndDay isLeap yd = | |
yd | ||
) | ||
|
||
findMonthDay :: [Int] -> Int -> (Int, Int) | ||
findMonthDay :: [Int] -> Int -> (MonthOfYear, Int) | ||
findMonthDay (n : ns) yd | ||
| yd > n = (\(m, d) -> (m + 1, d)) (findMonthDay ns (yd - n)) | ||
findMonthDay _ yd = (1, yd) | ||
| yd > n = (\(m, d) -> (succ m, d)) (findMonthDay ns (yd - n)) | ||
findMonthDay _ yd = (January, yd) | ||
|
||
-- | The length of a given month in the Gregorian or Julian calendars. | ||
-- First arg is leap year flag. | ||
monthLength :: Bool -> MonthOfYear -> DayOfMonth | ||
monthLength isLeap month' = monthLength' isLeap (clip 1 12 month') | ||
monthLength isLeap month = case month of | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function may be slightly more performant now. |
||
January -> 31 | ||
February -> if isLeap then 29 else 28 | ||
March -> 31 | ||
April -> 30 | ||
May -> 31 | ||
June -> 30 | ||
July -> 31 | ||
August -> 31 | ||
September -> 30 | ||
October -> 31 | ||
November -> 30 | ||
December -> 31 | ||
|
||
monthLength' :: Bool -> MonthOfYear -> DayOfMonth | ||
monthLength' isLeap month' = (monthLengths isLeap) !! (month' - 1) | ||
|
||
monthLengths :: Bool -> [DayOfMonth] | ||
monthLengths isleap = | ||
[ 31 | ||
, if isleap | ||
then 29 | ||
else 28 | ||
, 31 | ||
, 30 | ||
, 31 | ||
, 30 | ||
, 31 | ||
, 31 | ||
, 30 | ||
, 31 | ||
, 30 | ||
, 31 | ||
] | ||
|
||
-- J F M A M J J A S O N D | ||
monthLengths :: Bool -> [Int] | ||
monthLengths isLeap = map (monthLength isLeap) [minBound .. maxBound] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left this function for backward compatibility even though it is now no longer necessary.