summaryrefslogtreecommitdiffstats
path: root/kplato/kptduration.cpp
blob: 87e0e54a0c09a631d5a4de3e78a8879afb3ed09a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* This file is part of the KDE project
   Copyright (C) 2001 Thomas Zander [email protected]
   Copyright (C) 2004, 2005 Dag Andersen <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "kptduration.h"
#include "kptdatetime.h"

#include <tdeglobal.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <tqregexp.h>

namespace KPlato
{

// Set the value of Duration::zeroDuration to zero.
const Duration Duration::zeroDuration( 0, 0, 0 );

Duration::Duration() {
    m_ms = 0;
}

Duration::Duration(const Duration &d) {
    m_ms = d.m_ms;
}

Duration::Duration(unsigned d, unsigned h, unsigned m, unsigned s, unsigned ms) {
    m_ms = ms;
    m_ms += static_cast<TQ_INT64>(s) * 1000; // cast to avoid potential overflow problem
    m_ms += static_cast<TQ_INT64>(m) * 60 * 1000;
    m_ms += static_cast<TQ_INT64>(h) * 60 * 60 * 1000;
    m_ms += static_cast<TQ_INT64>(d) * 24 * 60 * 60 * 1000;
}

Duration::Duration(TQ_INT64 seconds) {
    m_ms = seconds * 1000;
}

Duration::~Duration() {
}

void Duration::add(const Duration &delta) {
    m_ms += delta.m_ms;
}

void Duration::add(TQ_INT64 delta) {
    TQ_INT64 tmp = m_ms + delta;
    if (tmp < 0) {
        kdDebug()<<k_funcinfo<<"Underflow"<<(long int)delta<<" from "<<this->toString()<<endl;
        m_ms = 0;
        return;
    }
    m_ms = tmp;
}

void Duration::subtract(const Duration &delta) {
    if (m_ms < delta.m_ms) {
        kdDebug()<<k_funcinfo<<"Underflow"<<delta.toString()<<" from "<<this->toString()<<endl;
        m_ms = 0;
        return;
    }
    m_ms -= delta.m_ms;
}

Duration Duration::operator*(int unit) const {
    Duration dur(*this);
    if (unit < 0) {
        kdDebug()<<k_funcinfo<<"Underflow"<<unit<<" from "<<this->toString()<<endl;
    }
    else {
        dur.m_ms = m_ms * unit; //FIXME
    }
    return dur;
}

Duration Duration::operator/(int unit) const {
    Duration dur(*this);
    if (unit <= 0) {
        kdDebug()<<k_funcinfo<<"Underflow"<<unit<<" from "<<this->toString()<<endl;
    }
    else {
        dur.m_ms = m_ms / unit; //FIXME
    }
    return dur;
}

Duration Duration::operator*(const double value) const {
    Duration dur(*this);
    dur.m_ms = TQABS(m_ms * (TQ_INT64)value);
    return dur;
}

double Duration::operator/(const Duration &d) const {
    if (d == zeroDuration) {
        kdDebug()<<k_funcinfo<<"Devide by zero: "<<this->toString()<<endl;
        return 0.0;
    }
    return (double)(m_ms) / (double)(d.m_ms);
}

TQString Duration::toString(Format format) const {
    TQ_INT64 ms;
    double days;
    unsigned hours;
    unsigned minutes;
    unsigned seconds;
    double f;
    TQString result;

    switch (format) {
        case Format_Hour:
            ms = m_ms;
            hours = ms / (1000 * 60 * 60);
            ms -= (TQ_INT64)hours * (1000 * 60 * 60);
            minutes = ms / (1000 * 60);
            result = TQString("%1h%2m").arg(hours).arg(minutes);
            break;
        case Format_Day:
            days = m_ms / (1000 * 60 * 60 * 24.0);
            result = TQString("%1d").arg(TQString::number(days, 'f', 4));
            break;
        case Format_DayTime:
            ms = m_ms;
            days = m_ms / (1000 * 60 * 60 * 24);
            ms -= (TQ_INT64)days * (1000 * 60 * 60 * 24);
            hours = ms / (1000 * 60 * 60);
            ms -= (TQ_INT64)hours * (1000 * 60 * 60);
            minutes = ms / (1000 * 60);
            ms -= minutes * (1000 * 60);
            seconds = ms / (1000);
            ms -= seconds * (1000);
            result.sprintf("%u %02u:%02u:%02u.%u", (unsigned)days, hours, minutes, seconds, (unsigned)ms);
            break;
        case Format_HourFraction:
            result = TDEGlobal::locale()->formatNumber(toDouble(Unit_h), 2);
            break;
        // i18n
        case Format_i18nHour:
            ms = m_ms;
            hours = ms / (1000 * 60 * 60);
            ms -= (TQ_INT64)hours * (1000 * 60 * 60);
            minutes = ms / (1000 * 60);
            result = i18n("<hours>h:<minutes>m", "%1h:%2m").arg(hours).arg(minutes);
            break;
        case Format_i18nDay:
            result = TDEGlobal::locale()->formatNumber(toDouble(Unit_d), 2);
            break;
        case Format_i18nDayTime:
            ms = m_ms;
            days = m_ms / (1000 * 60 * 60 * 24);
            ms -= (TQ_INT64)days * (1000 * 60 * 60 * 24);
            hours = ms / (1000 * 60 * 60);
            ms -= (TQ_INT64)hours * (1000 * 60 * 60);
            minutes = ms / (1000 * 60);
            ms -= minutes * (1000 * 60);
            seconds = ms / (1000);
            ms -= seconds * (1000);
            if (days == 0) {
                result = toString(Format_i18nHour);
            } else {
                result = i18n("<days>d <hours>h:<minutes>m", "%1d %2h:%3m").arg(days).arg(hours).arg(minutes);
            }
            break;
        case Format_i18nHourFraction:
            result = TDEGlobal::locale()->formatNumber(toDouble(Unit_h), 2);
            break;
        default:
            kdFatal()<<k_funcinfo<<"Unknown format"<<endl;
            break;
    }
    return result;
}

Duration Duration::fromString(const TQString &s, Format format, bool *ok) {
    if (ok) *ok = false;
    TQRegExp matcher;
    Duration tmp;
    switch (format) {
        case Format_Hour: {
            matcher.setPattern("^(\\d*)h(\\d*)m$" );
            int pos = matcher.search(s);
            if (pos > -1) {
                tmp.addHours(matcher.cap(1).toUInt());
                tmp.addMinutes(matcher.cap(2).toUInt());
                if (ok) *ok = true;
            }
            break;
        }
        case Format_DayTime: {
            matcher.setPattern("^(\\d*) (\\d*):(\\d*):(\\d*)\\.(\\d*)$" );
            int pos = matcher.search(s);
            if (pos > -1) {
                tmp.addDays(matcher.cap(1).toUInt());
                tmp.addHours(matcher.cap(2).toUInt());
                tmp.addMinutes(matcher.cap(3).toUInt());
                tmp.addSeconds(matcher.cap(4).toUInt());
                tmp.addMilliseconds(matcher.cap(5).toUInt());
                if (ok) *ok = true;
            }
            break;
        }
        case Format_HourFraction: {
            // should be in double format
            bool res;
            double f = TDEGlobal::locale()->readNumber(s, &res);
            if (ok) *ok = res;
            if (res) {
                return Duration((TQ_INT64)(f*3600.0));
            }
            break;
        }
        default:
            kdFatal()<<k_funcinfo<<"Unknown format"<<endl;
            break;
    }
    return tmp;
}

void Duration::get(unsigned *days, unsigned *hours, unsigned *minutes, unsigned *seconds, unsigned *milliseconds) const {
    TQ_INT64 ms;
    TQ_INT64 tmp;

    ms = m_ms;
    tmp = ms / (1000 * 60 * 60 * 24);
    *days = tmp;
    ms -= tmp * (1000 * 60 * 60 * 24);
    tmp = ms / (1000 * 60 * 60);
    *hours = tmp;
    ms -= tmp * (1000 * 60 * 60);
    tmp = ms / (1000 * 60);
    *minutes = tmp;
    ms -= tmp * (1000 * 60);
    tmp = ms / (1000);
    if (seconds)
        *seconds = tmp;
    ms -= tmp * (1000);
    if (milliseconds)
        *milliseconds = ms;
}

}  //KPlato namespace