/****************************************************************************** * This program test the Duration class. * * Copyright © 2021 Richard Lesh. All rights reserved. *****************************************************************************/ #include "Utils.hpp" #include #include #include #include #include #include #include std::locale utf8loc(std::locale(), new std::codecvt_utf8); using namespace std; class Duration { private: // Internal representation is in seconds double value; public: enum class Unit { ns, μs, ms, sec, min, hour, day, week, month, year }; // Constructor Duration() noexcept { value = 0; } // getter double getValue(Unit const unit) noexcept { double result = 0.; switch (unit) { case Unit::ns: result = value * 1e9; break; case Unit::μs: result = value * 1e6; break; case Unit::ms: result = value * 1e3; break; case Unit::sec: result = value; break; case Unit::min: result = value / 60.; break; case Unit::hour: result = value / 60. / 60.; break; case Unit::day: result = value / 60. / 60. / 24.; break; case Unit::week: result = value / 60. / 60. / 24. / 7.; break; case Unit::month: result = value / 60. / 60. / 24. / 30.4375; break; case Unit::year: result = value / 60. / 60. / 24. / 365.25; break; } return result; } // setter void setValue(double value, Unit const unit) noexcept { double result = 0.; switch (unit) { case Unit::ns: result = value / 1e9; break; case Unit::μs: result = value / 1e6; break; case Unit::ms: result = value / 1e3; break; case Unit::sec: result = value; break; case Unit::min: result = value * 60.; break; case Unit::hour: result = value * 60. * 60.; break; case Unit::day: result = value * 60. * 60. * 24.; break; case Unit::week: result = value * 60. * 60. * 24. * 7.; break; case Unit::month: result = value * 60. * 60. * 24. * 30.4375; break; case Unit::year: result = value * 60. * 60. * 24. * 365.25; break; } this->value = result; } }; int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); shared_ptr d(new Duration()); d->setValue(1.23, Duration::Unit::sec); wcout << fmt::format(L"{0:g} ns", d->getValue(Duration::Unit::ns)) << endl; wcout << fmt::format(L"{0:g} µs", d->getValue(Duration::Unit::μs)) << endl; wcout << fmt::format(L"{0:g} ms", d->getValue(Duration::Unit::ms)) << endl; wcout << fmt::format(L"{0:g} sec", d->getValue(Duration::Unit::sec)) << endl; wcout << fmt::format(L"{0:g} min", d->getValue(Duration::Unit::min)) << endl; wcout << fmt::format(L"{0:g} hour", d->getValue(Duration::Unit::hour)) << endl; wcout << fmt::format(L"{0:g} day", d->getValue(Duration::Unit::day)) << endl; wcout << fmt::format(L"{0:g} week", d->getValue(Duration::Unit::week)) << endl; wcout << fmt::format(L"{0:g} month", d->getValue(Duration::Unit::month)) << endl; wcout << fmt::format(L"{0:g} year", d->getValue(Duration::Unit::year)) << endl; wcout << endl; d->setValue(1., Duration::Unit::year); wcout << fmt::format(L"{0:g} ns", d->getValue(Duration::Unit::ns)) << endl; wcout << fmt::format(L"{0:g} µs", d->getValue(Duration::Unit::μs)) << endl; wcout << fmt::format(L"{0:g} ms", d->getValue(Duration::Unit::ms)) << endl; wcout << fmt::format(L"{0:g} sec", d->getValue(Duration::Unit::sec)) << endl; wcout << fmt::format(L"{0:g} min", d->getValue(Duration::Unit::min)) << endl; wcout << fmt::format(L"{0:g} hour", d->getValue(Duration::Unit::hour)) << endl; wcout << fmt::format(L"{0:g} day", d->getValue(Duration::Unit::day)) << endl; wcout << fmt::format(L"{0:g} week", d->getValue(Duration::Unit::week)) << endl; wcout << fmt::format(L"{0:g} month", d->getValue(Duration::Unit::month)) << endl; wcout << fmt::format(L"{0:g} year", d->getValue(Duration::Unit::year)) << endl; wcout << endl; d->setValue(1., Duration::Unit::ns); wcout << fmt::format(L"{0:g} ns", d->getValue(Duration::Unit::ns)) << endl; wcout << fmt::format(L"{0:g} µs", d->getValue(Duration::Unit::μs)) << endl; wcout << fmt::format(L"{0:g} ms", d->getValue(Duration::Unit::ms)) << endl; wcout << fmt::format(L"{0:g} sec", d->getValue(Duration::Unit::sec)) << endl; wcout << fmt::format(L"{0:g} min", d->getValue(Duration::Unit::min)) << endl; wcout << fmt::format(L"{0:g} hour", d->getValue(Duration::Unit::hour)) << endl; wcout << fmt::format(L"{0:g} day", d->getValue(Duration::Unit::day)) << endl; wcout << fmt::format(L"{0:g} week", d->getValue(Duration::Unit::week)) << endl; wcout << fmt::format(L"{0:g} month", d->getValue(Duration::Unit::month)) << endl; wcout << fmt::format(L"{0:g} year", d->getValue(Duration::Unit::year)) << endl; wcout << endl; return 0; }