|
| 1 | +-- | A module providing a type and operations for the native JavaScript `Date` |
| 2 | +-- | object. |
| 3 | +-- | |
| 4 | +-- | The `JSDate` type and associated functions are provided for interop |
| 5 | +-- | purposes with JavaScript, but for working with dates in PureScript it is |
| 6 | +-- | recommended that `DateTime` representation is used instead - `DateTime` |
| 7 | +-- | offers greater type safety, a more PureScript-friendly interface, and has |
| 8 | +-- | a `Generic` instance. |
| 9 | +module Data.JSDate where |
| 10 | + |
| 11 | +import Prelude |
| 12 | + |
| 13 | +import Control.Monad.Eff (Eff) |
| 14 | +import Control.Monad.Eff.Exception (EXCEPTION) |
| 15 | + |
| 16 | +import Data.Date as Date |
| 17 | +import Data.DateTime (DateTime(..), Date) |
| 18 | +import Data.DateTime as DateTime |
| 19 | +import Data.DateTime.Instant (Instant) |
| 20 | +import Data.DateTime.Instant as Instant |
| 21 | +import Data.Enum (fromEnum) |
| 22 | +import Data.Function.Uncurried (Fn2, runFn2) |
| 23 | +import Data.Int (toNumber) |
| 24 | +import Data.Maybe (Maybe(..)) |
| 25 | +import Data.Time as Time |
| 26 | +import Data.Time.Duration (Milliseconds(..)) |
| 27 | + |
| 28 | +-- | The type of JavaScript `Date` objects. |
| 29 | +foreign import data JSDate :: * |
| 30 | + |
| 31 | +-- | Checks whether a date value is valid. When a date is invalid, the majority |
| 32 | +-- | of the functions return `NaN`, `"Invalid Date"`, or throw an exception. |
| 33 | +foreign import isValid :: JSDate -> Boolean |
| 34 | + |
| 35 | +-- | Converts a `DateTime` value into a native JavaScript date object. The |
| 36 | +-- | resulting date value is guaranteed to be valid. |
| 37 | +fromDateTime :: DateTime -> JSDate |
| 38 | +fromDateTime (DateTime d t) = jsdate |
| 39 | + { year: toNumber (fromEnum (Date.year d)) |
| 40 | + , month: toNumber (fromEnum (Date.month d) - 1) |
| 41 | + , day: toNumber (fromEnum (Date.day d)) |
| 42 | + , hour: toNumber (fromEnum (Time.hour t)) |
| 43 | + , minute: toNumber (fromEnum (Time.minute t)) |
| 44 | + , second: toNumber (fromEnum (Time.second t)) |
| 45 | + , millisecond: toNumber (fromEnum (Time.millisecond t)) |
| 46 | + } |
| 47 | + |
| 48 | +-- | Attempts to construct a `DateTime` value for a `JSDate`. `Nothing` is |
| 49 | +-- | returned only when the date value is an invalid date. |
| 50 | +toDateTime :: JSDate -> Maybe DateTime |
| 51 | +toDateTime = map Instant.toDateTime <$> toInstant |
| 52 | + |
| 53 | +-- | Attempts to construct a `Date` value for a `JSDate`, ignoring any time |
| 54 | +-- | component of the `JSDate`. `Nothing` is returned only when the date value |
| 55 | +-- | is an invalid date. |
| 56 | +toDate :: JSDate -> Maybe Date |
| 57 | +toDate = map DateTime.date <$> toDateTime |
| 58 | + |
| 59 | +-- | Attempts to construct an `Instant` for a `JSDate`. `Nothing` is returned |
| 60 | +-- | only when the date value is an invalid date. |
| 61 | +toInstant :: JSDate -> Maybe Instant |
| 62 | +toInstant = Instant.instant <<< Milliseconds <=< toInstantImpl Just Nothing |
| 63 | + |
| 64 | +foreign import toInstantImpl |
| 65 | + :: (forall a. a -> Maybe a) |
| 66 | + -> (forall a. Maybe a) |
| 67 | + -> JSDate |
| 68 | + -> Maybe Number |
| 69 | + |
| 70 | +-- | Constructs a new `JSDate` from UTC component values. If any of the values |
| 71 | +-- | are `NaN` the resulting date will be invalid. |
| 72 | +foreign import jsdate |
| 73 | + :: { year :: Number |
| 74 | + , month :: Number |
| 75 | + , day :: Number |
| 76 | + , hour :: Number |
| 77 | + , minute :: Number |
| 78 | + , second :: Number |
| 79 | + , millisecond :: Number |
| 80 | + } |
| 81 | + -> JSDate |
| 82 | + |
| 83 | +-- | Constructs a new `JSDate` from component values using the current machine's |
| 84 | +-- | locale. If any of the values are `NaN` the resulting date will be invalid. |
| 85 | +foreign import jsdateLocal |
| 86 | + :: forall eff |
| 87 | + . { year :: Number |
| 88 | + , month :: Number |
| 89 | + , day :: Number |
| 90 | + , hour :: Number |
| 91 | + , minute :: Number |
| 92 | + , second :: Number |
| 93 | + , millisecond :: Number |
| 94 | + } |
| 95 | + -> Eff (locale :: LOCALE | eff) JSDate |
| 96 | + |
| 97 | +foreign import dateMethodEff :: forall eff a. Fn2 String JSDate (Eff eff a) |
| 98 | +foreign import dateMethod :: forall a. Fn2 String JSDate a |
| 99 | + |
| 100 | +-- | The effect type used when indicating the current machine's date/time locale |
| 101 | +-- | is used in computing a value. |
| 102 | +foreign import data LOCALE :: ! |
| 103 | + |
| 104 | +-- | Returns the date as a number of milliseconds since 1970-01-01 00:00:00 UTC. |
| 105 | +getTime :: JSDate -> Number |
| 106 | +getTime dt = runFn2 dateMethod "getTime" dt |
| 107 | + |
| 108 | +-- | Returns the day of the month for a date, according to UTC. |
| 109 | +getUTCDate :: JSDate -> Number |
| 110 | +getUTCDate dt = runFn2 dateMethod "getUTCDate" dt |
| 111 | + |
| 112 | +-- | Returns the day of the week for a date, according to UTC. |
| 113 | +getUTCDay :: JSDate -> Number |
| 114 | +getUTCDay dt = runFn2 dateMethod "getUTCDay" dt |
| 115 | + |
| 116 | +-- | Returns the year for a date, according to UTC. |
| 117 | +getUTCFullYear :: JSDate -> Number |
| 118 | +getUTCFullYear dt = runFn2 dateMethod "getUTCFullYear" dt |
| 119 | + |
| 120 | +-- | Returns the hours for a date, according to UTC. |
| 121 | +getUTCHours :: JSDate -> Number |
| 122 | +getUTCHours dt = runFn2 dateMethod "getUTCHours" dt |
| 123 | + |
| 124 | +-- | Returns the milliseconds for a date, according to UTC. |
| 125 | +getUTCMilliseconds :: JSDate -> Number |
| 126 | +getUTCMilliseconds dt = runFn2 dateMethod "getUTCMilliseconds" dt |
| 127 | + |
| 128 | +-- | Returns the minutes for a date, according to UTC. |
| 129 | +getUTCMinutes :: JSDate -> Number |
| 130 | +getUTCMinutes dt = runFn2 dateMethod "getUTCMinutes" dt |
| 131 | + |
| 132 | +-- | Returns the month for a date, according to UTC. |
| 133 | +getUTCMonth :: JSDate -> Number |
| 134 | +getUTCMonth dt = runFn2 dateMethod "getUTCMonth" dt |
| 135 | + |
| 136 | +-- | Returns the seconds for a date, according to UTC. |
| 137 | +getUTCSeconds :: JSDate -> Number |
| 138 | +getUTCSeconds dt = runFn2 dateMethod "getUTCSeconds" dt |
| 139 | + |
| 140 | +-- | Returns the day of the month for a date, according to the current |
| 141 | +-- | machine's date/time locale. |
| 142 | +getDate :: forall eff. JSDate -> Eff (locale :: LOCALE | eff) Number |
| 143 | +getDate dt = runFn2 dateMethodEff "getDate" dt |
| 144 | + |
| 145 | +-- | Returns the day of the week for a date, according to the current |
| 146 | +-- | machine's date/time locale. |
| 147 | +getDay :: forall eff. JSDate -> Eff (locale :: LOCALE | eff) Number |
| 148 | +getDay dt = runFn2 dateMethodEff "getDay" dt |
| 149 | + |
| 150 | +-- | Returns the year for a date, according to the current machine's date/time |
| 151 | +-- | locale. |
| 152 | +getFullYear :: forall eff. JSDate -> Eff (locale :: LOCALE | eff) Number |
| 153 | +getFullYear dt = runFn2 dateMethodEff "getFullYear" dt |
| 154 | + |
| 155 | +-- | Returns the hour for a date, according to the current machine's date/time |
| 156 | +-- | locale. |
| 157 | +getHours :: forall eff. JSDate -> Eff (locale :: LOCALE | eff) Number |
| 158 | +getHours dt = runFn2 dateMethodEff "getHours" dt |
| 159 | + |
| 160 | +-- | Returns the milliseconds for a date, according to the current machine's |
| 161 | +-- | date/time locale. |
| 162 | +getMilliseconds :: forall eff. JSDate -> Eff (locale :: LOCALE | eff) Number |
| 163 | +getMilliseconds dt = runFn2 dateMethodEff "getMilliseconds" dt |
| 164 | + |
| 165 | +-- | Returns the minutes for a date, according to the current machine's |
| 166 | +-- | date/time locale. |
| 167 | +getMinutes :: forall eff. JSDate -> Eff (locale :: LOCALE | eff) Number |
| 168 | +getMinutes dt = runFn2 dateMethodEff "getMinutes" dt |
| 169 | + |
| 170 | +-- | Returns the month for a date, according to the current machine's |
| 171 | +-- | date/time locale. |
| 172 | +getMonth :: forall eff. JSDate -> Eff (locale :: LOCALE | eff) Number |
| 173 | +getMonth dt = runFn2 dateMethodEff "getMonth" dt |
| 174 | + |
| 175 | +-- | Returns the seconds for a date, according to the current machine's |
| 176 | +-- | date/time locale. |
| 177 | +getSeconds :: forall eff. JSDate -> Eff (locale :: LOCALE | eff) Number |
| 178 | +getSeconds dt = runFn2 dateMethodEff "getSeconds" dt |
| 179 | + |
| 180 | +-- | Returns the time-zone offset for a date, according to the current machine's |
| 181 | +-- | date/time locale. |
| 182 | +getTimezoneOffset :: forall eff. JSDate -> Eff (locale :: LOCALE | eff) Number |
| 183 | +getTimezoneOffset dt = runFn2 dateMethodEff "getTimezoneOffset" dt |
| 184 | + |
| 185 | +-- | Returns the date portion of a date value as a human-readable string. |
| 186 | +toDateString :: JSDate -> String |
| 187 | +toDateString dt = runFn2 dateMethod "toDateString" dt |
| 188 | + |
| 189 | +-- | Converts a date value to an ISO 8601 Extended format date string. |
| 190 | +toISOString :: forall eff. JSDate -> Eff (exception :: EXCEPTION | eff) String |
| 191 | +toISOString dt = runFn2 dateMethodEff "toISOString" dt |
| 192 | + |
| 193 | +-- | Returns a string representing for a date value. |
| 194 | +toString :: JSDate -> String |
| 195 | +toString dt = runFn2 dateMethod "toString" dt |
| 196 | + |
| 197 | +-- | Returns the time portion of a date value as a human-readable string. |
| 198 | +toTimeString :: JSDate -> String |
| 199 | +toTimeString dt = runFn2 dateMethod "toTimeString" dt |
| 200 | + |
| 201 | +-- | Returns the date as a string using the UTC timezone. |
| 202 | +toUTCString :: JSDate -> String |
| 203 | +toUTCString dt = runFn2 dateMethod "toUTCString" dt |
0 commit comments