Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 0ef2efb

Browse files
committed
Add a randomDate provider
1 parent 5d26807 commit 0ef2efb

3 files changed

Lines changed: 14 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ a field within the JSON object.
2424
- randomChar
2525
- randomInt(lower, upper)
2626
- randomDouble(lower, upper)
27+
- randomDate
2728
- array(expr [, ...])
2829
- oneOf(arrayExpr)
2930
- oneOf(expr, expr [, ...])

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ dependencies:
3737
- containers
3838
- regex-tdfa
3939
- optparse-applicative
40+
- time
4041

4142
library:
4243
source-dirs: src

src/Fake.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import qualified Data.Scientific as S
2727
import qualified Data.Set as Set
2828
import qualified Data.Text as T
2929
import qualified Data.Text.Encoding as T
30+
import Data.Time.Calendar (Day (..), showGregorian)
3031
import qualified Data.UUID as UUID
3132
import qualified Data.UUID.V1 as UUID1
3233
import qualified Data.Vector as V
@@ -276,6 +277,16 @@ randomChar = charToString <$> State.state random
276277
charToString = String . T.pack . (: [])
277278

278279

280+
-- | Generate a random date between (inclusive) 1858-11-17 and 2132-09-01
281+
--
282+
-- >>> exec "randomDate()"
283+
-- String "2063-01-23"
284+
randomDate :: (RandomGen g, MonadState g m) => m Value
285+
randomDate = dayAsValue . ModifiedJulianDay <$> State.state (randomR (0, 100000))
286+
where
287+
dayAsValue = String . T.pack . showGregorian
288+
289+
279290
-- | Create a value getter for an expression
280291
--
281292
-- >>> exec "uuid4"
@@ -295,6 +306,7 @@ eval (FunctionCall "randomBool" []) = randomBool
295306
eval (FunctionCall "randomChar" []) = randomChar
296307
eval (FunctionCall "randomInt" [lower, upper]) = randomInt lower upper
297308
eval (FunctionCall "randomDouble" [lower, upper]) = randomDouble lower upper
309+
eval (FunctionCall "randomDate" []) = randomDate
298310
eval (FunctionCall "array" args) = Array . V.fromList <$> mapM eval args
299311
eval (FunctionCall "oneOf" [arg]) = oneOfArray arg
300312
eval (FunctionCall "oneOf" args) = oneOfArgs args

0 commit comments

Comments
 (0)