kplato

kptdatetime.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003 Dag Andersen <danders@get2net.dk>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "kptdatetime.h"
21
22#include <kdebug.h>
23
24namespace KPlato
25{
26
27DateTime::DateTime() : TQDateTime() {
28}
29
30DateTime::DateTime(const TQDateTime &dt) : TQDateTime(dt.date(), dt.time()) {
31}
32
33DateTime::DateTime(const TQDate &date, const TQTime &time) : TQDateTime(date, time) {
34}
35
36void DateTime::add(const Duration &duration) {
37 if (isValid())
38 *this = TQDateTime(addSecs(duration.seconds()));
39 //kdDebug()<<k_funcinfo<<"days,secs: "<<days<<","<<secs<<" gives: "<<toString()<<endl;
40}
41
42void DateTime::subtract(const Duration &duration) {
43 if (isValid())
44 *this = TQDateTime(addSecs(-duration.seconds()));
45 //kdDebug()<<k_funcinfo<<"days,secs: "<<days<<","<<secs<<" gives: "<<toString()<<endl;
46}
47
48Duration DateTime::duration(const DateTime &dt) const {
49 Duration dur;
50 if (isValid() && dt.isValid()) {
51 if (dt < *this) {
52 dur.addDays(dt.daysTo(*this));
53 dur.addSeconds(dt.time().secsTo(time()));
54 } else {
55 dur.addDays(daysTo(dt));
56 dur.addSeconds(time().secsTo(dt.time()));
57 }
58 }
59 return dur;
60}
61
62DateTime DateTime::operator+(const Duration &duration) const {
63 DateTime d(*this);
64 d.add(duration);
65 return d;
66}
67
68DateTime& DateTime::operator+=(const Duration &duration) {
69 add(duration);
70 return *this;
71}
72
73DateTime DateTime::operator-(const Duration &duration) const {
74 DateTime d(*this);
75 d.subtract(duration);
76 return d;
77}
78
79DateTime& DateTime::operator-=(const Duration &duration) {
80 subtract(duration);
81 return *this;
82}
83
84} //KPlato namespace
DateTime is a TQDateTime which knows about Duration.
Definition kptdatetime.h:33
DateTime operator-(const Duration &duration) const
Subtracts the duration.
DateTime operator+(const Duration &duration) const
Adds the duration.
The duration class can be used to store a timespan in a convenient format.
Definition kptduration.h:34