Added a few unittests + bugfix in Year
[libdatetime.git] / src / DateTime.cxx
blob120d902b3f0690e53bcee006ffe8c5d90bb59eeb
1 /*{{{ LICENCE
3 * libdatetime
4 * Copyright (C) 2009 Andreas Waidler <arandes@programmers.at>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED AS IS AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 *}}}*/
20 #include <libdatetime/DateTime.hxx>
22 #include <libdatetime/Date.hxx>
23 #include <libdatetime/Time.hxx>
25 #include <cstdlib>
27 namespace libdatetime
29 // public
30 DateTime::DateTime(const Timestamp& timestamp)/*{{{*/
31 : _timestamp(NULL)
32 , _ownsTimestamp(false)
33 , _date(NULL)
34 , _ownsDate(false)
35 , _time(NULL)
36 , _ownsTime(false)
38 set(timestamp);
41 /*}}}*/
42 DateTime::DateTime(Timestamp* timestamp)/*{{{*/
43 : _timestamp(NULL)
44 , _ownsTimestamp(false)
45 , _date(NULL)
46 , _ownsDate(false)
47 , _time(NULL)
48 , _ownsTime(false)
50 set(timestamp);
53 /*}}}*/
54 DateTime::DateTime(const Date& date, const Time& time)/*{{{*/
55 : _timestamp(NULL)
56 , _ownsTimestamp(false)
57 , _date(NULL)
58 , _ownsDate(false)
59 , _time(NULL)
60 , _ownsTime(false)
62 set(date, time);
65 /*}}}*/
66 DateTime::DateTime(Date* date, Time* time)/*{{{*/
67 : _timestamp(NULL)
68 , _ownsTimestamp(false)
69 , _date(NULL)
70 , _ownsDate(false)
71 , _time(NULL)
72 , _ownsTime(false)
74 set(date, time);
77 /*}}}*/
78 DateTime::~DateTime()/*{{{*/
80 dispose();
83 /*}}}*/
84 const Timestamp& DateTime::timestamp() const/*{{{*/
86 return *_timestamp;
89 /*}}}*/
90 const Date& DateTime::date() const/*{{{*/
92 return *_date;
95 /*}}}*/
96 const Time& DateTime::time() const/*{{{*/
98 return *_time;
101 /*}}}*/
103 // protected
104 DateTime::DateTime()/*{{{*/
105 : _timestamp(NULL)
106 , _ownsTimestamp(false)
107 , _date(NULL)
108 , _ownsDate(false)
109 , _time(NULL)
110 , _ownsTime(false)
114 /*}}}*/
115 void DateTime::set(const Timestamp& timestamp)/*{{{*/
117 dispose();
119 _timestamp = &timestamp;
120 _ownsTimestamp = false;
122 _allocateDateTime();
125 /*}}}*/
126 void DateTime::set(Timestamp* timestamp)/*{{{*/
128 dispose();
130 _timestamp = timestamp;
131 _ownsTimestamp = true;
133 _allocateDateTime();
136 /*}}}*/
137 void DateTime::set(const Date& date, const Time& time)/*{{{*/
139 dispose();
141 _date = &date;
142 _ownsDate = false;
143 _time = &time;
144 _ownsTime = false;
146 _allocateTimestamp();
149 /*}}}*/
150 void DateTime::set(Date* date, Time* time)/*{{{*/
152 dispose();
154 _date = date;
155 _ownsDate = true;
156 _time = time;
157 _ownsTime = true;
159 _allocateTimestamp();
162 /*}}}*/
163 void DateTime::dispose()/*{{{*/
165 if (_ownsTimestamp) {
166 delete _timestamp;
167 _timestamp = NULL;
169 if (_ownsDate) {
170 delete _date;
171 _date = NULL;
173 if (_ownsTime) {
174 delete _time;
175 _time = NULL;
179 /*}}}*/
181 // private
182 void DateTime::_allocateTimestamp()/*{{{*/
184 _timestamp = new Timestamp(abs(*_date + *_time));
185 _ownsTimestamp = true;
188 /*}}}*/
189 void DateTime::_allocateDateTime()/*{{{*/
191 _date = new Date(*_timestamp);
192 _ownsDate = true;
193 _time = new Time(*_date - *_timestamp);
194 _ownsTime = true;
197 /*}}}*/
200 // Use no tabs at all; two spaces indentation; max. eighty chars per line.
201 // vim: et ts=2 sw=2 sts=2 tw=80 fdm=marker