fuzzytime 0.1.1 → 0.2
raw patch · 2 files changed
+108/−28 lines, 2 filesdep ~base
Dependency ranges changed: base
Files
- fuzzytime.cabal +2/−2
- fuzzytime.hs +106/−26
fuzzytime.cabal view
@@ -1,5 +1,5 @@ Name: fuzzytime-Version: 0.1.1+Version: 0.2 Description: Print current time in a more casual way Synopsis: Print current time in a more casual way Category: Utils@@ -12,4 +12,4 @@ Executable fuzzytime Main-is: fuzzytime.hs- Build-Depends: base >= 3 && <= 4, cmdargs, old-time+ Build-Depends: base >= 4 && < 5, cmdargs, old-time
fuzzytime.hs view
@@ -11,7 +11,7 @@ ---- Prints the current time in a more casual way (the \"ten past six\"-style). -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -module Fuzzytime (+module Main ( -- * Description -- $description main@@ -22,6 +22,7 @@ import System.Console.CmdArgs import System.Time + -- $description -- A small utility to print the current time in a more casual way (the \"ten past six\"-style). --@@ -31,12 +32,19 @@ -- -- To add a new language, two things need to be done: ----- (1) The new language has to be added to the instance of Show FuzzyTime. (And to the help message.)+-- (1) The new language has to be added to the instance of Show FuzzyTime, and to the help message. -- -- (2) An appropriate function has to be created to turn FuzzyTime -> String. -- CHANGELOG+-- TODO+-- noon, midnight+-- more languages+-- wrong cli options+-- 0.2 2011.01.12+-- added French and German+-- added 12 vs. 24-hour clock -- 0.1 2010.12.05 -- initial release: two languages (en and pl), 1 < precision < 60 -- 0.1.1 2010.12.06@@ -55,7 +63,11 @@ confDefaultPrec :: Int confDefaultPrec = 5 +-- | \[config] The default for 12 vs. 24-hour clock.+confDefaultClock :: Int+confDefaultClock = 12 + -- main ================================================================================= @@ -64,24 +76,26 @@ main = do cl <- cmdArgs clOpts now <- getClockTime >>= toCalendarTime- print $ toFuzzyTime (prec cl) (lang cl) now- + print $ toFuzzyTime (clock cl) (lang cl) (prec cl) now + -- cl args ============================================================================== -- | Two options can be set via the command line: language and precision. data CLOpts = CLOpts {- lang :: String- , prec :: Int+ clock :: Int+ , lang :: String+ , prec :: Int } deriving (Show, Data, Typeable) clOpts = CLOpts {- lang=confDefaultLang &= help "Language (currently en and pl); default en."- , prec=confDefaultPrec &= help "Precision (1 < prec < 60 [minutes]); default 5."+ clock =confDefaultClock &= help "12 or 24-hour clock; default 12-hour."+ , lang =confDefaultLang &= help "Language (currently de, en, fr and pl); default en."+ , prec =confDefaultPrec &= help "Precision (1 < prec < 60 [minutes]); default 5." } &= program "fuzzytime"- &= summary "Print fuzzy time, e.g. 10:52 -> ten to eleven.\nv0.1, 2010.05.12, kamil.stachowski@gmail.com, GPL3+"+ &= summary "Print fuzzy time, e.g. 10:52 -> ten to eleven.\nv0.2, 2011.01.12, kamil.stachowski@gmail.com, GPL3+" -- FuzzyTime – main =====================================================================@@ -96,30 +110,33 @@ } deriving (Eq) -- | This is where FuzzyTime Int Int String is turned into the time String.+-- It is assumed that by the time these functions are called, hour will be in [0..23] and min will be in [0..59]. instance Show FuzzyTime where show (FuzzyTime hour min lang) = case lang of+ "de" -> showFuzzyTimeDe hour min "en" -> showFuzzyTimeEn hour min+ "fr" -> showFuzzyTimeFr hour min "pl" -> showFuzzyTimePl hour min otherwise -> "Language " ++ lang ++ " is not supported." --- | Converts CalendarTime to FuzzyTime using the given precision. The language is set, too, for Show to know how to display it.+-- | Converts CalendarTime to FuzzyTime using the given precision. The language is also set, so that Show knows how to display it. toFuzzyTime ::- Int -- ^ precision+ Int -- ^ clock -> String -- ^ language+ -> Int -- ^ precision -> CalendarTime -- ^ time -> FuzzyTime-toFuzzyTime cPrec cLang time =- if fuzzdM min /= 60 then- FuzzyTime hour (fuzzdM min) cLang- else- FuzzyTime (hour+1) 0 cLang+toFuzzyTime cClock cLang cPrec time = FuzzyTime hour (fuzzdM min) cLang where min :: Int min = (ctMin time) hour :: Int- hour = (ctHour time) `mod` 12+ hour = if cClock==24 then+ (ctHour time)+ else+ (ctHour time) `mod` 12 fuzzdM :: Int -> Int fuzzdM m = let@@ -155,6 +172,59 @@ numeralEnHelper10 i = ["twenty", "thirty", "forty", "fifty"] !! (i-2) +-- German -------------------------------------------------------------------------------+++showFuzzyTimeDe :: Int -> Int -> String+showFuzzyTimeDe hour min+ | min == 0 = numeralDe hour ++ " Uhr"+ | min < 30 = numeralDe min ++ " nach " ++ numeralDe hour+ | min == 30 = "halb " ++ numeralDe (hour+1)+ | min > 30 = numeralDe (60-min) ++ " vor " ++ numeralDe (hour+1)+ | otherwise = "Oops, es sieht aus, dass es " ++ show hour ++ ":" ++ show min ++ " ist."+++numeralDe :: Int -> String+numeralDe n+ | n < 20 = numeralDeHelper1 n+ | n `mod` 10 == 0 = numeralDeHelper10 (n `div` 10)+ | otherwise = numeralDeHelper1 (n `mod` 10) ++ "und" ++ numeralDeHelper10 (n `div` 10)+ where+ numeralDeHelper1 :: Int -> String+ numeralDeHelper1 i = ["zwölf", "ein", "zwei", "drei", "vier", "fünf", "sechs", "sieben", "acht", "neun", "zehn", "elf", "zwőlf", "dreizehn", "vierzehn", "Viertel", "sechzehn", "siebzehn", "achtzehn", "neunzehn"] !! i+ numeralDeHelper10 :: Int -> String+ numeralDeHelper10 i = ["zwanzig", "dreissig", "vierzig", "fünfzig"] !! (i-2)+++-- French -------------------------------------------------------------------------------+++showFuzzyTimeFr :: Int -> Int -> String+showFuzzyTimeFr hour min+ | min == 0 = numeralFr hour ++ hourFr hour+ | min == 15 = numeralFr hour ++ hourFr hour ++ " et quart"+ | min < 30 = numeralFr hour ++ hourFr hour ++ " " ++ numeralFr min+ | min == 30 = numeralFr hour ++ hourFr hour ++ " et demie"+ | min == 45 = numeralFr (hour+1) ++ hourFr (hour+1) ++ " moins le quart"+ | min > 30 = numeralFr (hour+1) ++ hourFr (hour+1) ++ " moins " ++ numeralFr (60-min)+ | otherwise = "Oops, il semble qu’il est " ++ show hour ++ ":" ++ show min ++ "."+ where+ hourFr :: Int -> String+ hourFr h = if h==1 then " heure" else " heures"+++numeralFr :: Int -> String+numeralFr n+ | n < 20 = numeralFrHelper1 n+ | n `mod` 10 == 0 = numeralFrHelper10 (n `div` 10)+ | otherwise = numeralFrHelper10 (n `div` 10) ++ "-" ++ numeralFrHelper1 (n `mod` 10)+ where+ numeralFrHelper1 :: Int -> String+ numeralFrHelper1 i = ["douze", "une", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf", "dix", "onze", "douze", "treize", "quatorze", "quinze", "seize", "dix-sept", "dix-huit", "dix-neuf"] !! i+ numeralFrHelper10 :: Int -> String+ numeralFrHelper10 i = ["vingt", "trente", "quarante", "cinquante"] !! (i-2)++ -- Polish ------------------------------------------------------------------------------ @@ -169,17 +239,27 @@ numeralPlCard :: Int -> String numeralPlCard n- | n < 20 = numeralPlHelper1 n- | n `mod` 10 == 0 = numeralPlHelper10 (n `div` 10)- | otherwise = numeralPlHelper10 (n `div` 10) ++ " " ++ numeralPlHelper1 (n `mod` 10)+ | n < 20 = numeralPlCardHelper1 n+ | n `mod` 10 == 0 = numeralPlCardHelper10 (n `div` 10)+ | otherwise = numeralPlCardHelper10 (n `div` 10) ++ " " ++ numeralPlCardHelper1 (n `mod` 10) where- numeralPlHelper1 :: Int -> String- numeralPlHelper1 i = ["jeden", "dwa", "trzy", "cztery", "pięć", "sześć", "siedem", "osiem", "dziewięć", "dziesięć", "jedenaście", "dwanaście", "trzynaście", "czternaście", "kwadrans", "szesnaście", "siedemnaście", "osiemnaście", "dziewiętnaście"] !! (i-1)- numeralPlHelper10 :: Int -> String- numeralPlHelper10 i = ["dwadzieścia", "trzydzieści", "czterdzieści", "pięćdziesiąt"] !! (i-2)+ numeralPlCardHelper1 :: Int -> String+ numeralPlCardHelper1 i = ["jeden", "dwa", "trzy", "cztery", "pięć", "sześć", "siedem", "osiem", "dziewięć", "dziesięć", "jedenaście", "dwanaście", "trzynaście", "czternaście", "kwadrans", "szesnaście", "siedemnaście", "osiemnaście", "dziewiętnaście"] !! (i-1)+ numeralPlCardHelper10 :: Int -> String+ numeralPlCardHelper10 i = ["dwadzieścia", "trzydzieści", "czterdzieści", "pięćdziesiąt"] !! (i-2) numeralPlOrdNom :: Int -> String-numeralPlOrdNom n = ["pierwsza", "druga", "trzecia", "czwarta", "piąta", "szósta", "siódma", "ósma", "dziewiąta", "dziesiąta", "jedenasta", "dwunasta"] !! (n-1)+numeralPlOrdNom n+ | n <= 20 = numeralPlOrdNomHelper1 n+ | otherwise = "dwudziesta " ++ numeralPlOrdNomHelper1 (n `mod` 10)+ where+ numeralPlOrdNomHelper1 :: Int -> String+ numeralPlOrdNomHelper1 n = ["dwunasta", "pierwsza", "druga", "trzecia", "czwarta", "piąta", "szósta", "siódma", "ósma", "dziewiąta", "dziesiąta", "jedenasta", "dwunasta", "trzynasta", "czternasta", "piętnasta", "szesnasta", "siedemnasta", "osiemnasta", "dziewiętnasta", "dwudziesta"] !! n numeralPlOrdPraep :: Int -> String-numeralPlOrdPraep n = ["dwunastej","pierwszej", "drugiej", "trzeciej", "czwartej", "piątej", "szóstej", "siódmej", "ósmej", "dziewiątej", "dziesiątej", "jedenastej", "dwunastej"] !! n+numeralPlOrdPraep n+ | n <= 20 = numeralPlOrdPraepHelper1 n+ | otherwise = "dwudziestej " ++ numeralPlOrdPraepHelper1 (n `mod` 10)+ where+ numeralPlOrdPraepHelper1 :: Int -> String+ numeralPlOrdPraepHelper1 n = ["dwunastej","pierwszej", "drugiej", "trzeciej", "czwartej", "piątej", "szóstej", "siódmej", "ósmej", "dziewiątej", "dziesiątej", "jedenastej", "dwunastej", "trzynastej", "czternastej", "piętnastej", "szesnastej", "siedemnastej", "osiemnastej", "dziewiętnastej", "dwudziestej"] !! n