---
# System prepended metadata

title: 'Human readable duration format [4 kyu]'
tags: [C++, CodeWars]

---

# Human readable duration format [4 kyu]

[Human readable duration format](https://www.codewars.com/kata/52742f58faf5485cae000b9a)
4 kyu

## Solution

```cpp=
/**
*** Author: R-CO
*** E-Mail: daniel1820kobe@gmail.com
*** Date: 2020-08-10
**/
#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;
#include <string>

struct TestCaseStruct {
  int input;
  std::string expected_output;
};

struct Data {
  int seconds{0};
  int count{0};
  std::string label;
};

class Solution {
 public:
  std::string format_duration(int seconds) {
    if (seconds == 0) {
      return std::string("now");
    }

    // enum class TimeUnit : size_t {year = 0, day, hour, minute, second};
    enum TimeUnit { kYear = 0, kDay, kHour, kMinute, kSecond };
    Data data[kSecond + 1];
    data[kSecond].seconds = 1;
    data[kSecond].label = "second";
    data[kMinute].seconds = data[kSecond].seconds * 60;
    data[kMinute].label = "minute";
    data[kHour].seconds = data[kMinute].seconds * 60;
    data[kHour].label = "hour";
    data[kDay].seconds = data[kHour].seconds * 24;
    data[kDay].label = "day";
    data[kYear].seconds = data[kDay].seconds * 365;
    data[kYear].label = "year";

    for (size_t i = 0; i <= kSecond; ++i) {
      data[i].count = seconds / data[i].seconds;
      seconds %= data[i].seconds;
    }

    int count = 0;
    for (const auto& it : data) {
      if (it.count > 0) {
        ++count;
      }
    }

    std::string output;
    for (const auto& it : data) {
      if (it.count > 0) {
        output += std::to_string(it.count) + " " + it.label;
        if (it.count > 1) {
          output += "s";
        }

        if (count >= 3) {
          output += ", ";
        } else if (count == 2) {
          output += " and ";
        }
        --count;
      }
    }

    return output;
  }
};

int main(int argc, char* argv[]) {
  /*
    It(tests) {
                        Assert::That(format_duration(0), Equals("now"));
                        Assert::That(format_duration(1), Equals("1 second"));
                        Assert::That(format_duration(62), Equals("1 minute and 2
    seconds")); Assert::That(format_duration(120), Equals("2 minutes"));
                        Assert::That(format_duration(3662), Equals("1 hour, 1
    minute and 2 seconds"));
                }
   */
  TestCaseStruct test_cases[] = {{0, "now"},
                                 {1, "1 second"},
                                 {62, "1 minute and 2 seconds"},
                                 {120, "2 minutes"},
                                 {3662, "1 hour, 1 minute and 2 seconds"}};

  Solution solution;
  for (const auto& test_case : test_cases) {
    if (solution.format_duration(test_case.input) ==
        test_case.expected_output) {
      cout << "test case \"" << test_case.input << "\" is pass." << std::endl;
    } else {
      cout << "test case \"" << test_case.input << "\" is fail." << std::endl;
    }
  }

  return EXIT_SUCCESS;
}

```

## Result
![](https://i.imgur.com/sXJVc8t.png)


###### tags: `CodeWars` `C++`
