#include #include #include using namespace std; class position_splitter { public: typedef double value_type; position_splitter(const value_type& ms) : time_ms(round(ms < 0 ? 0 : ms)), years(time_ms), milliseconds(calc(years, 1000.0)), seconds(calc(years, 60.0)), minutes(calc(years, 60.0)), hours(calc(years, 24.0)), days(calc(years, 365.0)) { } value_type getMilliseconds() const { return milliseconds; } value_type getSeconds() const { return seconds; } value_type getMinutes() const { return minutes; } value_type getHours() const { return hours; } value_type getDays() const { return days; } value_type getYears() const { return years; } value_type getTotalTime() const { return time_ms; } private: const value_type time_ms; value_type years; const value_type milliseconds; const value_type seconds; const value_type minutes; const value_type hours; const value_type days; value_type calc(value_type& left, const value_type& unit) const { return round(modf(left / unit, &left) * unit); } }; int main() { position_splitter pos(72500); cout.precision(20); cout << pos.getTotalTime() << "ms = "; cout << pos.getYears() << "y : "; cout << pos.getDays() << "d : "; cout << pos.getHours() << "h : "; cout << pos.getMinutes() << "m : "; cout << pos.getSeconds() << "s : "; cout << pos.getMilliseconds() << "ms"; cout << "\n"; }