-----------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Miso.Date
-- Copyright   :  (C) 2016-2026 David M. Johnson
-- License     :  BSD3-style (see the file LICENSE)
-- Maintainer  :  David M. Johnson <code@dmj.io>
-- Stability   :  experimental
-- Portability :  non-portable
--
-- = Overview
--
-- "Miso.Date" is a Haskell wrapper around the JavaScript
-- <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date Date>
-- object. A 'Date' value lives in JavaScript memory and represents a single
-- point in time. All operations run in 'IO' and call through to the
-- underlying JS object.
--
-- Import qualified to avoid clashing with 'Prelude':
--
-- @
-- import qualified "Miso.Date" as D
-- @
--
-- = Quick start
--
-- @
-- import qualified "Miso.Date" as D
--
-- example :: IO ()
-- example = do
--   now  <- D.'new'
--   iso  <- D.'toISOString' now     -- e.g. \"2026-06-23T12:00:00.000Z\"
--   year <- D.'getFullYear' now     -- e.g. 2026
--   ms   <- D.'valueOf' now         -- milliseconds since Unix epoch
--   pure ()
-- @
--
-- = API groups
--
-- * __Construction__: 'new'
-- * __Conversion__ (strings): 'toDateString', 'toISOString', 'toJSON',
--   'toLocaleDateString', 'toLocaleString', 'toLocaleTimeString',
--   'toString', 'toTimeString', 'toUTCString'
-- * __Conversion__ (numeric): 'valueOf' (ms since epoch)
-- * __Local getters__: 'getDate', 'getDay', 'getFullYear', 'getHours',
--   'getMilliseconds', 'getMinutes', 'getMonth', 'getSeconds',
--   'getTime', 'getTimezoneOffset'
-- * __UTC getters__: 'getUTCDate', 'getUTCDay', 'getUTCFullYear',
--   'getUTCHours', 'getUTCMilliseconds', 'getUTCMinutes',
--   'getUTCMonth', 'getUTCSeconds'
-- * __Local setters__: 'setDate', 'setFullYear', 'setHours',
--   'setMilliseconds', 'setMinutes', 'setMonth', 'setSeconds', 'setTime'
-- * __UTC setters__: 'setUTCDate', 'setUTCFullYear', 'setUTCHours',
--   'setUTCMilliseconds', 'setUTCMinutes', 'setUTCMonth', 'setUTCSeconds'
--
-- __Note__: JavaScript months are __0-indexed__ (January = 0, December = 11).
-- All getter and setter functions in this module follow that convention.
-- Setter functions return the new timestamp as milliseconds since the Unix
-- epoch (a 'Double'), mirroring the JavaScript return value.
--
-- = See also
--
-- * "Miso.DSL" — 'Miso.DSL.JSVal' and marshaling used internally
-----------------------------------------------------------------------------
module Miso.Date
  ( -- * Type
    Date
    -- * Construction
  , new
    -- * Conversion
  , toDateString
  , toISOString
  , toJSON
  , toLocaleDateString
  , toLocaleString
  , toLocaleTimeString
  , toString
  , toTimeString
  , toUTCString
  , valueOf
    -- * Getters
  , getDate
  , getDay
  , getFullYear
  , getHours
  , getMilliseconds
  , getMinutes
  , getMonth
  , getSeconds
  , getTime
  , getTimezoneOffset
  , getUTCDate
  , getUTCDay
  , getUTCFullYear
  , getUTCHours
  , getUTCMilliseconds
  , getUTCMinutes
  , getUTCMonth
  , getUTCSeconds
    -- * Setters
  , setDate
  , setFullYear
  , setHours
  , setMilliseconds
  , setMinutes
  , setMonth
  , setSeconds
  , setTime
  , setUTCDate
  , setUTCFullYear
  , setUTCHours
  , setUTCMilliseconds
  , setUTCMinutes
  , setUTCMonth
  , setUTCSeconds
  ) where
