kplato

kptduration.h
1/* This file is part of the KDE project
2 Copyright (C) 2001 Thomas Zander zander@kde.org
3 Copyright (C) 2004 - 2006 Dag Andersen <danders@get2net.dk>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#ifndef KPTDURATION_H
22#define KPTDURATION_H
23
24#include <tqglobal.h>
25#include <tqstring.h>
26
27namespace KPlato
28{
29
34class Duration {
35 public:
42 enum Format { Format_DayTime, Format_Day, Format_Hour, Format_HourFraction, Format_i18nDayTime, Format_i18nDay, Format_i18nHour, Format_i18nHourFraction };
43
44 Duration();
45 Duration(const Duration &d);
46 Duration(unsigned d, unsigned h, unsigned m, unsigned s=0, unsigned ms=0);
47 Duration(TQ_INT64 seconds);
48 ~Duration();
49
53 void addMilliseconds(TQ_INT64 delta) { add(delta); }
54
58 void addSeconds(TQ_INT64 delta) { addMilliseconds(delta * 1000); }
59
63 void addMinutes(TQ_INT64 delta) { addSeconds(delta * 60); }
64
68 void addHours(TQ_INT64 delta) { addMinutes(delta * 60); }
69
73 void addDays(TQ_INT64 delta) { addHours(delta * 24); }
74
75 //FIXME: overflow problem
76 TQ_INT64 milliseconds() const { return m_ms; }
77 TQ_INT64 seconds() const { return m_ms / 1000; }
78 TQ_INT64 minutes() const { return seconds() / 60; }
79 unsigned hours() const { return minutes() / 60; }
80 unsigned days() const { return hours() / 24; }
81 void get(unsigned *days, unsigned *hours, unsigned *minutes, unsigned *seconds=0, unsigned *milliseconds=0) const;
82
83 bool operator==( const Duration &d ) const { return m_ms == d.m_ms; }
84 bool operator==( TQ_INT64 d ) const { return m_ms == d; }
85 bool operator!=( const Duration &d ) const { return m_ms != d.m_ms; }
86 bool operator!=( TQ_INT64 d ) const { return m_ms != d; }
87 bool operator<( const Duration &d ) const { return m_ms < d.m_ms; }
88 bool operator<( TQ_INT64 d ) const { return m_ms < d; }
89 bool operator<=( const Duration &d ) const { return m_ms <= d.m_ms; }
90 bool operator<=( TQ_INT64 d ) const { return m_ms <= d; }
91 bool operator>( const Duration &d ) const { return m_ms > d.m_ms; }
92 bool operator>( TQ_INT64 d ) const { return m_ms > d; }
93 bool operator>=( const Duration &d ) const { return m_ms >= d.m_ms; }
94 bool operator>=( TQ_INT64 d ) const { return m_ms >= d; }
95 Duration &operator=(const Duration &d ) { m_ms = d.m_ms; return *this;}
96 Duration operator*(int unit) const;
97 Duration operator*(const double value) const;
98 Duration operator/(int unit) const;
99 double operator/(const Duration &d) const;
100
101 Duration operator+(const Duration &d) const
102 {Duration dur(*this); dur.add(d); return dur; }
103 Duration &operator+=(const Duration &d) {add(d); return *this; }
104
105 Duration operator-(const Duration &d) const
106 {Duration dur(*this); dur.subtract(d); return dur; }
107 Duration &operator-=(const Duration &d) {subtract(d); return *this; }
108
109 TQString toString(Format format = Format_DayTime) const;
110 static Duration fromString(const TQString &s, Format format = Format_DayTime, bool *ok=0);
111
112 //NOTE: These must match fieldnumbers in duration widget!
113 enum Unit { Unit_d, Unit_h, Unit_m, Unit_s, Unit_ms };
114 double toDouble(Unit u=Unit_ms) const {
115 if (u == Unit_ms) return (double)m_ms;
116 else if (u == Unit_s) return (double)m_ms/1000.0;
117 else if (u == Unit_m) return (double)m_ms/(1000.0*60.0);
118 else if (u == Unit_h) return (double)m_ms/(1000.0*60.0*60.0);
119 else if (u == Unit_d) return (double)m_ms/(1000.0*60.0*60.0*24.0);
120 return (double)m_ms;
121 }
122
126 static const Duration zeroDuration;
127
128 private:
133 TQ_INT64 m_ms;
134
135 void add(TQ_INT64 delta);
136 void add(const Duration &delta);
137
141 void subtract(const Duration &delta);
142};
143
144} //KPlato namespace
145
146#endif
The duration class can be used to store a timespan in a convenient format.
Definition kptduration.h:34
void addMinutes(TQ_INT64 delta)
Adds.
Definition kptduration.h:63
void addHours(TQ_INT64 delta)
Adds.
Definition kptduration.h:68
static const Duration zeroDuration
This is useful for occasions where we need a zero duration.
Format
DayTime = d hh:mm:ss.sss Day = d.ddd Hour = hh:mm HourFraction = h.fraction of an hour.
Definition kptduration.h:42
void addMilliseconds(TQ_INT64 delta)
Adds.
Definition kptduration.h:53
void addDays(TQ_INT64 delta)
Adds.
Definition kptduration.h:73
void addSeconds(TQ_INT64 delta)
Adds.
Definition kptduration.h:58