Task

Write a function or program which:

   takes a positive integer representing a duration in seconds as input (e.g., 100), and
   returns a string which shows the same duration decomposed into:
   weeks,
   days, 
   hours, 
   minutes,   and 
   seconds.
This is detailed below (e.g., "2 hr, 59 sec").



Demonstrate that it passes the following three test-cases:

Test Cases


 input number

 output string


 7259

 2 hr, 59 sec


 86400

 1 d


 6000000

 9 wk, 6 d, 10 hr, 40 min

Details
The following five units should be used:



 unit

 suffix used in output

 conversion


 week

 wk

 1 week = 7 days


 day

 d

 1 day = 24 hours


 hour

 hr

 1 hour = 60 minutes


 minute

 min

 1 minute = 60 seconds


 second

 sec



However, only include quantities with non-zero values in the output (e.g., return "1 d" and not "0 wk, 1 d, 0 hr, 0 min, 0 sec").

Give larger units precedence over smaller ones as much as possible (e.g., return 2 min, 10 sec and not 1 min, 70 sec or 130 sec)

Mimic the formatting shown in the test-cases (quantities sorted from largest unit to smallest and separated by comma+space; value and unit of each quantity separated by space).




 
      SUBROUTINE PROUST(T)	!Remembrance of time passed.
       INTEGER T		!The time, in seconds. Positive only, please.
       INTEGER NTYPES		!How many types of time?
       PARAMETER (NTYPES = 5)	!This should do.
       INTEGER USIZE(NTYPES)	!Size of the time unit.
       CHARACTER*3 UNAME(NTYPES)!Name of the time unit.
       PARAMETER (USIZE = (/7*24*60*60, 24*60*60, 60*60,   60,    1/))	!The compiler does some arithmetic.
       PARAMETER (UNAME = (/      "wk",      "d",  "hr","min","sec"/))	!Approved names, with trailing spaces.
       CHARACTER*28 TEXT	!A scratchpad.
       INTEGER I,L,N,S		!Assistants.
        S = T			!A copy I can mess with.
        L = 0			!No text has been generated.
        DO I = 1,NTYPES		!Step through the types to do so.
          N = S/USIZE(I)	!Largest first.
          IF (N.GT.0) THEN	!Above the waterline?
            S = S - N*USIZE(I)		!Yes! Remove its contribution.
            IF (L.GT.0) THEN		!Is this the first text to be rolled?
              L = L + 2				!No.
              TEXT(L - 1:L) = ", "		!Cough forth some punctuation.
            END IF			!Now ready for this count.
            WRITE (TEXT(L + 1:),1) N,UNAME(I)	!Place, with the unit name.
    1       FORMAT (I0,1X,A)		!I0 means I only: variable-length, no leading spaces.
            L = LEN_TRIM(TEXT)		!Find the last non-blank resulting.
          END IF			!Since I'm not keeping track.
        END DO			!On to the next unit.
Cast forth the result.
        WRITE (6,*) T,">",TEXT(1:L),"<"	!With annotation.
       END			!Simple enough with integers.
 
       PROGRAM MARCEL		!Stir the cup.
       CALL PROUST(7259)
       CALL PROUST(7260)
       CALL PROUST(86400)
       CALL PROUST(6000000)
       CALL PROUST(0)
       CALL PROUST(-666)
       END
 