module Composite.Aeson.DateTimeFormatUtils
( fixupTzIn, fixupTzOut, fixupMs
) where
import Data.Char (isDigit)
import Data.Function (fix)
import Data.List (stripPrefix)
fixupTzIn :: String -> String
fixupTzIn s = maybe s ((++ "+00:00") . reverse) $ stripPrefix "Z" (reverse s)
fixupTzOut :: String -> String
fixupTzOut s = maybe s ((++ "Z") . reverse) $ stripPrefix "00:00+" (reverse s)
fixupMs :: String -> String
fixupMs (':':s10:s1:tzChar:rest) | tzChar == 'Z' || tzChar == '+' || tzChar == '-' =
':':s10:s1:'.':'0':'0':'0':tzChar:rest
fixupMs (':':s10:s1:'.':digitsAndRest) =
let (digits, rest) = span isDigit digitsAndRest
newDigits = take 3 (digits ++ fix ('0':))
in ':':s10:s1:'.':(newDigits ++ rest)
fixupMs (c:cs) = c : fixupMs cs
fixupMs [] = []