-----------------------------------------------------------------------------
import           Data.Maybe (catMaybes)
-----------------------------------------------------------------------------
import           Miso.DSL (jsg, JSVal, ToJSVal, FromJSVal, ToObject)
import qualified Miso.DSL as DSL
import           Miso.FFI (callFunction)
import           Miso.String (MisoString)
-----------------------------------------------------------------------------
newtype Date = Date JSVal deriving (JSVal -> IO (Maybe Date)
JSVal -> IO Date
(JSVal -> IO (Maybe Date)) -> (JSVal -> IO Date) -> FromJSVal Date
forall a. (JSVal -> IO (Maybe a)) -> (JSVal -> IO a) -> FromJSVal a
$cfromJSVal :: JSVal -> IO (Maybe Date)
fromJSVal :: JSVal -> IO (Maybe Date)
$cfromJSValUnchecked :: JSVal -> IO Date
fromJSValUnchecked :: JSVal -> IO Date
FromJSVal, Date -> IO JSVal
(Date -> IO JSVal) -> ToJSVal Date
forall a. (a -> IO JSVal) -> ToJSVal a
$ctoJSVal :: Date -> IO JSVal
toJSVal :: Date -> IO JSVal
ToJSVal, Date -> IO Object
(Date -> IO Object) -> ToObject Date
forall a. (a -> IO Object) -> ToObject a
$ctoObject :: Date -> IO Object
toObject :: Date -> IO Object
ToObject, Date -> Date -> Bool
(Date -> Date -> Bool) -> (Date -> Date -> Bool) -> Eq Date
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Date -> Date -> Bool
== :: Date -> Date -> Bool
$c/= :: Date -> Date -> Bool
/= :: Date -> Date -> Bool
Eq)
-----------------------------------------------------------------------------
-- | Constructs a new JS [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) in t'IO'.
--
new :: IO Date
new :: IO Date
new = JSVal -> Date
Date (JSVal -> Date) -> IO JSVal -> IO Date
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO JSVal -> [JSVal] -> IO JSVal
forall constructor args.
(ToObject constructor, ToArgs args) =>
constructor -> args -> IO JSVal
DSL.new (MisoString -> IO JSVal
jsg MisoString
"Date") ([] :: [JSVal])
-----------------------------------------------------------------------------
call0 :: FromJSVal a => Date -> MisoString -> IO a
call0 :: forall a. FromJSVal a => Date -> MisoString -> IO a
call0 (Date JSVal
d) MisoString
name = JSVal -> IO a
forall a. FromJSVal a => JSVal -> IO a
DSL.fromJSValUnchecked (JSVal -> IO a) -> IO JSVal -> IO a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< JSVal -> MisoString -> [JSVal] -> IO JSVal
forall args. ToArgs args => JSVal -> MisoString -> args -> IO JSVal
callFunction JSVal
d MisoString
name ([] :: [JSVal])
-----------------------------------------------------------------------------
callArgs :: FromJSVal a => Date -> MisoString -> [JSVal] -> IO a
callArgs :: forall a. FromJSVal a => Date -> MisoString -> [JSVal] -> IO a
callArgs (Date JSVal
d) MisoString
name [JSVal]
args = JSVal -> IO a
forall a. FromJSVal a => JSVal -> IO a
DSL.fromJSValUnchecked (JSVal -> IO a) -> IO JSVal -> IO a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< JSVal -> MisoString -> [JSVal] -> IO JSVal
forall args. ToArgs args => JSVal -> MisoString -> args -> IO JSVal
callFunction JSVal
d MisoString
name [JSVal]
args
-----------------------------------------------------------------------------
-- | Returns a human-readable date string.
--
-- <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString>
--
toDateString :: Date -> IO MisoString
toDateString :: Date -> IO MisoString
toDateString Date
date = Date -> MisoString -> IO MisoString
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"toDateString"
-----------------------------------------------------------------------------
-- | Returns an ISO 8601 string.
--
-- <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString>
--
toISOString :: Date -> IO MisoString
toISOString :: Date -> IO MisoString
toISOString Date
date = Date -> MisoString -> IO MisoString
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"toISOString"
-----------------------------------------------------------------------------
-- | Returns the JSON representation of the date.
--
-- <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON>
--
toJSON :: Date -> IO MisoString
toJSON :: Date -> IO MisoString
toJSON Date
date = Date -> MisoString -> IO MisoString
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"toJSON"
-----------------------------------------------------------------------------
-- | Returns a locale-sensitive date string.
--
-- <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString>
--
toLocaleDateString :: Date -> IO MisoString
toLocaleDateString :: Date -> IO MisoString
toLocaleDateString Date
date = Date -> MisoString -> IO MisoString
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"toLocaleDateString"
-----------------------------------------------------------------------------
-- | Returns a locale-sensitive date and time string.
--
-- <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString>
--
toLocaleString :: Date -> IO MisoString
toLocaleString :: Date -> IO MisoString
toLocaleString Date
date = Date -> MisoString -> IO MisoString
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"toLocaleString"
-----------------------------------------------------------------------------
-- | Returns a locale-sensitive time string.
--
-- <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString>
--
toLocaleTimeString :: Date -> IO MisoString
toLocaleTimeString :: Date -> IO MisoString
toLocaleTimeString Date
date = Date -> MisoString -> IO MisoString
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"toLocaleTimeString"
-----------------------------------------------------------------------------
-- | Returns the full date string.
--
-- <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString>
--
toString :: Date -> IO MisoString
toString :: Date -> IO MisoString
toString Date
date = Date -> MisoString -> IO MisoString
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"toString"
-----------------------------------------------------------------------------
-- | Returns a time string.
--
-- <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toTimeString>
--
toTimeString :: Date -> IO MisoString
toTimeString :: Date -> IO MisoString
toTimeString Date
date = Date -> MisoString -> IO MisoString
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"toTimeString"
-----------------------------------------------------------------------------
-- | Returns a UTC string.
--
-- <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toUTCString>
--
toUTCString :: Date -> IO MisoString
toUTCString :: Date -> IO MisoString
toUTCString Date
date = Date -> MisoString -> IO MisoString
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"toUTCString"
-----------------------------------------------------------------------------
-- | Returns the primitive value (milliseconds since epoch).
--
-- <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf>
--
valueOf :: Date -> IO Double
valueOf :: Date -> IO Double
valueOf Date
date = Date -> MisoString -> IO Double
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"valueOf"
-----------------------------------------------------------------------------
-- | Returns the day of the month.
--
getDate :: Date -> IO Int
getDate :: Date -> IO Int
getDate Date
date = Date -> MisoString -> IO Int
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"getDate"
-----------------------------------------------------------------------------
-- | Returns the day of the week.
--
getDay :: Date -> IO Int
getDay :: Date -> IO Int
getDay Date
date = Date -> MisoString -> IO Int
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"getDay"
-----------------------------------------------------------------------------
-- | Returns the full year.
--
getFullYear :: Date -> IO Int
getFullYear :: Date -> IO Int
getFullYear Date
date = Date -> MisoString -> IO Int
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"getFullYear"
-----------------------------------------------------------------------------
-- | Returns the hour.
--
getHours :: Date -> IO Int
getHours :: Date -> IO Int
getHours Date
date = Date -> MisoString -> IO Int
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"getHours"
-----------------------------------------------------------------------------
-- | Returns the milliseconds.
--
getMilliseconds :: Date -> IO Int
getMilliseconds :: Date -> IO Int
getMilliseconds Date
date = Date -> MisoString -> IO Int
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"getMilliseconds"
-----------------------------------------------------------------------------
-- | Returns the minutes.
--
getMinutes :: Date -> IO Int
getMinutes :: Date -> IO Int
getMinutes Date
date = Date -> MisoString -> IO Int
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"getMinutes"
-----------------------------------------------------------------------------
-- | Returns the month (0-11).
--
getMonth :: Date -> IO Int
getMonth :: Date -> IO Int
getMonth Date
date = Date -> MisoString -> IO Int
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"getMonth"
-----------------------------------------------------------------------------
-- | Returns the seconds.
--
getSeconds :: Date -> IO Int
getSeconds :: Date -> IO Int
getSeconds Date
date = Date -> MisoString -> IO Int
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"getSeconds"
-----------------------------------------------------------------------------
-- | Returns milliseconds since epoch.
--
getTime :: Date -> IO Double
getTime :: Date -> IO Double
getTime Date
date = Date -> MisoString -> IO Double
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"getTime"
-----------------------------------------------------------------------------
-- | Returns the time zone offset in minutes.
--
getTimezoneOffset :: Date -> IO Int
getTimezoneOffset :: Date -> IO Int
getTimezoneOffset Date
date = Date -> MisoString -> IO Int
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"getTimezoneOffset"
-----------------------------------------------------------------------------
-- | Returns the UTC day of the month.
--
getUTCDate :: Date -> IO Int
getUTCDate :: Date -> IO Int
getUTCDate Date
date = Date -> MisoString -> IO Int
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"getUTCDate"
-----------------------------------------------------------------------------
-- | Returns the UTC day of the week.
--
getUTCDay :: Date -> IO Int
getUTCDay :: Date -> IO Int
getUTCDay Date
date = Date -> MisoString -> IO Int
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"getUTCDay"
-----------------------------------------------------------------------------
-- | Returns the UTC full year.
--
getUTCFullYear :: Date -> IO Int
getUTCFullYear :: Date -> IO Int
getUTCFullYear Date
date = Date -> MisoString -> IO Int
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"getUTCFullYear"
-----------------------------------------------------------------------------
-- | Returns the UTC hour.
--
getUTCHours :: Date -> IO Int
getUTCHours :: Date -> IO Int
getUTCHours Date
date = Date -> MisoString -> IO Int
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"getUTCHours"
-----------------------------------------------------------------------------
-- | Returns the UTC milliseconds.
--
getUTCMilliseconds :: Date -> IO Int
getUTCMilliseconds :: Date -> IO Int
getUTCMilliseconds Date
date = Date -> MisoString -> IO Int
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"getUTCMilliseconds"
-----------------------------------------------------------------------------
-- | Returns the UTC minutes.
--
getUTCMinutes :: Date -> IO Int
getUTCMinutes :: Date -> IO Int
getUTCMinutes Date
date = Date -> MisoString -> IO Int
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"getUTCMinutes"
-----------------------------------------------------------------------------
-- | Returns the UTC month (0-11).
--
getUTCMonth :: Date -> IO Int
getUTCMonth :: Date -> IO Int
getUTCMonth Date
date = Date -> MisoString -> IO Int
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"getUTCMonth"
-----------------------------------------------------------------------------
-- | Returns the UTC seconds.
--
getUTCSeconds :: Date -> IO Int
getUTCSeconds :: Date -> IO Int
getUTCSeconds Date
date = Date -> MisoString -> IO Int
forall a. FromJSVal a => Date -> MisoString -> IO a
call0 Date
date MisoString
"getUTCSeconds"
-----------------------------------------------------------------------------
-- | Sets the day of the month.
--
setDate
  :: Int
  -- ^ Day of the month (1–31)
  -> Date
  -- ^ Date object to modify
  -> IO Double
