0 | module IotaTime.TimeZone
  1 |
  2 | import public IotaTime.TimeZone.Core
  3 | import public IotaTime.TimeZone.Error
  4 | import public IotaTime.Tzdb.Metadata
  5 | import IotaTime.Tzdb.Provider
  6 | import IotaTime.Tzdb
  7 |
  8 | %default total
  9 |
 10 | export
 11 | record TimeZoneProviderRep where
 12 | constructor MkTimeZoneProviderRep
 13 | providerRepresentation : IotaTime.Tzdb.Provider.TimeZoneProviderRep
 14 |
 15 | ||| An opaque source of time zones and associated metadata.
 16 | public export
 17 | TimeZoneProvider : Type
 18 | TimeZoneProvider = IotaTime.TimeZone.TimeZoneProviderRep
 19 |
 20 | ||| Build a provider from its loading operations.
 21 | public export
 22 | timeZoneProvider : IO (Either TzdbError TimeZone) ->
 23 |  (String -> IO (Either TzdbError TimeZone)) ->
 24 |  IO (Either TzdbError TimeZone) ->
 25 |  IO (Either TzdbError (List String)) ->
 26 |  IO (Either TzdbError TzdbMetadata) ->
 27 |  TimeZoneProvider
 28 | timeZoneProvider utcAction zoneAction localAction availableAction metadataAction =
 29 | MkTimeZoneProviderRep $ IotaTime.Tzdb.Provider.timeZoneProvider
 30 | utcAction zoneAction localAction availableAction metadataAction
 31 |
 32 | export
 33 | record TimeZoneCachePolicyRep where
 34 | constructor MkTimeZoneCachePolicyRep
 35 | cachePolicyRepresentation : IotaTime.Tzdb.Provider.TimeZoneCachePolicyRep
 36 |
 37 | ||| An opaque selection of provider results to cache.
 38 | public export
 39 | TimeZoneCachePolicy : Type
 40 | TimeZoneCachePolicy = IotaTime.TimeZone.TimeZoneCachePolicyRep
 41 |
 42 | ||| Select which successful provider queries are retained in memory.
 43 | public export
 44 | timeZoneCachePolicy : (cacheNamedZones : Bool) ->
 45 | (cacheAvailableZones : Bool) ->
 46 | (cacheMetadata : Bool) ->
 47 | (cacheLocalZone : Bool) ->
 48 | TimeZoneCachePolicy
 49 | timeZoneCachePolicy named available valueMetadata local =
 50 | MkTimeZoneCachePolicyRep $ IotaTime.Tzdb.Provider.timeZoneCachePolicy
 51 | named available valueMetadata local
 52 |
 53 | ||| Cache named zones, discovery, and metadata while keeping the local zone live.
 54 | public export
 55 | defaultTimeZoneCachePolicy : TimeZoneCachePolicy
 56 | defaultTimeZoneCachePolicy =
 57 | MkTimeZoneCachePolicyRep IotaTime.Tzdb.Provider.defaultTimeZoneCachePolicy
 58 |
 59 | ||| Wrap a provider in caller-owned caches for selected successful operations.
 60 | public export
 61 | cachedTimeZoneProvider : TimeZoneCachePolicy -> TimeZoneProvider ->
 62 |  IO TimeZoneProvider
 63 | cachedTimeZoneProvider (MkTimeZoneCachePolicyRep policy)
 64 |  (MkTimeZoneProviderRep provider) =
 65 | MkTimeZoneProviderRep <$>
 66 | IotaTime.Tzdb.Provider.cachedTimeZoneProvider policy provider
 67 |
 68 | ||| The built-in Unix filesystem provider.
 69 | public export
 70 | unixTimeZoneProvider : TimeZoneProvider
 71 | unixTimeZoneProvider = MkTimeZoneProviderRep IotaTime.Tzdb.unixTimeZoneProvider
 72 |
 73 | ||| The provider selected for the current operating system.
 74 | public export
 75 | systemTimeZoneProvider : TimeZoneProvider
 76 | systemTimeZoneProvider =
 77 | MkTimeZoneProviderRep IotaTime.Tzdb.systemTimeZoneProvider
 78 |
 79 | ||| Snapshot the native Windows registry into an immutable provider.
 80 | public export
 81 | windowsSnapshotTimeZoneProvider : IO (Either TzdbError TimeZoneProvider)
 82 | windowsSnapshotTimeZoneProvider = do
 83 | provider <- IotaTime.Tzdb.windowsSnapshotTimeZoneProvider
 84 | pure (MkTimeZoneProviderRep <$> provider)
 85 |
 86 | ||| Load UTC through an explicit provider.
 87 | public export
 88 | utcWith : TimeZoneProvider -> IO (Either TzdbError TimeZone)
 89 | utcWith (MkTimeZoneProviderRep provider) = IotaTime.Tzdb.utcWith provider
 90 |
 91 | ||| Load a named zone through an explicit provider.
 92 | public export
 93 | timeZoneWith : TimeZoneProvider -> String -> IO (Either TzdbError TimeZone)
 94 | timeZoneWith (MkTimeZoneProviderRep provider) = IotaTime.Tzdb.timeZoneWith provider
 95 |
 96 | ||| Load the local zone through an explicit provider.
 97 | public export
 98 | localZoneWith : TimeZoneProvider -> IO (Either TzdbError TimeZone)
 99 | localZoneWith (MkTimeZoneProviderRep provider) = IotaTime.Tzdb.localZoneWith provider
100 |
101 | ||| Enumerate zones through an explicit provider.
102 | public export
103 | availableZonesWith : TimeZoneProvider ->
104 |  IO (Either TzdbError (List String))
105 | availableZonesWith (MkTimeZoneProviderRep provider) =
106 | IotaTime.Tzdb.availableZonesWith provider
107 |
108 | ||| Query version and identifier metadata through an explicit provider.
109 | public export
110 | metadataWith : TimeZoneProvider -> IO (Either TzdbError TzdbMetadata)
111 | metadataWith (MkTimeZoneProviderRep provider) = IotaTime.Tzdb.metadataWith provider
112 |
113 | ||| Load UTC from the platform time-zone database.
114 | public export
115 | utc : IO (Either TzdbError TimeZone)
116 | utc = IotaTime.Tzdb.loadSystemUtc
117 |
118 | ||| Load a named zone from the platform time-zone database.
119 | public export
120 | timeZone : String -> IO (Either TzdbError TimeZone)
121 | timeZone = IotaTime.Tzdb.loadSystemTimeZone
122 |
123 | ||| Load the locally configured platform zone.
124 | public export
125 | localZone : IO (Either TzdbError TimeZone)
126 | localZone = IotaTime.Tzdb.loadSystemLocalZone
127 |
128 | ||| List every zone available through the platform provider.
129 | public export
130 | availableZones : IO (Either TzdbError (List String))
131 | availableZones = IotaTime.Tzdb.loadSystemAvailableZones
132 |
133 | ||| Query version, aliases, and platform mappings from the system provider.
134 | public export
135 | metadata : IO (Either TzdbError TzdbMetadata)
136 | metadata = IotaTime.TimeZone.metadataWith
137 | IotaTime.TimeZone.systemTimeZoneProvider
138 |