@@ -46,6 +46,9 @@ module Dhall.Eval (
46
46
, Val (.. )
47
47
, (~>)
48
48
, textShow
49
+ , dateShow
50
+ , timeShow
51
+ , timezoneShow
49
52
) where
50
53
51
54
import Data.Bifunctor (first )
@@ -54,6 +57,7 @@ import Data.Foldable (foldr', toList)
54
57
import Data.List.NonEmpty (NonEmpty (.. ))
55
58
import Data.Sequence (Seq , ViewL (.. ), ViewR (.. ))
56
59
import Data.Text (Text )
60
+ import Data.Time (Day , TimeOfDay (.. ), TimeZone )
57
61
import Data.Void (Void )
58
62
import Dhall.Map (Map )
59
63
import Dhall.Set (Set )
@@ -81,7 +85,7 @@ import qualified Data.Time as Time
81
85
import qualified Dhall.Map as Map
82
86
import qualified Dhall.Set
83
87
import qualified Dhall.Syntax as Syntax
84
- import qualified Text.Printf
88
+ import qualified Text.Printf as Printf
85
89
86
90
data Environment a
87
91
= Empty
@@ -206,10 +210,13 @@ data Val a
206
210
207
211
| VDate
208
212
| VDateLiteral Time. Day
213
+ | VDateShow ! (Val a )
209
214
| VTime
210
215
| VTimeLiteral Time. TimeOfDay Word
216
+ | VTimeShow ! (Val a )
211
217
| VTimeZone
212
218
| VTimeZoneLiteral Time. TimeZone
219
+ | VTimeZoneShow ! (Val a )
213
220
214
221
| VList ! (Val a )
215
222
| VListLit ! (Maybe (Val a )) ! (Seq (Val a ))
@@ -667,14 +674,26 @@ eval !env t0 =
667
674
VDate
668
675
DateLiteral d ->
669
676
VDateLiteral d
677
+ DateShow ->
678
+ VPrim $ \ case
679
+ VDateLiteral d -> VTextLit (VChunks [] (dateShow d))
680
+ t -> VDateShow t
670
681
Time ->
671
682
VTime
672
683
TimeLiteral t p ->
673
684
VTimeLiteral t p
685
+ TimeShow ->
686
+ VPrim $ \ case
687
+ VTimeLiteral d p -> VTextLit (VChunks [] (timeShow d p))
688
+ t -> VTimeShow t
674
689
TimeZone ->
675
690
VTimeZone
676
691
TimeZoneLiteral z ->
677
692
VTimeZoneLiteral z
693
+ TimeZoneShow ->
694
+ VPrim $ \ case
695
+ VTimeZoneLiteral d -> VTextLit (VChunks [] (timezoneShow d))
696
+ t -> VTimeZoneShow t
678
697
List ->
679
698
VPrim VList
680
699
ListLit ma ts ->
@@ -898,9 +917,32 @@ textShow text = "\"" <> Text.concatMap f text <> "\""
898
917
f ' \r ' = " \\ r"
899
918
f ' \t ' = " \\ t"
900
919
f ' \f ' = " \\ f"
901
- f c | c <= ' \x1F ' = Text. pack (Text. Printf. printf " \\ u%04x" (Data.Char. ord c))
920
+ f c | c <= ' \x1F ' = Text. pack (Printf. printf " \\ u%04x" (Data.Char. ord c))
902
921
| otherwise = Text. singleton c
903
922
923
+ -- | Utility that powers the @Date/show@ built-in
924
+ dateShow :: Day -> Text
925
+ dateShow = Text. pack . Time. formatTime Time. defaultTimeLocale " %0Y-%m-%d"
926
+
927
+ -- | Utility that powers the @Time/show@ built-in
928
+ timeShow :: TimeOfDay -> Word -> Text
929
+ timeShow (TimeOfDay hh mm seconds) precision =
930
+ Text. pack (Printf. printf " %02d:%02d:%02d" hh mm ss <> suffix)
931
+ where
932
+ magnitude :: Integer
933
+ magnitude = 10 ^ precision
934
+
935
+ (ss, fraction) =
936
+ truncate (seconds * fromInteger magnitude) `divMod` magnitude
937
+
938
+ suffix
939
+ | precision == 0 = " "
940
+ | otherwise = Printf. printf " .%0*d" precision fraction
941
+
942
+ -- | Utility that powers the @TimeZone/show@ built-in
943
+ timezoneShow :: TimeZone -> Text
944
+ timezoneShow = Text. pack . Time. formatTime Time. defaultTimeLocale " %Ez"
945
+
904
946
conv :: forall a . Eq a => Environment a -> Val a -> Val a -> Bool
905
947
conv ! env t0 t0' =
906
948
case (t0, t0') of
@@ -1008,14 +1050,20 @@ conv !env t0 t0' =
1008
1050
True
1009
1051
(VDateLiteral l, VDateLiteral r) ->
1010
1052
l == r
1053
+ (VDateShow t, VDateShow t') ->
1054
+ conv env t t'
1011
1055
(VTime , VTime ) ->
1012
1056
True
1013
1057
(VTimeLiteral tl pl, VTimeLiteral tr pr) ->
1014
1058
tl == tr && pl == pr
1059
+ (VTimeShow t, VTimeShow t') ->
1060
+ conv env t t'
1015
1061
(VTimeZone , VTimeZone ) ->
1016
1062
True
1017
1063
(VTimeZoneLiteral l, VTimeZoneLiteral r) ->
1018
1064
l == r
1065
+ (VTimeZoneShow t, VTimeZoneShow t') ->
1066
+ conv env t t'
1019
1067
(VList a, VList a') ->
1020
1068
conv env a a'
1021
1069
(VListLit _ xs, VListLit _ xs') ->
@@ -1224,14 +1272,20 @@ quote !env !t0 =
1224
1272
Date
1225
1273
VDateLiteral d ->
1226
1274
DateLiteral d
1275
+ VDateShow t ->
1276
+ DateShow `qApp` t
1227
1277
VTime ->
1228
1278
Time
1229
1279
VTimeLiteral t p ->
1230
1280
TimeLiteral t p
1281
+ VTimeShow t ->
1282
+ TimeShow `qApp` t
1231
1283
VTimeZone ->
1232
1284
TimeZone
1233
1285
VTimeZoneLiteral z ->
1234
1286
TimeZoneLiteral z
1287
+ VTimeZoneShow t ->
1288
+ TimeZoneShow `qApp` t
1235
1289
VList t ->
1236
1290
List `qApp` t
1237
1291
VListLit ma ts ->
@@ -1427,14 +1481,20 @@ alphaNormalize = goEnv EmptyNames
1427
1481
Date
1428
1482
DateLiteral d ->
1429
1483
DateLiteral d
1484
+ DateShow ->
1485
+ DateShow
1430
1486
Time ->
1431
1487
Time
1432
1488
TimeLiteral t p ->
1433
1489
TimeLiteral t p
1490
+ TimeShow ->
1491
+ TimeShow
1434
1492
TimeZone ->
1435
1493
TimeZone
1436
1494
TimeZoneLiteral z ->
1437
1495
TimeZoneLiteral z
1496
+ TimeZoneShow ->
1497
+ TimeZoneShow
1438
1498
List ->
1439
1499
List
1440
1500
ListLit ma ts ->
0 commit comments