Convert seconds to compound duration: Difference between revisions
Convert seconds to compound duration (view source)
Revision as of 23:49, 5 September 2020
, 3 years ago→{{header|Haskell}}: Parameterised both local names and assumptions about hours per day, days per week.
m (→JS :: ES6) |
(→{{header|Haskell}}: Parameterised both local names and assumptions about hours per day, days per week.) |
||
Line 1,780:
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)
----------------- COMPOUND DURATION STRINGS ----------------
translation :: Int -> String -> String▼
intercalate " -> " $ [show, timePhrase (words local)] <*> [n]▼
durationString local daysPerWeek hoursPerDay n =
timePhrase xs n = intercalate ", " (foldr timeTags [] (zip (weekParts n) xs))▼
intercalate " -> " $
timePhrase daysPerWeek hoursPerDay xs n =
intercalate
", "
▲
timeTags :: (Int, String) -> [String] -> [String]
Line 1,796 ⟶ 1,802:
| otherwise = xs
weekParts :: Int -> Int -> Int -> [Int]
weekParts daysPerWeek hoursPerDay =
byUnits :: Int -> Int -> (Int, Int)
Line 1,806 ⟶ 1,813:
| otherwise = (1, rest)
---------------------------- TEST
main :: IO ()
main = do
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}}
<pre>Assuming 24/7:
86400 -> 1 d
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}}==
|