{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Hcompta.Model.Date where

import           Data.Data
import qualified Data.Fixed
import qualified Data.Time.Calendar    as Time
import qualified Data.Time.Clock       as Time
import qualified Data.Time.Clock.POSIX as Time (posixSecondsToUTCTime)
import qualified Data.Time.Format      as Time ()
import qualified Data.Time.LocalTime   as Time
import           Data.Typeable ()

-- * The 'Date' type

type Date = Time.UTCTime

nil :: Date
nil = Time.posixSecondsToUTCTime 0

gregorian :: Date -> (Integer, Int, Int)
gregorian = Time.toGregorian . Time.utctDay

year :: Date -> Integer
year = (\(x, _, _) -> x) . gregorian

month :: Date -> Int
month = (\(_, x, _) -> x) . gregorian

dom :: Date -> Int
dom = (\(_, _, x) -> x) . gregorian

tod :: Date -> Time.TimeOfDay
tod = Time.timeToTimeOfDay . Time.utctDayTime

hour :: Date -> Int
hour = (\(Time.TimeOfDay x _ _) -> x) . tod

minute :: Date -> Int
minute = (\(Time.TimeOfDay _ x _) -> x) . tod

second :: Date -> Data.Fixed.Pico
second = (\(Time.TimeOfDay _ _ x) -> x) . tod

data Interval
 =   Interval_None
 |   Interval_Days Int
 |   Interval_Weeks Int
 |   Interval_Months Int
 |   Interval_Quarters Int
 |   Interval_Years Int
 |   Interval_DayOfMonth Int
 |   Interval_DayOfWeek Int
 -- Interval_WeekOfYear Int
 -- Interval_MonthOfYear Int
 -- Interval_QuarterOfYear Int
 deriving (Data, Eq, Ord, Read, Show, Typeable)

type Smart
 = (String, String, String)

data Span
 =   Span (Maybe Time.Day) (Maybe Time.Day)
 deriving (Data, Eq, Ord, Read, Show, Typeable)

data Which
 =   Which_Primary
 |   Which_Secondary
 deriving (Eq, Read, Show)

type Year
 = Integer