-
Notifications
You must be signed in to change notification settings - Fork 48
Add generators for date and time types #59
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
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Data.Date.Component.Gen where | ||
|
||
import Prelude | ||
import Control.Monad.Gen (class MonadGen, chooseInt) | ||
import Data.Date.Component (Day, Month, Weekday, Year) | ||
import Data.Enum (toEnum) | ||
import Data.Enum.Gen (genBoundedEnum) | ||
import Data.Maybe (fromJust) | ||
import Partial.Unsafe (unsafePartial) | ||
|
||
-- | Generates a random `Year` in the range 1900-2100, inclusive. | ||
genYear :: forall m. MonadGen m => m Year | ||
genYear = unsafePartial fromJust <<< toEnum <$> chooseInt 1900 2100 | ||
|
||
-- | Generates a random `Month` component. | ||
genMonth :: forall m. MonadGen m => m Month | ||
genMonth = genBoundedEnum | ||
|
||
-- | Generates a random `Day` component. | ||
genDay :: forall m. MonadGen m => m Day | ||
genDay = genBoundedEnum | ||
|
||
-- | Generates a random `Weekday` component. | ||
genWeekday :: forall m. MonadGen m => m Weekday | ||
genWeekday = genBoundedEnum |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Data.Date.Gen | ||
( genDate | ||
, module Data.Date.Component.Gen | ||
) where | ||
|
||
import Prelude | ||
import Control.Monad.Gen (class MonadGen) | ||
import Data.Date (Date, canonicalDate) | ||
import Data.Date.Component.Gen (genDay, genMonth, genWeekday, genYear) | ||
|
||
-- | Generates a random `Date` between 1st Jan 1900 and 31st Dec 2100, | ||
-- | inclusive. | ||
genDate :: forall m. MonadGen m => m Date | ||
genDate = canonicalDate <$> genYear <*> genMonth <*> genDay |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Data.DateTime.Gen | ||
( genDateTime | ||
, module Data.Date.Gen | ||
, module Data.Time.Gen | ||
) where | ||
|
||
import Prelude | ||
import Control.Monad.Gen (class MonadGen) | ||
import Data.Date.Gen (genDate, genDay, genMonth, genWeekday, genYear) | ||
import Data.DateTime (DateTime(..)) | ||
import Data.Time.Gen (genHour, genMillisecond, genMinute, genSecond, genTime) | ||
|
||
-- | Generates a random `DateTime` between 1st Jan 1900 00:00:00 and | ||
-- | 31st Dec 2100 23:59:59, inclusive. | ||
genDateTime :: forall m. MonadGen m => m DateTime | ||
genDateTime = DateTime <$> genDate <*> genTime |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Data.Time.Component.Gen where | ||
|
||
import Control.Monad.Gen (class MonadGen) | ||
import Data.Enum.Gen (genBoundedEnum) | ||
import Data.Time.Component (Hour, Millisecond, Minute, Second) | ||
|
||
-- | Generates a random `Hour` component. | ||
genHour :: forall m. MonadGen m => m Hour | ||
genHour = genBoundedEnum | ||
|
||
-- | Generates a random `Minute` component. | ||
genMinute :: forall m. MonadGen m => m Minute | ||
genMinute = genBoundedEnum | ||
|
||
-- | Generates a random `Second` component. | ||
genSecond :: forall m. MonadGen m => m Second | ||
genSecond = genBoundedEnum | ||
|
||
-- | Generates a random `Millisecond` component. | ||
genMillisecond :: forall m. MonadGen m => m Millisecond | ||
genMillisecond = genBoundedEnum |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Data.Time.Duration.Gen where | ||
|
||
import Prelude | ||
|
||
import Control.Monad.Gen (class MonadGen) | ||
import Control.Monad.Gen as Gen | ||
import Data.Time.Duration (Days(..), Hours(..), Milliseconds(..), Minutes(..), Seconds(..)) | ||
|
||
-- | Generates a random `Milliseconds` duration, up to 10 minutes. | ||
genMilliseconds :: forall m. MonadGen m => m Milliseconds | ||
genMilliseconds = Milliseconds <$> Gen.chooseFloat 0.0 600000.0 | ||
|
||
-- | Generates a random `Seconds` duration, up to 10 minutes. | ||
genSeconds :: forall m. MonadGen m => m Seconds | ||
genSeconds = Seconds <$> Gen.chooseFloat 0.0 600.0 | ||
|
||
-- | Generates a random `Seconds` duration, up to 10 hours. | ||
genMinutes :: forall m. MonadGen m => m Minutes | ||
genMinutes = Minutes <$> Gen.chooseFloat 0.0 600.0 | ||
|
||
-- | Generates a random `Hours` duration, up to 10 days. | ||
genHours :: forall m. MonadGen m => m Hours | ||
genHours = Hours <$> Gen.chooseFloat 0.0 240.0 | ||
|
||
-- | Generates a random `Days` duration, up to 6 weeks. | ||
genDays :: forall m. MonadGen m => m Days | ||
genDays = Days <$> Gen.chooseFloat 0.0 42.0 |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Data.Time.Gen | ||
( genTime | ||
, module Data.Time.Component.Gen | ||
) where | ||
|
||
import Prelude | ||
import Control.Monad.Gen (class MonadGen) | ||
import Data.Time (Time(..)) | ||
import Data.Time.Component.Gen (genHour, genMillisecond, genMinute, genSecond) | ||
|
||
-- | Generates a random `Time` between 00:00:00 and 23:59:59, inclusive. | ||
genTime :: forall m. MonadGen m => m Time | ||
genTime = Time <$> genHour <*> genMinute <*> genSecond <*> genMillisecond |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Shouldn't this take a
Month
as an argument?Uh oh!
There was an error while loading. Please reload this page.
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.
Hmm, that's a good point. I guess it would need to receive the
Year
also, in that case.Maybe it's fine as is though, since you can't construct an invalid
Date
anyway (canonicalDate
will fix it, orexactDate
will returnNothing
) - but we could add an alternative generator that does take month/year to ensure it will be correct for a specific period.