{-# OPTIONS_GHC -fno-warn-orphans #-}

module Data.Time.Clock.Instances
  (
  ) where

import ClassyPrelude

import Database.Persist.Sql

import Data.Proxy

import Data.Binary (Binary)
import qualified Data.Binary as Binary

import Data.Time.Clock
import Data.Time.Calendar.Instances ()
import Web.PathPieces


instance Hashable DiffTime where
  hashWithSalt s = hashWithSalt s . toRational

instance PersistField NominalDiffTime where
  toPersistValue   = toPersistValue . toRational
  fromPersistValue = fmap fromRational . fromPersistValue

instance PersistFieldSql NominalDiffTime where
  sqlType _ = sqlType (Proxy @Rational)


deriving instance Generic UTCTime
instance Hashable UTCTime

instance PathPiece UTCTime where
  toPathPiece = pack . formatTime defaultTimeLocale "%0Y-%m-%dT%H:%M:%S%Q%z"
  fromPathPiece = parseTimeM False defaultTimeLocale "%Y-%m-%dT%H:%M:%S%Q%z" . unpack


instance Binary DiffTime where
  get = fromRational <$> Binary.get
  put = Binary.put . toRational

instance Binary UTCTime
