diff options
Diffstat (limited to 'lib/Postdelay/TimeSpec')
-rw-r--r-- | lib/Postdelay/TimeSpec/Units.hs | 111 | ||||
-rw-r--r-- | lib/Postdelay/TimeSpec/Utils.hs | 35 |
2 files changed, 146 insertions, 0 deletions
diff --git a/lib/Postdelay/TimeSpec/Units.hs b/lib/Postdelay/TimeSpec/Units.hs new file mode 100644 index 0000000..330997a --- /dev/null +++ b/lib/Postdelay/TimeSpec/Units.hs | |||
@@ -0,0 +1,111 @@ | |||
1 | {-# LANGUAGE TemplateHaskell, TypeFamilies, GADTs, RankNTypes, DataKinds, ViewPatterns, ScopedTypeVariables #-} | ||
2 | |||
3 | module Postdelay.TimeSpec.Units | ||
4 | ( Time | ||
5 | |||
6 | , Second, Minute, Hour | ||
7 | , Day, Week, Month, Year | ||
8 | |||
9 | , timeLength | ||
10 | |||
11 | , module Data.Units.SI.Prefixes | ||
12 | ) where | ||
13 | |||
14 | import Postdelay.TimeSpec.Utils | ||
15 | |||
16 | import Control.Applicative | ||
17 | |||
18 | import Data.Metrology | ||
19 | import Data.Metrology.TH | ||
20 | import Data.Metrology.SI.Mono () | ||
21 | |||
22 | import Data.Units.SI | ||
23 | import Data.Units.SI.Prefixes | ||
24 | import Data.Units.SI.Parser | ||
25 | import qualified Data.Dimensions.SI as D | ||
26 | |||
27 | import Data.Foldable | ||
28 | import Data.Function | ||
29 | import Data.VectorSpace | ||
30 | |||
31 | import Data.Fixed (Fixed, HasResolution) | ||
32 | import qualified Data.Fixed as Fixed | ||
33 | |||
34 | |||
35 | import Text.Megaparsec | ||
36 | |||
37 | |||
38 | declareDerivedUnit "Day" [t| Hour |] 24 Nothing | ||
39 | declareDerivedUnit "Week" [t| Day |] 7 Nothing | ||
40 | declareDerivedUnit "Month" [t| Day |] 30 Nothing | ||
41 | declareDerivedUnit "Year" [t| Day |] 365.25 Nothing | ||
42 | |||
43 | type Time = MkQu_DLN D.Time DefaultLCSU Fixed.Pico | ||
44 | |||
45 | data Prefix where | ||
46 | Prefix :: forall p. (UnitPrefix p, Show p) => p -> Prefix | ||
47 | |||
48 | instance HasResolution p => AdditiveGroup (Fixed p) where | ||
49 | zeroV = 0 | ||
50 | (^+^) = (+) | ||
51 | negateV = negate | ||
52 | (^-^) = (-) | ||
53 | |||
54 | instance HasResolution p => VectorSpace (Fixed p) where | ||
55 | type Scalar (Fixed p) = Fixed p | ||
56 | (*^) = (*) | ||
57 | |||
58 | |||
59 | timeLength :: StringParser s m => m Time | ||
60 | timeLength = (*^) <$> lexeme rational <*> timeUnit | ||
61 | |||
62 | rational :: forall s m n. (StringParser s m, Fractional n, Num n) => m n | ||
63 | rational = combine <$> some digitChar <*> option [] (char '.' *> many digitChar) | ||
64 | where | ||
65 | combine :: [Char] -> [Char] -> n | ||
66 | combine (map asN -> whole) (map asN -> fractional) | ||
67 | = foldl' (\i l -> i * 10 + l) 0 whole + (foldr' (\h t -> h + t / 10) 0 fractional) / 10 | ||
68 | asN :: Char -> n | ||
69 | asN c = fromIntegral $ ((-) `on` fromEnum) c '0' | ||
70 | |||
71 | timeUnit :: StringParser s m => m Time | ||
72 | timeUnit = label "Unit of time" . choice $ | ||
73 | [ 1 % Second <$ choice [ string' "seconds" | ||
74 | , string' "second" | ||
75 | , string' "secs" | ||
76 | , string' "sec" | ||
77 | ] | ||
78 | , 1 % Minute <$ choice [ string' "minutes" | ||
79 | , string' "minute" | ||
80 | , string' "mins" | ||
81 | , string' "min" | ||
82 | ] | ||
83 | , 1 % Hour <$ choice [ string' "hours" | ||
84 | , string' "hour" | ||
85 | ] | ||
86 | , 1 % Day <$ choice [ string' "days" | ||
87 | , string' "day" | ||
88 | ] | ||
89 | , 1 % Week <$ choice [ string' "weeks" | ||
90 | , string' "week" | ||
91 | ] | ||
92 | , 1 % Month <$ choice [ string' "months" | ||
93 | , string' "month" | ||
94 | ] | ||
95 | , 1 % Year <$ choice [ string' "years" | ||
96 | , string' "year" | ||
97 | ] | ||
98 | ] ++ | ||
99 | [ (% Second) <$> option 1 siPrefix <* string "s" | ||
100 | , (% Hour) <$> option 1 siPrefix <* string "h" | ||
101 | , (% Day) <$> option 1 siPrefix <* string "d" | ||
102 | , (% Year) <$> option 1 siPrefix <* choice [ string "a", string "yr", string "yrs" ] | ||
103 | ] | ||
104 | |||
105 | siPrefix :: (StringParser s m, Fractional n) => m n | ||
106 | siPrefix = label "SI prefix" . choice $ map (\(Prefix p) -> multiplier p <$ string (show p)) | ||
107 | [ Prefix Deca, Prefix Hecto, Prefix Kilo, Prefix Mega, Prefix Giga | ||
108 | , Prefix Tera, Prefix Peta, Prefix Exa, Prefix Zetta, Prefix Yotta | ||
109 | , Prefix Deci, Prefix Centi, Prefix Milli, Prefix Micro, Prefix Nano | ||
110 | , Prefix Pico, Prefix Femto, Prefix Atto, Prefix Zepto, Prefix Yocto | ||
111 | ] | ||
diff --git a/lib/Postdelay/TimeSpec/Utils.hs b/lib/Postdelay/TimeSpec/Utils.hs new file mode 100644 index 0000000..e4ba732 --- /dev/null +++ b/lib/Postdelay/TimeSpec/Utils.hs | |||
@@ -0,0 +1,35 @@ | |||
1 | {-# LANGUAGE RankNTypes, ConstraintKinds, TypeFamilies, FlexibleContexts, ScopedTypeVariables, ViewPatterns #-} | ||
2 | |||
3 | module Postdelay.TimeSpec.Utils where | ||
4 | |||
5 | import Control.Applicative | ||
6 | import Control.Monad | ||
7 | import Control.Lens | ||
8 | |||
9 | import Data.Time | ||
10 | import Data.Time.Zones | ||
11 | |||
12 | import Data.AdditiveGroup | ||
13 | |||
14 | import Text.Megaparsec | ||
15 | import Text.Megaparsec.Prim (MonadParsec) | ||
16 | import qualified Text.Megaparsec.Lexer as L | ||
17 | |||
18 | |||
19 | type StringParser s m = (MonadParsec Dec s m, Token s ~ Char) | ||
20 | |||
21 | |||
22 | spaceConsumer :: StringParser s m => m () | ||
23 | spaceConsumer = L.space (void spaceChar) empty empty | ||
24 | |||
25 | lexeme :: StringParser s m => m a -> m a | ||
26 | lexeme = L.lexeme spaceConsumer | ||
27 | |||
28 | signed, optSigned :: (StringParser s m, AdditiveGroup n) => m n -> m n | ||
29 | signed = (<*>) (lexeme sign) | ||
30 | optSigned = (<*>) (option id $ lexeme sign) | ||
31 | |||
32 | sign :: (StringParser s m, AdditiveGroup n) => m (n -> n) | ||
33 | sign = choice [ id <$ char '+' | ||
34 | , negateV <$ char '-' | ||
35 | ] | ||