2 Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "metric_time.h"
21 #include "compose.hpp"
30 MetricTime::MetricTime (int h, int m, int s, int ms)
31 /* cast up to int64_t to force a 64-bit calculation */
32 : _ms ((int64_t (h) * 3600 + m * 60 + s) * 1000 + ms)
38 MetricTime::split (int& h, int &m, int& s, int& ms) const
41 h = floor (w / (3600 * 1000));
42 /* this multiply could overflow 32 bits so cast to make sure it is done as 64-bit */
43 w -= int64_t (h) * (3600 * 1000);
44 m = floor (w / (60 * 1000));
52 MetricTime::hours () const
60 MetricTime::minutes () const
68 MetricTime::seconds () const
76 MetricTime::milliseconds () const
84 MetricTime::add (MetricTime t)
90 MetricTime::scale (float f)
96 sub::operator== (MetricTime const & a, MetricTime const & b)
98 return a._ms == b._ms;
102 sub::operator> (MetricTime const & a, MetricTime const & b)
104 return a._ms > b._ms;
108 sub::operator< (MetricTime const & a, MetricTime const & b)
110 return a._ms < b._ms;
114 sub::operator<< (ostream& st, MetricTime const & t)
117 t.split (h, m, s, ms);
118 st << h << ":" << m << ":" << s << ":" << ms;