1 | /* This file is part of the KDE project |
2 | Copyright (C) 2001 Thomas Zander zander@kde.org |
3 | Copyright (C) 2004 - 2011 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 "kplatokernel_export.h" |
25 | |
26 | #include <QtGlobal> |
27 | #include <QString> |
28 | |
29 | class KLocale; |
30 | |
31 | /// The main namespace. |
32 | namespace KPlato |
33 | { |
34 | |
35 | /** |
36 | * The Duration class can be used to store a timespan in a convenient format. |
37 | * The timespan can be in length in many many hours down to milliseconds. |
38 | */ |
39 | class KPLATOKERNEL_EXPORT Duration |
40 | { |
41 | public: |
42 | /** |
43 | * DayTime = d hh:mm:ss.sss |
44 | * Day = d.ddd |
45 | * Hour = hh:mm |
46 | * HourFraction = h.fraction of an hour |
47 | */ |
48 | enum Format { Format_DayTime, Format_Year, Format_Month, Format_Week, Format_Day, Format_Hour, Format_HourFraction, Format_i18nDayTime, Format_i18nYear, Format_i18nMonth, Format_i18nWeek, Format_i18nDay, Format_i18nHour, Format_i18nHourFraction }; |
49 | |
50 | //NOTE: These must match units in DurationSpinBox! |
51 | enum Unit { Unit_Y, Unit_M, Unit_w, Unit_d, Unit_h, Unit_m, Unit_s, Unit_ms }; |
52 | |
53 | /// Create a zero duration |
54 | Duration(); |
55 | /// Create a duration of @p value, the value is in @p unit (defaut unit is milliseconds) |
56 | explicit Duration(const qint64 value, Unit unit = Unit_ms); |
57 | /// Create a duration of @p value, the value is in @p unit (default is hours) |
58 | explicit Duration(double value, Unit unit = Unit_h); |
59 | /// Create a duration of @p d days, @p h hours, @p m minutes, @p s seconds and @p ms milliseconds |
60 | Duration(unsigned d, unsigned h, unsigned m, unsigned s=0, unsigned ms=0); |
61 | |
62 | /// Return duration in milliseconds |
63 | qint64 milliseconds() const { return m_ms; } |
64 | /// Return duration in whole seconds |
65 | qint64 seconds() const { return m_ms / 1000; } |
66 | /// Return duration in whole minutes |
67 | qint64 minutes() const { return seconds() / 60; } |
68 | /// Return duration in whole hours |
69 | unsigned hours() const { return minutes() / 60; } |
70 | /// Return duration in whole days |
71 | unsigned days() const { return hours() / 24; } |
72 | |
73 | /** |
74 | * Adds @p delta to *this. If @p delta > *this, *this is set to zeroDuration. |
75 | */ |
76 | void addMilliseconds(qint64 delta) { add(delta); } |
77 | |
78 | /** |
79 | * Adds @p delta to *this. If @p delta > *this, *this is set to zeroDuration. |
80 | */ |
81 | void addSeconds(qint64 delta) { addMilliseconds(delta * 1000); } |
82 | |
83 | /** |
84 | * Adds @p delta to *this. If @p delta > *this, *this is set to zeroDuration. |
85 | */ |
86 | void addMinutes(qint64 delta) { addSeconds(delta * 60); } |
87 | |
88 | /** |
89 | * Adds @p delta to *this. If @p delta > *this, *this is set to zeroDuration. |
90 | */ |
91 | void addHours(qint64 delta) { addMinutes(delta * 60); } |
92 | |
93 | /** |
94 | * Adds @p delta to *this. If @p delta > *this, *this is set to zeroDuration. |
95 | */ |
96 | void addDays(qint64 delta) { addHours(delta * 24); } |
97 | |
98 | bool operator==( const Duration &d ) const { return m_ms == d.m_ms; } |
99 | bool operator==( qint64 d ) const { return m_ms == d; } |
100 | bool operator!=( const Duration &d ) const { return m_ms != d.m_ms; } |
101 | bool operator!=( qint64 d ) const { return m_ms != d; } |
102 | bool operator<( const Duration &d ) const { return m_ms < d.m_ms; } |
103 | bool operator<( qint64 d ) const { return m_ms < d; } |
104 | bool operator<=( const Duration &d ) const { return m_ms <= d.m_ms; } |
105 | bool operator<=( qint64 d ) const { return m_ms <= d; } |
106 | bool operator>( const Duration &d ) const { return m_ms > d.m_ms; } |
107 | bool operator>( qint64 d ) const { return m_ms > d; } |
108 | bool operator>=( const Duration &d ) const { return m_ms >= d.m_ms; } |
109 | bool operator>=( qint64 d ) const { return m_ms >= d; } |
110 | Duration &operator=(const Duration &d ) { m_ms = d.m_ms; return *this;} |
111 | Duration operator*(int value) const; |
112 | Duration operator*(const double value) const; |
113 | Duration operator*(const Duration value) const; |
114 | /// Divide duration with the integer @p value |
115 | Duration operator/(int value) const; |
116 | /// Divide duration with the duration @p d |
117 | double operator/(const Duration &d) const; |
118 | /// Add duration with duration @p d |
119 | Duration operator+(const Duration &d) const |
120 | {Duration dur(*this); dur.add(d); return dur; } |
121 | /// Add duration with duration @p d |
122 | Duration &operator+=(const Duration &d) {add(d); return *this; } |
123 | /// Subtract duration with duration @p d |
124 | Duration operator-(const Duration &d) const |
125 | {Duration dur(*this); dur.subtract(d); return dur; } |
126 | /// Subtract duration with duration @p d |
127 | Duration &operator-=(const Duration &d) {subtract(d); return *this; } |
128 | |
129 | /// Format duration into a string with @p unit and @p presition using @p locale. If @p locale == 0, uses KGLobal::locale. |
130 | QString format( Unit unit = Unit_h, int presition = 1, const KLocale *locale = 0 ) const; |
131 | /// Convert duration to a string with @p format |
132 | QString toString(Format format = Format_DayTime) const; |
133 | /// Create a duration from string @p s with @p format |
134 | static Duration fromString(const QString &s, Format format = Format_DayTime, bool *ok=0); |
135 | |
136 | /// Return the duration scaled to hours |
137 | double toHours() const; |
138 | /** |
139 | * Converts Duration into a double and scales it to unit @p u (default unit is hours) |
140 | */ |
141 | double toDouble( Unit u = Unit_h ) const; |
142 | |
143 | /// Return the list of units. Translated if @p trans is true. |
144 | static QStringList unitList( bool trans = false ); |
145 | /// Return @p unit in human readable form. Translated if @p trans is true. |
146 | static QString unitToString( Duration::Unit unit, bool trans = false ); |
147 | /// Convert @p unit name into Unit |
148 | static Unit unitFromString( const QString &unit ); |
149 | /// Returns value and unit from a <value><unit> coded string in @p rv and @p unit. |
150 | static bool valueFromString( const QString &value, double &rv, Unit &unit ); |
151 | /** |
152 | * This is useful for occasions where we need a zero duration. |
153 | */ |
154 | static const Duration zeroDuration; |
155 | |
156 | private: |
157 | friend class DateTime; |
158 | /** |
159 | * Duration in milliseconds. Signed to allow for simple calculations which |
160 | * might go negative for intermediate results. |
161 | */ |
162 | qint64 m_ms; |
163 | |
164 | private: |
165 | void add(qint64 delta); |
166 | void add(const Duration &delta); |
167 | |
168 | /** |
169 | * Subtracts @param delta from *this. If @param delta > *this, *this is set to zeroDuration. |
170 | */ |
171 | void subtract(const Duration &delta); |
172 | }; |
173 | |
174 | } //KPlato namespace |
175 | |
176 | #endif |
177 | |