setDate :: Int -> Date -> IO Double
setDate Int
day (Date JSVal
d) = JSVal -> IO Double
forall a. FromJSVal a => JSVal -> IO a
DSL.fromJSValUnchecked (JSVal -> IO Double) -> IO JSVal -> IO Double
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< JSVal -> MisoString -> [Int] -> IO JSVal
forall args. ToArgs args => JSVal -> MisoString -> args -> IO JSVal
callFunction JSVal
d MisoString
"setDate" [Int
day]
-----------------------------------------------------------------------------
-- | Sets the full year, with optional month and day.
--
setFullYear
  :: Int
  -- ^ Full year (e.g. 2026)
  -> Maybe Int
  -- ^ Optional month (0-indexed: 0 = January)
  -> Maybe Int
  -- ^ Optional day of the month (1–31)
  -> Date
  -- ^ Date object to modify
  -> IO Double
setFullYear :: Int -> Maybe Int -> Maybe Int -> Date -> IO Double
setFullYear Int
year Maybe Int
month Maybe Int
day (Date JSVal
d) = do
  y <- Int -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
DSL.toJSVal Int
year
  m <- traverse DSL.toJSVal month
  d' <- traverse DSL.toJSVal day
  callArgs (Date d) "setFullYear" (catMaybes [Just y, m, d'])
-----------------------------------------------------------------------------
-- | Sets the hour, with optional minutes, seconds, and milliseconds.
--
setHours
  :: Int
  -- ^ Hour (0–23)
  -> Maybe Int
  -- ^ Optional minutes (0–59)
  -> Maybe Int
  -- ^ Optional seconds (0–59)
  -> Maybe Int
  -- ^ Optional milliseconds (0–999)
  -> Date
  -- ^ Date object to modify
  -> IO Double
setHours :: Int -> Maybe Int -> Maybe Int -> Maybe Int -> Date -> IO Double
setHours Int
hours Maybe Int
minutes Maybe Int
seconds Maybe Int
millis (Date JSVal
d) = do
  h <- Int -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
DSL.toJSVal Int
hours
  m <- traverse DSL.toJSVal minutes
  s <- traverse DSL.toJSVal seconds
  ms <- traverse DSL.toJSVal millis
  callArgs (Date d) "setHours" (catMaybes [Just h, m, s, ms])
-----------------------------------------------------------------------------
-- | Sets the milliseconds.
--
setMilliseconds
  :: Int
  -- ^ Milliseconds (0–999)
  -> Date
  -- ^ Date object to modify
  -> IO Double
setMilliseconds :: Int -> Date -> IO Double
setMilliseconds Int
ms (Date JSVal
d) = JSVal -> IO Double
forall a. FromJSVal a => JSVal -> IO a
DSL.fromJSValUnchecked (JSVal -> IO Double) -> IO JSVal -> IO Double
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< JSVal -> MisoString -> [Int] -> IO JSVal
forall args. ToArgs args => JSVal -> MisoString -> args -> IO JSVal
callFunction JSVal
d MisoString
"setMilliseconds" [Int
ms]
-----------------------------------------------------------------------------
-- | Sets the minutes, with optional seconds and milliseconds.
--
setMinutes
  :: Int
  -- ^ Minutes (0–59)
  -> Maybe Int
  -- ^ Optional seconds (0–59)
  -> Maybe Int
  -- ^ Optional milliseconds (0–999)
  -> Date
  -- ^ Date object to modify
  -> IO Double
setMinutes :: Int -> Maybe Int -> Maybe Int -> Date -> IO Double
setMinutes Int
minutes Maybe Int
seconds Maybe Int
millis (Date JSVal
d) = do
  m <- Int -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
DSL.toJSVal Int
minutes
  s <- traverse DSL.toJSVal seconds
  ms <- traverse DSL.toJSVal millis
  callArgs (Date d) "setMinutes" (catMaybes [Just m, s, ms])
-----------------------------------------------------------------------------
-- | Sets the month, with optional day of the month.
--
setMonth
  :: Int
  -- ^ Month (0-indexed: 0 = January, 11 = December)
  -> Maybe Int
  -- ^ Optional day of the month (1–31)
  -> Date
  -- ^ Date object to modify
  -> IO Double
setMonth :: Int -> Maybe Int -> Date -> IO Double
setMonth Int
month Maybe Int
day (Date JSVal
d) = do
  m <- Int -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
DSL.toJSVal Int
month
  d' <- traverse DSL.toJSVal day
  callArgs (Date d) "setMonth" (catMaybes [Just m, d'])
-----------------------------------------------------------------------------
-- | Sets the seconds, with optional milliseconds.
--
setSeconds
  :: Int
  -- ^ Seconds (0–59)
  -> Maybe Int
  -- ^ Optional milliseconds (0–999)
  -> Date
  -- ^ Date object to modify
  -> IO Double
setSeconds :: Int -> Maybe Int -> Date -> IO Double
setSeconds Int
seconds Maybe Int
millis (Date JSVal
d) = do
  s <- Int -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
DSL.toJSVal Int
seconds
  ms <- traverse DSL.toJSVal millis
  callArgs (Date d) "setSeconds" (catMaybes [Just s, ms])
-----------------------------------------------------------------------------
-- | Sets the time in milliseconds since epoch.
--
setTime
  :: Double
  -- ^ Milliseconds since the Unix epoch (1 January 1970 00:00:00 UTC)
  -> Date
  -- ^ Date object to modify
  -> IO Double
setTime :: Double -> Date -> IO Double
setTime Double
time (Date JSVal
d) = JSVal -> IO Double
forall a. FromJSVal a => JSVal -> IO a
DSL.fromJSValUnchecked (JSVal -> IO Double) -> IO JSVal -> IO Double
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< JSVal -> MisoString -> [Double] -> IO JSVal
forall args. ToArgs args => JSVal -> MisoString -> args -> IO JSVal
callFunction JSVal
d MisoString
"setTime" [Double
time]
-----------------------------------------------------------------------------
-- | Sets the UTC day of the month.
--
setUTCDate
  :: Int
  -- ^ Day of the month in UTC (1–31)
  -> Date
  -- ^ Date object to modify
  -> IO Double
setUTCDate :: Int -> Date -> IO Double
setUTCDate Int
day (Date JSVal
d) = JSVal -> IO Double
forall a. FromJSVal a => JSVal -> IO a
DSL.fromJSValUnchecked (JSVal -> IO Double) -> IO JSVal -> IO Double
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< JSVal -> MisoString -> [Int] -> IO JSVal
forall args. ToArgs args => JSVal -> MisoString -> args -> IO JSVal
callFunction JSVal
d MisoString
"setUTCDate" [Int
day]
-----------------------------------------------------------------------------
-- | Sets the UTC full year, with optional month and day.
--
setUTCFullYear
  :: Int
  -- ^ Full year in UTC (e.g. 2026)
  -> Maybe Int
  -- ^ Optional month in UTC (0-indexed)
  -> Maybe Int
  -- ^ Optional day in UTC (1–31)
  -> Date
  -- ^ Date object to modify
  -> IO Double
setUTCFullYear :: Int -> Maybe Int -> Maybe Int -> Date -> IO Double
setUTCFullYear Int
year Maybe Int
month Maybe Int
day (Date JSVal
d) = do
  y <- Int -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
DSL.toJSVal Int
year
  m <- traverse DSL.toJSVal month
  d' <- traverse DSL.toJSVal day
  callArgs (Date d) "setUTCFullYear" (catMaybes [Just y, m, d'])
-----------------------------------------------------------------------------
-- | Sets the UTC hour, with optional minutes, seconds, and milliseconds.
--
setUTCHours
  :: Int
  -- ^ Hour in UTC (0–23)
  -> Maybe Int
  -- ^ Optional minutes (0–59)
  -> Maybe Int
  -- ^ Optional seconds (0–59)
  -> Maybe Int
  -- ^ Optional milliseconds (0–999)
  -> Date
  -- ^ Date object to modify
  -> IO Double
setUTCHours :: Int -> Maybe Int -> Maybe Int -> Maybe Int -> Date -> IO Double
setUTCHours Int
hours Maybe Int
minutes Maybe Int
seconds Maybe Int
millis (Date JSVal
d) = do
  h <- Int -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
DSL.toJSVal Int
hours
  m <- traverse DSL.toJSVal minutes
  s <- traverse DSL.toJSVal seconds
  ms <- traverse DSL.toJSVal millis
  callArgs (Date d) "setUTCHours" (catMaybes [Just h, m, s, ms])
-----------------------------------------------------------------------------
-- | Sets the UTC milliseconds.
--
setUTCMilliseconds
  :: Int
  -- ^ Milliseconds in UTC (0–999)
  -> Date
  -- ^ Date object to modify
  -> IO Double
setUTCMilliseconds :: Int -> Date -> IO Double
setUTCMilliseconds Int
ms (Date JSVal
d) = JSVal -> IO Double
forall a. FromJSVal a => JSVal -> IO a
DSL.fromJSValUnchecked (JSVal -> IO Double) -> IO JSVal -> IO Double
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< JSVal -> MisoString -> [Int] -> IO JSVal
forall args. ToArgs args => JSVal -> MisoString -> args -> IO JSVal
callFunction JSVal
d MisoString
"setUTCMilliseconds" [Int
ms]
-----------------------------------------------------------------------------
-- | Sets the UTC minutes, with optional seconds and milliseconds.
--
setUTCMinutes
  :: Int
  -- ^ Minutes in UTC (0–59)
  -> Maybe Int
  -- ^ Optional seconds (0–59)
  -> Maybe Int
  -- ^ Optional milliseconds (0–999)
  -> Date
  -- ^ Date object to modify
  -> IO Double
setUTCMinutes :: Int -> Maybe Int -> Maybe Int -> Date -> IO Double
setUTCMinutes Int
minutes Maybe Int
seconds Maybe Int
millis (Date JSVal
d) = do
  m <- Int -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
DSL.toJSVal Int
minutes
  s <- traverse DSL.toJSVal seconds
  ms <- traverse DSL.toJSVal millis
  callArgs (Date d) "setUTCMinutes" (catMaybes [Just m, s, ms])
-----------------------------------------------------------------------------
-- | Sets the UTC month, with optional day of the month.
--
setUTCMonth
  :: Int
  -- ^ Month in UTC (0-indexed: 0 = January)
  -> Maybe Int
  -- ^ Optional day in UTC (1–31)
  -> Date
  -- ^ Date object to modify
  -> IO Double
setUTCMonth :: Int -> Maybe Int -> Date -> IO Double
setUTCMonth Int
month Maybe Int
day (Date JSVal
d) = do
  m <- Int -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
DSL.toJSVal Int
month
  d' <- traverse DSL.toJSVal day
  callArgs (Date d) "setUTCMonth" (catMaybes [Just m, d'])
-----------------------------------------------------------------------------
-- | Sets the UTC seconds, with optional milliseconds.
--
setUTCSeconds
  :: Int
  -- ^ Seconds in UTC (0–59)
  -> Maybe Int
  -- ^ Optional milliseconds (0–999)
  -> Date
  -- ^ Date object to modify
  -> IO Double
setUTCSeconds :: Int -> Maybe Int -> Date -> IO Double
setUTCSeconds Int
seconds Maybe Int
millis (Date JSVal
d) = do
  s <- Int -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
DSL.toJSVal Int
seconds
  ms <- traverse DSL.toJSVal millis
  callArgs (Date d) "setUTCSeconds" (catMaybes [Just s, ms])
-----------------------------------------------------------------------------