This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for "date" and "time_ms" logical types in Avro (#114)
Co-authored-by: Flavio Corpa <[email protected]>
- Loading branch information
Showing
6 changed files
with
49 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{-# language GeneralizedNewtypeDeriving #-} | ||
module Data.Time.Millis where | ||
|
||
|
||
import Control.DeepSeq (NFData) | ||
import Data.Avro.Encode | ||
import Data.Avro.EncodeRaw | ||
import Data.Avro.FromAvro | ||
import Data.Avro.HasAvroSchema | ||
import Data.Avro.Schema as S | ||
import Data.Avro.ToAvro | ||
import Data.Avro.Types as T | ||
import Data.Tagged | ||
import Data.Time | ||
import Unsafe.Coerce | ||
|
||
-- | Wrapper for time difference expressed in milliseconds | ||
newtype DiffTimeMs = DiffTimeMs { unDiffTimeMs :: DiffTime } | ||
deriving (Show, Eq, Ord, Enum, Num, Fractional, Real, RealFrac, NFData) | ||
|
||
instance EncodeAvro DiffTimeMs where | ||
avro d | ||
-- Unfortunately, the AvroM constructor is not exposed :( | ||
= unsafeCoerce ( encodeRaw (fromIntegral $ diffTimeToMillis d :: Int) | ||
, S.Long (Just TimeMicros) ) | ||
|
||
instance HasAvroSchema DiffTimeMs where | ||
schema = Tagged $ S.Int (Just TimeMillis) | ||
|
||
instance ToAvro DiffTimeMs where | ||
toAvro = T.Int . fromIntegral . diffTimeToMillis | ||
|
||
instance FromAvro DiffTimeMs where | ||
fromAvro (T.Int v) = pure $ millisToDiffTime (toInteger v) | ||
fromAvro v = badValue v "TimeMicros" | ||
|
||
diffTimeToMillis :: DiffTimeMs -> Integer | ||
diffTimeToMillis = (`div` 1000000000) . diffTimeToPicoseconds . unDiffTimeMs | ||
|
||
millisToDiffTime :: Integer -> DiffTimeMs | ||
millisToDiffTime = DiffTimeMs . picosecondsToDiffTime . (* 1000000000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters