21#include "kptduration.h"
22#include "kptdatetime.h"
39Duration::Duration(
const Duration &d) {
43Duration::Duration(
unsigned d,
unsigned h,
unsigned m,
unsigned s,
unsigned ms) {
45 m_ms +=
static_cast<TQ_INT64
>(s) * 1000;
46 m_ms +=
static_cast<TQ_INT64
>(m) * 60 * 1000;
47 m_ms +=
static_cast<TQ_INT64
>(h) * 60 * 60 * 1000;
48 m_ms +=
static_cast<TQ_INT64
>(d) * 24 * 60 * 60 * 1000;
51Duration::Duration(TQ_INT64 seconds) {
52 m_ms = seconds * 1000;
55Duration::~Duration() {
58void Duration::add(
const Duration &delta) {
62void Duration::add(TQ_INT64 delta) {
63 TQ_INT64 tmp = m_ms + delta;
65 kdDebug()<<k_funcinfo<<
"Underflow"<<(
long int)delta<<
" from "<<this->toString()<<endl;
72void Duration::subtract(
const Duration &delta) {
73 if (m_ms < delta.m_ms) {
74 kdDebug()<<k_funcinfo<<
"Underflow"<<delta.toString()<<
" from "<<this->toString()<<endl;
81Duration Duration::operator*(
int unit)
const {
84 kdDebug()<<k_funcinfo<<
"Underflow"<<unit<<
" from "<<this->toString()<<endl;
87 dur.m_ms = m_ms * unit;
92Duration Duration::operator/(
int unit)
const {
95 kdDebug()<<k_funcinfo<<
"Underflow"<<unit<<
" from "<<this->toString()<<endl;
98 dur.m_ms = m_ms / unit;
103Duration Duration::operator*(
const double value)
const {
105 dur.m_ms = TQABS(m_ms * (TQ_INT64)value);
109double Duration::operator/(
const Duration &d)
const {
111 kdDebug()<<k_funcinfo<<
"Devide by zero: "<<this->toString()<<endl;
114 return (
double)(m_ms) / (
double)(d.m_ms);
117TQString Duration::toString(Format format)
const {
129 hours = ms / (1000 * 60 * 60);
130 ms -= (TQ_INT64)hours * (1000 * 60 * 60);
131 minutes = ms / (1000 * 60);
132 result = TQString(
"%1h%2m").arg(hours).arg(minutes);
135 days = m_ms / (1000 * 60 * 60 * 24.0);
136 result = TQString(
"%1d").arg(TQString::number(days,
'f', 4));
140 days = m_ms / (1000 * 60 * 60 * 24);
141 ms -= (TQ_INT64)days * (1000 * 60 * 60 * 24);
142 hours = ms / (1000 * 60 * 60);
143 ms -= (TQ_INT64)hours * (1000 * 60 * 60);
144 minutes = ms / (1000 * 60);
145 ms -= minutes * (1000 * 60);
146 seconds = ms / (1000);
147 ms -= seconds * (1000);
148 result.sprintf(
"%u %02u:%02u:%02u.%u", (
unsigned)days, hours, minutes, seconds, (
unsigned)ms);
150 case Format_HourFraction:
151 result = TDEGlobal::locale()->formatNumber(toDouble(Unit_h), 2);
154 case Format_i18nHour:
156 hours = ms / (1000 * 60 * 60);
157 ms -= (TQ_INT64)hours * (1000 * 60 * 60);
158 minutes = ms / (1000 * 60);
159 result = i18n(
"<hours>h:<minutes>m",
"%1h:%2m").arg(hours).arg(minutes);
162 result = TDEGlobal::locale()->formatNumber(toDouble(Unit_d), 2);
164 case Format_i18nDayTime:
166 days = m_ms / (1000 * 60 * 60 * 24);
167 ms -= (TQ_INT64)days * (1000 * 60 * 60 * 24);
168 hours = ms / (1000 * 60 * 60);
169 ms -= (TQ_INT64)hours * (1000 * 60 * 60);
170 minutes = ms / (1000 * 60);
171 ms -= minutes * (1000 * 60);
172 seconds = ms / (1000);
173 ms -= seconds * (1000);
175 result = toString(Format_i18nHour);
177 result = i18n(
"<days>d <hours>h:<minutes>m",
"%1d %2h:%3m").arg(days).arg(hours).arg(minutes);
180 case Format_i18nHourFraction:
181 result = TDEGlobal::locale()->formatNumber(toDouble(Unit_h), 2);
184 kdFatal()<<k_funcinfo<<
"Unknown format"<<endl;
190Duration Duration::fromString(
const TQString &s, Format format,
bool *ok) {
196 matcher.setPattern(
"^(\\d*)h(\\d*)m$" );
197 int pos = matcher.search(s);
199 tmp.addHours(matcher.cap(1).toUInt());
200 tmp.addMinutes(matcher.cap(2).toUInt());
205 case Format_DayTime: {
206 matcher.setPattern(
"^(\\d*) (\\d*):(\\d*):(\\d*)\\.(\\d*)$" );
207 int pos = matcher.search(s);
209 tmp.addDays(matcher.cap(1).toUInt());
210 tmp.addHours(matcher.cap(2).toUInt());
211 tmp.addMinutes(matcher.cap(3).toUInt());
212 tmp.addSeconds(matcher.cap(4).toUInt());
213 tmp.addMilliseconds(matcher.cap(5).toUInt());
218 case Format_HourFraction: {
221 double f = TDEGlobal::locale()->readNumber(s, &res);
224 return Duration((TQ_INT64)(f*3600.0));
229 kdFatal()<<k_funcinfo<<
"Unknown format"<<endl;
235void Duration::get(
unsigned *days,
unsigned *hours,
unsigned *minutes,
unsigned *seconds,
unsigned *milliseconds)
const {
240 tmp = ms / (1000 * 60 * 60 * 24);
242 ms -= tmp * (1000 * 60 * 60 * 24);
243 tmp = ms / (1000 * 60 * 60);
245 ms -= tmp * (1000 * 60 * 60);
246 tmp = ms / (1000 * 60);
248 ms -= tmp * (1000 * 60);
The duration class can be used to store a timespan in a convenient format.
static const Duration zeroDuration
This is useful for occasions where we need a zero duration.