system-locale (empty) → 0.1.0.0
raw patch · 5 files changed
+162/−0 lines, 5 filesdep +basedep +hspecdep +megaparsecsetup-changed
Dependencies added: base, hspec, megaparsec, process, system-locale, time
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- src/System/Locale/Read.hs +91/−0
- system-locale.cabal +38/−0
- test/Spec.hs +1/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Moritz Kiefer (c) 2016++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Moritz Kiefer nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/System/Locale/Read.hs view
@@ -0,0 +1,91 @@+{-| Read locales on unix systems and parse them into their corresponding 'TimeLocale' representation.+-}+module System.Locale.Read+ (getLocale+ ,getCurrentLocale+ ,parseLocale+ ,TimeLocale(..)+ ,LocaleParseException(..)+ ) where++import Control.Exception+import Data.Time.Format (TimeLocale(..))+import System.Process+import Text.Megaparsec+import Text.Megaparsec.String++-- | Thrown when the locale cannot be parsed+data LocaleParseException =+ LocaleParseException (ParseError Char Dec)+ deriving (Show,Eq)++instance Exception LocaleParseException++-- | 'Parser' for locales returned by the unix utility 'locale'+parseLocale :: Parser TimeLocale+parseLocale =+ do abDay <- parseSemicolonSeparatedLine+ day <- parseSemicolonSeparatedLine+ abMon <- parseSemicolonSeparatedLine+ mon <- parseSemicolonSeparatedLine+ [am,pm] <- parseSemicolonSeparatedLine+ dateTimeFmt' <- manyTill anyChar newline+ dateFmt' <- manyTill anyChar newline+ timeFmt' <- manyTill anyChar newline+ time12Fmt' <- manyTill anyChar newline+ pure (TimeLocale (zip day abDay)+ (zip mon abMon)+ (am,pm)+ dateTimeFmt'+ dateFmt'+ timeFmt'+ time12Fmt'+ [])++-- | Read a locale with 'LC_TIME' set according to the first argument.+--+-- Throws a 'LocaleParseException' if the output of calling 'locale'+-- cannot be parsed.+--+-- The 'knownTimeZones' field will always be empty.+--+-- > getLocale (Just "en_US.UTF-8")+getLocale :: Maybe String -> IO TimeLocale+getLocale localeName =+ do output <-+ readCreateProcess (getLocaleProcess localeName)+ ""+ case runParser parseLocale "" output of+ Left err -> throwIO (LocaleParseException err)+ Right locale -> pure locale++-- | Get the current locale of the process.+--+-- Throws a 'LocaleParseException' if the output of calling 'locale'+-- cannot be parsed.+--+-- The 'knownTimeZones' field will always be empty.+getCurrentLocale :: IO TimeLocale+getCurrentLocale = getLocale Nothing++getLocaleProcess :: Maybe String -> CreateProcess+getLocaleProcess localeName =+ (proc "locale"+ ["abday"+ ,"day"+ ,"abmon"+ ,"mon"+ ,"am_pm"+ ,"d_t_fmt"+ ,"d_fmt"+ ,"t_fmt"+ ,"t_fmt_ampm"]) {env = toLangEnv <$> localeName}++parseSemicolonSeparatedLine :: Parser [String]+parseSemicolonSeparatedLine =+ sepBy (many (noneOf [';','\n']))+ (char ';') <*+ newline++toLangEnv :: String -> [(String,String)]+toLangEnv s = [("LC_TIME",s)]
+ system-locale.cabal view
@@ -0,0 +1,38 @@+name: system-locale+version: 0.1.0.0+synopsis: Get system locales+description: Please see README.md+homepage: https://github.com/cocreature/system-locale+license: BSD3+license-file: LICENSE+author: Moritz Kiefer+maintainer: moritz.kiefer@purelyfunctional.org+copyright: 2016+category: Unknown+build-type: Simple+-- extra-source-files:+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules: System.Locale.Read+ build-depends: base >= 4.7 && < 5+ , megaparsec >= 5.0 && < 5.2+ , process >= 1.2 && < 1.5+ , time >= 1.5 && < 1.7+ ghc-options: -Wall+ default-language: Haskell2010++test-suite system-locale-test+ type: exitcode-stdio-1.0+ hs-source-dirs: test+ main-is: Spec.hs+ build-depends: base+ , hspec+ , system-locale+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/cocreature/system-locale
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}