Convert seconds to compound duration: Difference between revisions
Content deleted Content added
→{{header|Haskell}}: Parameterised both local names and assumptions about hours per day, days per week. |
|||
Line 1,780: | Line 1,780: | ||
Or, parameterising the local names for these durations: |
Or, parameterising both the local names for these durations, and also the working assumptions about hours per day, and days per week: |
||
<lang haskell>import Data.List (intercalate, mapAccumR) |
<lang haskell>import Data.List (intercalate, mapAccumR) |
||
----------------- COMPOUND DURATION STRINGS ---------------- |
|||
⚫ | |||
translation n local = |
|||
⚫ | |||
durationString :: String -> Int -> Int -> Int -> String |
|||
durationString local daysPerWeek hoursPerDay n = |
|||
⚫ | |||
intercalate " -> " $ |
|||
⚫ | |||
⚫ | |||
timePhrase daysPerWeek hoursPerDay xs n = |
|||
intercalate |
|||
", " |
|||
⚫ | |||
timeTags :: (Int, String) -> [String] -> [String] |
timeTags :: (Int, String) -> [String] -> [String] |
||
Line 1,796: | Line 1,802: | ||
| otherwise = xs |
| otherwise = xs |
||
weekParts :: Int -> [Int] |
weekParts :: Int -> Int -> Int -> [Int] |
||
weekParts daysPerWeek hoursPerDay = |
|||
snd . flip (mapAccumR byUnits) [0, daysPerWeek, hoursPerDay, 60, 60] |
|||
byUnits :: Int -> Int -> (Int, Int) |
byUnits :: Int -> Int -> (Int, Int) |
||
Line 1,806: | Line 1,813: | ||
| otherwise = (1, rest) |
| otherwise = (1, rest) |
||
---------------------------TEST |
---------------------------- TEST -------------------------- |
||
main :: IO () |
main :: IO () |
||
main = |
main = do |
||
let names = "wk d hr min sec" |
|||
let tests = [7259, 86400, 6000000] |
|||
putStrLn "Assuming 24/7:" |
|||
mapM_ (putStrLn . durationString names 7 24) tests |
|||
putStrLn "\nor, at 8 working hours per day, 5 days per week:" |
|||
mapM_ (putStrLn . durationString names 5 8) tests</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre>Assuming 24/7: |
|||
7259 -> 2 hr, 59 sec |
|||
86400 -> 1 d |
86400 -> 1 d |
||
6000000 -> 9 wk, 6 d, 10 hr, 40 min |
6000000 -> 9 wk, 6 d, 10 hr, 40 min |
||
or, at 8 working hours per day, 5 days per week: |
|||
7259 -> 2 hr, 59 sec |
|||
86400 -> 3 d |
|||
6000000 -> 41 wk, 3 d, 2 hr, 40 min</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |