Task: Convert seconds to compound duration


C++: #include <iostream>
#include <vector>

using entry = std::pair<int, const char*>;

void print(const std::vector<entry>& entries, std::ostream& out = std::cout)
{
    bool first = true;
    for(const auto& e: entries) {
        if(!first) out << ", ";
        first = false;
        out << e.first << " " << e.second;
    }
    out << '\n';
}

std::vector<entry> convert(int seconds)
{
    static const entry time_table[] = {
        {7*24*60*60, "wk"}, {24*60*60, "d"}, {60*60, "hr"}, {60, "min"}, {1, "sec"}
    };
    std::vector<entry> result;
    for(const auto& e: time_table) {
        int time = seconds / e.first;
        if(time != 0) result.emplace_back(time, e.second);
        seconds %= e.first;
    }
    return result;
}

int main()
{
    std::cout << "   7259 sec is "; print(convert(   7259));
    std::cout << "  86400 sec is "; print(convert(  86400));
    std::cout << "6000000 sec is "; print(convert(6000000));
}








COBOL:        identification division.
       program-id. fmt-dura.
       data division.
       working-storage section.
       1 input-seconds pic 9(8).
       1 formatted-duration pic x(30) global.
       1 fractions.
        2 weeks pic z(3)9.
        2 days pic z(3)9.
        2 hours pic z(3)9.
        2 minutes pic z(3)9.
        2 seconds pic z(3)9.
       1 .
        2 weeks-str pic x(4) value "wk".
        2 days-str pic x(4) value "d".
        2 hours-str pic x(4) value "hr".
        2 minutes-str pic x(4) value "min".
        2 seconds-str pic x(4) value "sec".
       1 work binary global.
        2 str-pos pic 9(4).
        2 chars-transferred pic 9(4).
       procedure division.
       begin.
           display "Enter duration (seconds): " no advancing
           accept input-seconds
           divide input-seconds by 60 giving input-seconds
               remainder seconds
           divide input-seconds by 60 giving input-seconds
               remainder minutes
           divide input-seconds by 24 giving input-seconds
               remainder hours
           divide input-seconds by 7 giving weeks
               remainder days
           move 1 to str-pos
           call "fmt" using weeks weeks-str
           call "fmt" using days days-str
           call "fmt" using hours hours-str
           call "fmt" using minutes minutes-str
           call "fmt" using seconds seconds-str
           display formatted-duration
           stop run
           .

       identification division.
       program-id. fmt.
       data division.
       working-storage section.
       77 nothing pic x.
       linkage section.
       1 formatted-value pic x(4).
       1 duration-size pic x(4).
       procedure division using formatted-value duration-size.
       begin.
           if function numval (formatted-value) not = 0
               perform insert-comma-space
               unstring formatted-value delimited all space
                   into nothing formatted-duration (str-pos:)
                   count chars-transferred
               add chars-transferred to str-pos
               string space delimited size
                   duration-size delimited space
                   into formatted-duration pointer str-pos
           end-if
           exit program
           .

       insert-comma-space.
           if str-pos > 1
               move ", " to formatted-duration (str-pos:)
               add 2 to str-pos
           end-if
           .
       end program fmt.
       end program fmt-dura.