0 | module IotaTime.Locale
3 | import IotaTime.Locale.Unix.Platform
4 | import IotaTime.Locale.Windows.Platform
7 | import Derive.Prelude
9 | %language ElabReflection
19 | constructor MkLocale
20 | storedLocaleId : String
21 | storedMonthNames : Vect 12 String
22 | storedMonthNamesShort : Vect 12 String
23 | storedDayNames : Vect 7 String
24 | storedDayNamesShort : Vect 7 String
25 | storedAmName : String
26 | storedPmName : String
27 | storedRawDateFormat : String
28 | storedRawTimeFormat : String
29 | storedRawDateTimeFormat : String
33 | localeId : Locale -> String
34 | localeId = storedLocaleId
38 | monthNames : Locale -> Vect 12 String
39 | monthNames = storedMonthNames
43 | monthNamesShort : Locale -> Vect 12 String
44 | monthNamesShort = storedMonthNamesShort
48 | dayNames : Locale -> Vect 7 String
49 | dayNames = storedDayNames
53 | dayNamesShort : Locale -> Vect 7 String
54 | dayNamesShort = storedDayNamesShort
58 | amName : Locale -> String
59 | amName = storedAmName
63 | pmName : Locale -> String
64 | pmName = storedPmName
67 | rawDateFormat : Locale -> String
68 | rawDateFormat = storedRawDateFormat
71 | rawTimeFormat : Locale -> String
72 | rawTimeFormat = storedRawTimeFormat
75 | rawDateTimeFormat : Locale -> String
76 | rawDateTimeFormat = storedRawDateTimeFormat
78 | %runElab derive `{Locale
} [Eq]
82 | show value = "<Locale " ++ show value.storedLocaleId ++ ">"
87 | = LocaleNotFound String
88 | | LocalePlatformError String
91 | Eq LocaleError where
92 | LocaleNotFound left == LocaleNotFound right = left == right
93 | LocalePlatformError left == LocalePlatformError right = left == right
97 | Show LocaleError where
98 | show (LocaleNotFound name) = "locale is not installed: " ++ name
99 | show (LocalePlatformError message) = message
101 | fromUnixData : String -> UnixLocaleData -> Locale
102 | fromUnixData valueId localeData = MkLocale
104 | (localeMonthNames localeData)
105 | (localeMonthNamesShort localeData)
106 | (localeDayNames localeData)
107 | (localeDayNamesShort localeData)
108 | (localeAmName localeData)
109 | (localePmName localeData)
110 | (localeDateFormat localeData)
111 | (localeTimeFormat localeData)
112 | (localeDateTimeFormat localeData)
114 | unixLocaleByName : String -> IO (Either LocaleError Locale)
115 | unixLocaleByName name = do
116 | loaded <- loadUnixLocaleData name
117 | pure (case loaded of
118 | Nothing => Left (LocaleNotFound name)
119 | Just localeData => Right (fromUnixData name localeData))
121 | currentLocaleId : IO String
122 | currentLocaleId = do
123 | all <- getEnv "LC_ALL"
124 | time <- getEnv "LC_TIME"
125 | language <- getEnv "LANG"
126 | pure (firstPresent [all, time, language])
128 | firstPresent : List (Maybe String) -> String
129 | firstPresent [] = "C"
130 | firstPresent (Nothing :: rest) = firstPresent rest
131 | firstPresent (Just "" :: rest) = firstPresent rest
132 | firstPresent (Just value :: rest) = value
134 | unixCurrentLocale : IO (Either LocaleError Locale)
135 | unixCurrentLocale = do
136 | valueId <- currentLocaleId
137 | loaded <- loadUnixLocaleData ""
139 | Just localeData => pure (Right (fromUnixData valueId localeData))
141 | fallback <- loadUnixLocaleData "C"
142 | pure (case fallback of
143 | Just localeData => Right (fromUnixData "C" localeData)
144 | Nothing => Left (LocalePlatformError
145 | "native Unix locale access could not load the POSIX C locale"))
147 | fromWindowsData : WindowsLocaleData -> Locale
148 | fromWindowsData localeData =
149 | let dateFormat = IotaTime.Locale.Windows.Platform.localeDateFormat localeData
150 | timeFormat = IotaTime.Locale.Windows.Platform.localeTimeFormat localeData
152 | (localeIdentifier localeData)
153 | (IotaTime.Locale.Windows.Platform.localeMonthNames localeData)
154 | (IotaTime.Locale.Windows.Platform.localeMonthNamesShort localeData)
155 | (IotaTime.Locale.Windows.Platform.localeDayNames localeData)
156 | (IotaTime.Locale.Windows.Platform.localeDayNamesShort localeData)
157 | (IotaTime.Locale.Windows.Platform.localeAmName localeData)
158 | (IotaTime.Locale.Windows.Platform.localePmName localeData)
161 | (dateFormat ++ " " ++ timeFormat)
163 | windowsLocaleByName : String -> IO (Either LocaleError Locale)
164 | windowsLocaleByName name = do
165 | loaded <- loadWindowsLocaleData False name
166 | pure (case loaded of
167 | Nothing => Left (LocaleNotFound name)
168 | Just localeData => Right (fromWindowsData localeData))
170 | windowsCurrentLocale : IO (Either LocaleError Locale)
171 | windowsCurrentLocale = do
172 | loaded <- loadWindowsLocaleData True ""
173 | pure (case loaded of
174 | Nothing => Left (LocalePlatformError
175 | "native Windows locale access could not load the user locale")
176 | Just localeData => Right (fromWindowsData localeData))
180 | localeByName : String -> IO (Either LocaleError Locale)
181 | localeByName name = if isWindows
182 | then windowsLocaleByName name
183 | else unixLocaleByName name
187 | currentLocale : IO (Either LocaleError Locale)
188 | currentLocale = if isWindows
189 | then windowsCurrentLocale
190 | else unixCurrentLocale
197 | [ "January", "February", "March", "April", "May", "June"
198 | , "July", "August", "September", "October", "November", "December"
200 | [ "Jan", "Feb", "Mar", "Apr", "May", "Jun"
201 | , "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
203 | [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
206 | [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ]
211 | "%a %d %b %Y %r %Z"
218 | [ "Januar", "Februar", "März", "April", "Mai", "Juni"
219 | , "Juli", "August", "September", "Oktober", "November", "Dezember"
221 | [ "Jan", "Feb", "Mär", "Apr", "Mai", "Jun"
222 | , "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"
224 | [ "Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag"
227 | [ "So", "Mo", "Di", "Mi", "Do", "Fr", "Sa" ]
232 | "%a %d %b %Y %T %Z"
239 | [ "1月", "2月", "3月", "4月", "5月", "6月"
240 | , "7月", "8月", "9月", "10月", "11月", "12月"
242 | [ "1月", "2月", "3月", "4月", "5月", "6月"
243 | , "7月", "8月", "9月", "10月", "11月", "12月"
245 | [ "日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日" ]
246 | [ "日", "月", "火", "水", "木", "金", "土" ]
251 | "%Y年%m月%d日 %H時%M分%S秒"