|
| 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 |
0 commit comments