summaryrefslogtreecommitdiffabout
path: root/kabc/vcard/DateValue.cpp
Unidiff
Diffstat (limited to 'kabc/vcard/DateValue.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/vcard/DateValue.cpp434
1 files changed, 434 insertions, 0 deletions
diff --git a/kabc/vcard/DateValue.cpp b/kabc/vcard/DateValue.cpp
new file mode 100644
index 0000000..c5c5c85
--- a/dev/null
+++ b/kabc/vcard/DateValue.cpp
@@ -0,0 +1,434 @@
1/*
2 libvcard - vCard parsing library for vCard version 3.0
3
4 Copyright (C) 1998 Rik Hemsley rik@kde.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to
8 deal in the Software without restriction, including without limitation the
9 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 sell copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22*/
23
24#include <qregexp.h>
25
26#include <kdebug.h>
27
28#include <VCardDefines.h>
29#include <VCardDateValue.h>
30#include <VCardValue.h>
31
32using namespace VCARD;
33
34DateValue::DateValue()
35 :Value()
36{
37 vDebug("DateValue::DateValue()");
38}
39
40DateValue::DateValue(
41 unsigned intyear,
42 unsigned intmonth,
43 unsigned intday,
44 unsigned inthour,
45 unsigned intminute,
46 unsigned intsecond,
47 double secFrac,
48 bool zonePositive,
49 unsigned intzoneHour,
50 unsigned intzoneMinute)
51 : Value (),
52 year_ (year),
53 month_ (month),
54 day_ (day),
55 hour_ (hour),
56 minute_ (minute),
57 second_ (second),
58 zoneHour_ (zoneHour),
59 zoneMinute_ (zoneMinute),
60 secFrac_ (secFrac),
61 zonePositive_(zonePositive),
62 hasTime_(true)
63{
64 parsed_ = true;
65 assembled_ = false;
66}
67
68DateValue::DateValue(const QDate & d)
69 : Value (),
70 year_ (d.year()),
71 month_ (d.month()),
72 day_ (d.day()),
73 hasTime_(false)
74{
75 parsed_ = true;
76 assembled_ = false;
77}
78
79DateValue::DateValue(const QDateTime & d)
80 : Value (),
81 year_ (d.date().year()),
82 month_ (d.date().month()),
83 day_ (d.date().day()),
84 hour_ (d.time().hour()),
85 minute_ (d.time().minute()),
86 second_ (d.time().second()),
87 hasTime_(true)
88{
89 parsed_ = true;
90 assembled_ = false;
91}
92
93DateValue::DateValue(const DateValue & x)
94 :Value(x)
95{
96 year_ = x.year_;
97 month_ = x.month_;
98 day_ = x.day_;
99 hour_ = x.hour_;
100 minute_ = x.minute_;
101 second_ = x.second_;
102 zoneHour_ = x.zoneHour_;
103 zoneMinute_ = x.zoneMinute_;
104 secFrac_ = x.secFrac_;
105 hasTime_ = x.hasTime_;
106}
107
108DateValue::DateValue(const QCString & s)
109 :Value(s)
110{
111}
112
113 DateValue &
114DateValue::operator = (DateValue & x)
115{
116 if (*this == x) return *this;
117
118 Value::operator = (x);
119 return *this;
120}
121
122 DateValue &
123DateValue::operator = (const QCString & s)
124{
125 Value::operator = (s);
126 return *this;
127}
128
129 bool
130DateValue::operator == (DateValue & x)
131{
132 x.parse();
133 return false;
134}
135
136DateValue::~DateValue()
137{
138}
139
140 DateValue *
141DateValue::clone()
142{
143 return new DateValue( *this );
144}
145
146 void
147DateValue::_parse()
148{
149 vDebug("DateValue::_parse()");
150
151 // date = date-full-year ["-"] date-month ["-"] date-mday
152 // time = time-hour [":"] time-minute [":"] time-second [":"]
153 // [time-secfrac] [time-zone]
154
155 int timeSep = strRep_.find('T');
156
157 QCString dateStr;
158 QCString timeStr;
159
160 if (timeSep == -1) {
161
162 dateStr = strRep_;
163 vDebug("Has date string \"" + dateStr + "\"");
164
165 } else {
166
167 dateStr = strRep_.left(timeSep);
168 vDebug("Has date string \"" + dateStr + "\"");
169
170 timeStr = strRep_.mid(timeSep + 1);
171 vDebug("Has time string \"" + timeStr + "\"");
172 }
173
174 /////////////////////////////////////////////////////////////// DATE
175
176 dateStr.replace(QRegExp("-"), "");
177
178 kdDebug(5710) << "dateStr: " << dateStr << endl;
179
180 year_= dateStr.left(4).toInt();
181 month_= dateStr.mid(4, 2).toInt();
182 day_= dateStr.right(2).toInt();
183
184 if (timeSep == -1) {
185 hasTime_ = false;
186 return; // No time, done.
187 }
188 else
189 hasTime_ = true;
190
191 /////////////////////////////////////////////////////////////// TIME
192
193 /////////////////////////////////////////////////////////////// ZONE
194
195 int zoneSep = timeStr.find('Z');
196
197 if (zoneSep != -1 && timeStr.length() - zoneSep > 3) {
198
199 QCString zoneStr(timeStr.mid(zoneSep + 1));
200 vDebug("zoneStr == " + zoneStr);
201
202 zonePositive_= (zoneStr[0] == '+');
203 zoneHour_ = zoneStr.mid(1, 2).toInt();
204 zoneMinute_ = zoneStr.right(2).toInt();
205
206 timeStr.remove(zoneSep, timeStr.length() - zoneSep);
207 }
208
209 //////////////////////////////////////////////////// SECOND FRACTION
210
211 int secFracSep = timeStr.findRev(',');
212
213 if (secFracSep != -1 && zoneSep != -1) { // zoneSep checked to avoid errors.
214 QCString quirkafleeg = "0." + timeStr.mid(secFracSep + 1, zoneSep);
215 secFrac_ = quirkafleeg.toDouble();
216 }
217
218 /////////////////////////////////////////////////////////////// HMS
219
220 timeStr.replace(QRegExp(":"), "");
221
222 hour_= timeStr.left(2).toInt();
223 minute_= timeStr.mid(2, 2).toInt();
224 second_= timeStr.mid(4, 2).toInt();
225}
226
227 void
228DateValue::_assemble()
229{
230 vDebug("DateValue::_assemble");
231
232 QCString year;
233 QCString month;
234 QCString day;
235
236 year.setNum( year_ );
237 month.setNum( month_ );
238 day.setNum( day_ );
239
240 if ( month.length() < 2 ) month.prepend( "0" );
241 if ( day.length() < 2 ) day.prepend( "0" );
242
243 strRep_ = year + '-' + month + '-' + day;
244
245 if ( hasTime_ ) {
246 QCString hour;
247 QCString minute;
248 QCString second;
249
250 hour.setNum( hour_ );
251 minute.setNum( minute_ );
252 second.setNum( second_ );
253
254 if ( hour.length() < 2 ) hour.prepend( "0" );
255 if ( minute.length() < 2 ) minute.prepend( "0" );
256 if ( second.length() < 2 ) second.prepend( "0" );
257
258 strRep_ += 'T' + hour + ':' + minute + ':' + second + 'Z';
259 }
260}
261
262 unsigned int
263DateValue::year()
264{
265 parse();
266 return year_;
267}
268
269 unsigned int
270DateValue::month()
271{
272 parse();
273 return month_;
274}
275
276 unsigned int
277DateValue::day()
278{
279 parse();
280 return day_;
281}
282 unsigned int
283DateValue::hour()
284{
285 parse();
286 return hour_;
287}
288
289 unsigned int
290DateValue::minute()
291{
292 parse();
293 return minute_;
294}
295
296 unsigned int
297DateValue::second()
298{
299 parse();
300 return second_;
301}
302
303 double
304DateValue::secondFraction()
305{
306 parse();
307 return secFrac_;
308}
309
310 bool
311DateValue::zonePositive()
312{
313 parse();
314 return zonePositive_;
315}
316
317 unsigned int
318DateValue::zoneHour()
319{
320 parse();
321 return zoneHour_;
322}
323
324 unsigned int
325DateValue::zoneMinute()
326{
327 parse();
328 return zoneMinute_;
329}
330
331 void
332DateValue::setYear(unsigned int i)
333{
334 year_ = i;
335 assembled_ = false;
336}
337
338 void
339DateValue::setMonth(unsigned int i)
340{
341 month_ = i;
342 assembled_ = false;
343}
344
345 void
346DateValue::setDay(unsigned int i)
347{
348 day_ = i;
349 assembled_ = false;
350}
351
352 void
353DateValue::setHour(unsigned int i)
354{
355 hour_ = i;
356 assembled_ = false;
357}
358
359 void
360DateValue::setMinute(unsigned int i)
361{
362 minute_ = i;
363 assembled_ = false;
364}
365
366 void
367DateValue::setSecond(unsigned int i)
368{
369 second_ = i;
370 assembled_ = false;
371}
372
373 void
374DateValue::setSecondFraction(double d)
375{
376 secFrac_ = d;
377 assembled_ = false;
378}
379
380 void
381DateValue::setZonePositive(bool b)
382{
383 zonePositive_ = b;
384 assembled_ = false;
385 }
386
387 void
388DateValue::setZoneHour(unsigned int i)
389{
390 zoneHour_ = i;
391 assembled_ = false;
392}
393
394 void
395DateValue::setZoneMinute(unsigned int i)
396{
397 zoneMinute_ = i;
398 assembled_ = false;
399}
400
401 QDate
402DateValue::qdate()
403{
404 parse();
405 QDate d(year_, month_, day_);
406 return d;
407}
408
409 QTime
410DateValue::qtime()
411{
412 parse();
413 QTime t(hour_, minute_, second_);
414 //t.setMs(1 / secFrac_);
415 return t;
416}
417
418 QDateTime
419DateValue::qdt()
420{
421 parse();
422 QDateTime dt;
423 dt.setDate(qdate());
424 dt.setTime(qtime());
425 return dt;
426}
427
428 bool
429DateValue::hasTime()
430{
431 parse();
432 return hasTime_;
433}
434