Skip to content

Commit fbfea0f

Browse files
committed
Add more functions, include local date/time
1 parent 5065934 commit fbfea0f

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/Control/Monad/Eff/Now.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@
33
exports.now = function () {
44
return Date.now();
55
};
6+
7+
exports.nowOffset = function () {
8+
var dt = new Date();
9+
return dt.getTimezoneOffset();
10+
};

src/Control/Monad/Eff/Now.purs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,48 @@
1-
module Control.Monad.Eff.Now where
1+
module Control.Monad.Eff.Now
2+
( NOW
3+
, now
4+
, nowDateTime
5+
, nowDate
6+
, nowTime
7+
, locale
8+
) where
9+
10+
import Prelude
211

312
import Control.Monad.Eff (Eff)
4-
import Data.DateTime.Instant (Instant)
13+
14+
import Data.DateTime (time, date)
15+
import Data.DateTime.Instant (Instant, toDateTime)
16+
import Data.DateTime.Locale (LocalTime, LocalDate, LocalDateTime, LocalValue(..), Locale(..))
17+
import Data.Maybe (Maybe(..))
18+
import Data.Time.Duration (Minutes)
519

620
-- | Effect type for when accessing the current date/time.
721
foreign import data NOW :: !
822

9-
-- | Gets an `Instant` value corresponding to the current date/time according to
10-
-- | the current machine’s clock.
23+
-- | Gets an `Instant` value for the date and time according to the current
24+
-- | machine’s clock.
1125
foreign import now :: forall e. Eff (now :: NOW | e) Instant
26+
27+
-- | Gets a `DateTime` value for the date and time according to the current
28+
-- | machine’s clock.
29+
nowDateTime :: forall e. Eff (now :: NOW | e) LocalDateTime
30+
nowDateTime = LocalValue <$> locale <*> (toDateTime <$> now)
31+
32+
-- | Gets the date according to the current machine’s clock.
33+
nowDate :: forall e. Eff (now :: NOW | e) LocalDate
34+
nowDate = LocalValue <$> locale <*> (date <<< toDateTime <$> now)
35+
36+
-- | Gets the time according to the current machine’s clock.
37+
nowTime :: forall e. Eff (now :: NOW | e) LocalTime
38+
nowTime = LocalValue <$> locale <*> (time <<< toDateTime <$> now)
39+
40+
-- | Gets the locale according to the current machine’s clock.
41+
-- |
42+
-- | **Note**: The `LocaleName` will always be empty for the `Locale` value
43+
-- | returned here until there is a reliable way to detect a name for the
44+
-- | locale.
45+
locale :: forall e. Eff (now :: NOW | e) Locale
46+
locale = Locale Nothing <$> nowOffset
47+
48+
foreign import nowOffset :: forall e. Eff (now :: NOW | e) Minutes

0 commit comments

Comments
 (0)