F:/KPlato/koffice/kplato/kptduration.h

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Thomas Zander zander@kde.org
00003    Copyright (C) 2004 - 2006 Dag Andersen <danders@get2net.dk>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #ifndef KPTDURATION_H
00022 #define KPTDURATION_H
00023 
00024 #include <qglobal.h>
00025 #include <QString>
00026 
00027 namespace KPlato
00028 {
00029 
00034 class Duration {
00035     public:
00042         enum Format { Format_DayTime, Format_Day, Format_Hour, Format_HourFraction, Format_i18nDayTime, Format_i18nDay, Format_i18nHour, Format_i18nHourFraction };
00043 
00044         Duration();
00045         Duration(const Duration &d);
00046         Duration(unsigned d, unsigned h, unsigned m, unsigned s=0, unsigned ms=0);
00047         Duration(qint64 seconds);
00048         ~Duration();
00049 
00053         void addMilliseconds(qint64 delta)  { add(delta); }
00054 
00058         void addSeconds(qint64 delta) { addMilliseconds(delta * 1000); }
00059 
00063         void addMinutes(qint64 delta) { addSeconds(delta * 60); }
00064 
00068         void addHours(qint64 delta) { addMinutes(delta * 60); }
00069 
00073         void addDays(qint64 delta) { addHours(delta * 24); }
00074 
00075         //FIXME: overflow problem
00076         qint64 milliseconds() const { return m_ms; }
00077         qint64 seconds() const { return m_ms / 1000; }
00078         qint64 minutes() const { return seconds() / 60; }
00079         unsigned hours() const { return minutes() / 60; }
00080         unsigned days() const { return hours() / 24; }
00081         void get(unsigned *days, unsigned *hours, unsigned *minutes, unsigned *seconds=0, unsigned *milliseconds=0) const;
00082 
00083         bool   operator==( const Duration &d ) const { return m_ms == d.m_ms; }
00084         bool   operator==( qint64 d ) const { return m_ms == d; }
00085         bool   operator!=( const Duration &d ) const { return m_ms != d.m_ms; }
00086         bool   operator!=( qint64 d ) const { return m_ms != d; }
00087         bool   operator<( const Duration &d ) const { return m_ms < d.m_ms; }
00088         bool   operator<( qint64 d ) const { return m_ms < d; }
00089         bool   operator<=( const Duration &d ) const { return m_ms <= d.m_ms; }
00090         bool   operator<=( qint64 d ) const { return m_ms <= d; }
00091         bool   operator>( const Duration &d ) const { return m_ms > d.m_ms; }
00092         bool   operator>( qint64 d ) const { return m_ms > d; }
00093         bool   operator>=( const Duration &d ) const { return m_ms >= d.m_ms; }
00094         bool   operator>=( qint64 d ) const { return m_ms >= d; }
00095         Duration &operator=(const Duration &d ) { m_ms = d.m_ms; return *this;}
00096         Duration operator*(int unit) const; 
00097         Duration operator*(const double value) const;
00098         Duration operator/(int unit) const;
00099         double operator/(const Duration &d) const;
00100         
00101         Duration operator+(const Duration &d) const
00102             {Duration dur(*this); dur.add(d); return dur; }
00103         Duration &operator+=(const Duration &d) {add(d); return *this; }
00104         
00105         Duration operator-(const Duration &d) const
00106             {Duration dur(*this); dur.subtract(d); return dur; }
00107         Duration &operator-=(const Duration &d) {subtract(d); return *this; }
00108 
00109         QString toString(Format format = Format_DayTime) const;
00110         static Duration fromString(const QString &s, Format format = Format_DayTime, bool *ok=0);
00111         
00112         //NOTE: These must match fieldnumbers in duration widget!
00113         enum Unit { Unit_d, Unit_h, Unit_m, Unit_s, Unit_ms };
00114         double toDouble(Unit u=Unit_ms) const { 
00115             if (u == Unit_ms) return (double)m_ms;
00116             else if (u == Unit_s) return (double)m_ms/1000.0;
00117             else if (u == Unit_m) return (double)m_ms/(1000.0*60.0);
00118             else if (u == Unit_h) return (double)m_ms/(1000.0*60.0*60.0);
00119             else if (u == Unit_d) return (double)m_ms/(1000.0*60.0*60.0*24.0);
00120             return (double)m_ms; 
00121         }
00122 
00126         static const Duration zeroDuration;
00127 
00128     private:
00133         qint64 m_ms;
00134 
00135         void add(qint64 delta);
00136         void add(const Duration &delta);
00137 
00141         void subtract(const Duration &delta);
00142 };
00143 
00144 }  //KPlato namespace
00145 
00146 #endif

Généré le Wed Nov 22 23:40:56 2006 pour KPlato par  doxygen 1.5.1-p1