Skip to content

Commit fb48546

Browse files
Implement DateTimeBasicsLog (#192)
1 parent 5518fac commit fb48546

File tree

8 files changed

+160
-0
lines changed

8 files changed

+160
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Running a web-compatible recipe:
100100
| | :heavy_check_mark: | [ComponentsHalogenHooks](recipes/ComponentsHalogenHooks) | Demonstrates how to nest one Halogen-Hooks-based component inside another and send/receive queries between the two. |
101101
| | :heavy_check_mark: | [ComponentsInputHalogenHooks](recipes/ComponentsInputHalogenHooks) | Each time a parent re-renders, it will pass a new input value into the child, and the child will update accordingly. |
102102
| | :heavy_check_mark: | [ComponentsMultiTypeHalogenHooks](recipes/ComponentsMultiTypeHalogenHooks) | Demonstrates a component that can communicate with its children that have differing types. |
103+
| :heavy_check_mark: | :heavy_check_mark: | [DateTimeBasicsLog](recipes/DateTimeBasicsLog) | This recipe shows how to use `purescript-datetime` library to create `Time`, `Date`, and `DateTime` values and adjust/diff them. |
103104
| :heavy_check_mark: | :heavy_check_mark: | [DebuggingLog](recipes/DebuggingLog) | This recipe shows how to do print-debugging using the `Debug` module's `spy` and `traceM` functions. The compiler will emit warnings to remind you to remove these debug functions before you ship production code. |
104105
| :heavy_check_mark: | | [DiceCLI](recipes/DiceCLI) | This recipe shows how to create an interactive command line prompt that repeatedly generates a random number between 1 and 6. |
105106
| :heavy_check_mark: | :heavy_check_mark: | [DiceLog](recipes/DiceLog) | This recipe shows how to log a random integer between 1 and 6 (representing a roll of a die) in either the node.js or web browser console. |

recipes/DateTimeBasicsLog/.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/bower_components/
2+
/node_modules/
3+
/.pulp-cache/
4+
/output/
5+
/generated-docs/
6+
/.psc-package/
7+
/.psc*
8+
/.purs*
9+
/.psa*
10+
/.spago
11+
/web-dist/
12+
/prod-dist/
13+
/prod/

recipes/DateTimeBasicsLog/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# DateTimeBasicsLog
2+
3+
This recipe shows how to use `purescript-datetime` library to create `Time`, `Date`, and `DateTime` values and adjust/diff them.
4+
5+
## Expected Behavior:
6+
7+
Prints the following to the console. For the browser environement, make sure to open the console with dev tools first, then reload/refresh the page:
8+
```
9+
To create a specific part of a `Time` value, you must use the `toEnum` function. This ensures that the value you provide is within the correct bounds.
10+
For example, a Minute must be between 0 and 59. If you happen to pass it -1, what should it's value be? Should it be clamped to 0? Or should it be set to 59? Rather than deciding for you, it forces you to do the checking by using `toEnum` to return a `Maybe Minute`.
11+
toEnum 4: (Just (Minute 4))
12+
toEnum -8: Nothing
13+
toEnum 69: Nothing
14+
The same goes for Hour (0 - 23), Second (0 - 59), and Millisecond (0 - 999).
15+
toEnum 4: (Just (Hour 4))
16+
toEnum -8: Nothing
17+
toEnum 69: Nothing
18+
To create a `Time` value, you use the `Maybe` monad and do notation:
19+
mkTime 4 24 4 3: (Just (Time (Hour 4) (Minute 24) (Second 4) (Millisecond 3)))
20+
mkTime 42 842 2 -42: Nothing
21+
To create a `Date` value, we use the `Maybe` monad again:
22+
mkCanonicalDate 2016 Feb 31: (Just (Date (Year 2016) March (Day 2)))
23+
mkExactDate 2016 Feb 31: Nothing
24+
mkExactDate 2016 Feb 27: (Just (Date (Year 2016) February (Day 27)))
25+
mkDateTime 2016 Feb 27 @ 11:04:14:423: (Just (DateTime (Date (Year 2016) February (Day 27)) (Time (Hour 11) (Minute 4) (Second 14) (Millisecond 423))))
26+
Original DateTime: (DateTime (Date (Year 2016) February (Day 27)) (Time (Hour 11) (Minute 4) (Second 14) (Millisecond 423)))
27+
Add five seconds: (Just (DateTime (Date (Year 2016) February (Day 27)) (Time (Hour 11) (Minute 4) (Second 19) (Millisecond 423))))
28+
Add two days: (Just (DateTime (Date (Year 2016) February (Day 29)) (Time (Hour 11) (Minute 4) (Second 14) (Millisecond 423))))
29+
Add four hours: (Just (DateTime (Date (Year 2016) February (Day 27)) (Time (Hour 15) (Minute 4) (Second 14) (Millisecond 423))))
30+
Number of Days between two dateTimes: (Days 1.0)
31+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This file just indicates that the node backend is supported.
2+
It is used for CI and autogeneration purposes.

recipes/DateTimeBasicsLog/spago.dhall

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{ name = "DateTimeBasicsLog"
2+
, dependencies = [ "console", "datetime", "effect", "maybe", "psci-support" ]
3+
, packages = ../../packages.dhall
4+
, sources = [ "recipes/DateTimeBasicsLog/src/**/*.purs" ]
5+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
module DateTimeBasicsLog.Main where
2+
3+
import Prelude
4+
5+
import Data.Date (Date, Month(..), canonicalDate, exactDate)
6+
import Data.DateTime (DateTime(..), adjust, diff)
7+
import Data.Enum (toEnum)
8+
import Data.Maybe (Maybe, fromJust)
9+
import Data.Time (Hour, Minute, Time(..))
10+
import Data.Time.Duration as D
11+
import Effect (Effect)
12+
import Effect.Console (log)
13+
import Partial.Unsafe (unsafePartial)
14+
15+
main :: Effect Unit
16+
main = do
17+
log "To create a specific part of a `Time` value, you must use \
18+
\the `toEnum` function. This ensures that the value you provide \
19+
\is within the correct bounds. "
20+
log "For example, a Minute must be between 0 and 59. If you happen \
21+
\to pass it -1, what should it's value be? Should it be clamped to 0? \
22+
\Or should it be set to 59? Rather than deciding for you, it forces \
23+
\you to do the checking by using `toEnum` to return a `Maybe Minute`."
24+
25+
log $ "toEnum 4: " <> show (toEnum 4 :: Maybe Minute)
26+
log $ "toEnum -8: " <> show (toEnum (-8) :: Maybe Minute)
27+
log $ "toEnum 69: " <> show (toEnum 69 :: Maybe Minute)
28+
29+
log "The same goes for Hour (0 - 23), Second (0 - 59), and \
30+
\Millisecond (0 - 999)."
31+
log $ "toEnum 4: " <> show (toEnum 4 :: Maybe Hour)
32+
log $ "toEnum -8: " <> show (toEnum (-8) :: Maybe Hour)
33+
log $ "toEnum 69: " <> show (toEnum 69 :: Maybe Hour)
34+
35+
log "To create a `Time` value, you use the `Maybe` monad and do notation:"
36+
37+
let
38+
mkTime :: Int -> Int -> Int -> Int -> Maybe Time
39+
mkTime h m s ms = do
40+
Time <$> toEnum h
41+
<*> toEnum m
42+
<*> toEnum s
43+
<*> toEnum ms
44+
45+
log $ "mkTime 4 24 4 3: " <> show (mkTime 4 24 4 3) -- valid
46+
log $ "mkTime 42 842 2 -42: " <> show (mkTime 42 842 2 (-42)) -- invalid
47+
48+
log "To create a `Date` value, we use the `Maybe` monad again:"
49+
let
50+
mkCanonicalDate :: Int -> Month -> Int -> Maybe Date
51+
mkCanonicalDate year month day = ado
52+
year' <- toEnum year
53+
day' <- toEnum day
54+
in canonicalDate year' month day'
55+
56+
mkExactDate :: Int -> Month -> Int -> Maybe Date
57+
mkExactDate year month day = do
58+
year' <- toEnum year
59+
day' <- toEnum day
60+
exactDate year' month day'
61+
62+
log $ "mkCanonicalDate 2016 Feb 31: " <> show (mkCanonicalDate 2016 February 31)
63+
log $ "mkExactDate 2016 Feb 31: " <> show (mkExactDate 2016 February 31)
64+
log $ "mkExactDate 2016 Feb 27: " <> show (mkExactDate 2016 February 27)
65+
66+
let
67+
mkDateTime :: Int -> Month -> Int -> Int -> Int -> Int -> Int -> Maybe DateTime
68+
mkDateTime y month d h m s ms = ado
69+
date' <- mkCanonicalDate y month d
70+
time' <- mkTime h m s ms
71+
in DateTime date' time'
72+
73+
log $ "mkDateTime 2016 Feb 27 @ 11:04:14:423: " <>
74+
show (mkDateTime 2016 February 27 11 4 14 423)
75+
76+
let
77+
dateTimeValue :: DateTime
78+
dateTimeValue = unsafePartial $ fromJust $
79+
mkDateTime 2016 February 27 11 4 14 423
80+
81+
log $ "Original DateTime: " <> show dateTimeValue
82+
log $ "Add five seconds: " <> show (adjust (D.Seconds 5.0) dateTimeValue)
83+
log $ "Add two days: " <> show (adjust (D.Days 2.0) dateTimeValue)
84+
log $ "Add four hours: " <> show (adjust (D.Hours 4.0) dateTimeValue)
85+
86+
let
87+
oneDayDifference = unsafePartial $ fromJust $
88+
adjust (D.Days 1.0) dateTimeValue
89+
90+
result :: D.Days
91+
result = diff oneDayDifference dateTimeValue
92+
93+
log $ "Number of Days between two dateTimes: " <> show result
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>DateTimeBasicsLog</title>
7+
</head>
8+
9+
<body>
10+
<script src="./index.js"></script>
11+
</body>
12+
13+
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
require("../../../output/DateTimeBasicsLog.Main/index.js").main();

0 commit comments

Comments
 (0)