• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.4 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • date
klocalizeddate.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2010 John Layt <john@layt.net>
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 "klocalizeddate.h"
21 
22 #include "kglobal.h"
23 #include "kdebug.h"
24 
25 /*****************************************************************************
26  *
27  * Private Section
28  *
29  *****************************************************************************/
30 
31 class KLocalizedDatePrivate : public QSharedData
32 {
33 public:
34  explicit KLocalizedDatePrivate(const QDate &date, const KCalendarSystem *calendar, bool manageCalendar);
35  KLocalizedDatePrivate(const KLocalizedDatePrivate &rhs);
36  KLocalizedDatePrivate &operator=(const KLocalizedDatePrivate &rhs);
37  virtual ~KLocalizedDatePrivate();
38 
39  QDate m_date;
40  const KCalendarSystem *m_calendar;
41  bool m_manageCalendar;
42 };
43 
44 KLocalizedDatePrivate::KLocalizedDatePrivate(const QDate &date, const KCalendarSystem *calendar, bool manageCalendar)
45  : QSharedData(),
46  m_date(date),
47  m_calendar(calendar),
48  m_manageCalendar(manageCalendar)
49 {
50 }
51 
52 KLocalizedDatePrivate::KLocalizedDatePrivate(const KLocalizedDatePrivate &rhs)
53  : QSharedData(rhs),
54  m_date(rhs.m_date),
55  m_calendar(rhs.m_calendar),
56  m_manageCalendar(rhs.m_manageCalendar)
57 {
58  // If we're managing the calendar object, then take a copy,
59  // i.e. user called setCalendarSystem() rather than passing a custom one into the constructor
60  if (m_manageCalendar) {
61  m_calendar = KCalendarSystem::create(m_calendar->calendarSystem(), new KLocale(*m_calendar->locale()));
62  }
63 }
64 
65 KLocalizedDatePrivate &KLocalizedDatePrivate::operator=(const KLocalizedDatePrivate &rhs)
66 {
67  m_date = rhs.m_date;
68  m_calendar = rhs.m_calendar;
69  m_manageCalendar = rhs.m_manageCalendar;
70  // If we're managing the calendar object, then take a copy,
71  // i.e. user called setCalendarSystem() rather than passing a custom one into the constructor
72  if (rhs.m_manageCalendar) {
73  m_calendar = KCalendarSystem::create(m_calendar->calendarSystem(), new KLocale(*m_calendar->locale()));
74  }
75  return *this;
76 }
77 
78 KLocalizedDatePrivate::~KLocalizedDatePrivate()
79 {
80  // If we're managing the calendar object, then delete it,
81  // i.e. user called setCalendarSystem() rather than passing a custom one into the constructor
82  if (m_manageCalendar) {
83  delete m_calendar;
84  }
85 }
86 
87 /*****************************************************************************
88  *
89  * Date Creation Section
90  *
91  *****************************************************************************/
92 
93 KLocalizedDate::KLocalizedDate(const QDate &date, const KCalendarSystem *calendarSystem)
94  : d(new KLocalizedDatePrivate(date, calendarSystem, false))
95 {
96 }
97 
98 KLocalizedDate::KLocalizedDate(int year, int month, int day, const KCalendarSystem *calendarSystem)
99  : d(new KLocalizedDatePrivate(QDate(), calendarSystem, false))
100 {
101  setDate(year, month, day);
102 }
103 
104 KLocalizedDate::KLocalizedDate(const KLocalizedDate &rhs)
105  : d(new KLocalizedDatePrivate(*rhs.d))
106 {
107 }
108 
109 KLocalizedDate &KLocalizedDate::operator=(const KLocalizedDate &rhs)
110 {
111  *d = *rhs.d;
112  return *this;
113 }
114 
115 KLocalizedDate &KLocalizedDate::operator=(const QDate &rhs)
116 {
117  d->m_date = rhs;
118  return *this;
119 }
120 
121 KLocalizedDate::~KLocalizedDate()
122 {
123 }
124 
125 /*****************************************************************************
126  *
127  * Calendar System Section
128  *
129  *****************************************************************************/
130 
131 void KLocalizedDate::setCalendarSystem(KLocale::CalendarSystem calendarSystem)
132 {
133  if (calendarSystem == calendar()->calendarSystem()) {
134  return;
135  }
136  KCalendarSystem *newCalendar = KCalendarSystem::create(calendarSystem,
137  new KLocale(*calendar()->locale()));
138  if (d->m_manageCalendar) {
139  delete d->m_calendar;
140  }
141  d->m_calendar = newCalendar;
142 }
143 
144 KLocale::CalendarSystem KLocalizedDate::calendarSystem()
145 {
146  return calendar()->calendarSystem();
147 }
148 
149 const KCalendarSystem *KLocalizedDate::calendar() const
150 {
151  if (d->m_calendar) {
152  return d->m_calendar;
153  }
154  return KGlobal::locale()->calendar();
155 }
156 
157 /*****************************************************************************
158  *
159  * Date Status Section
160  *
161  *****************************************************************************/
162 
163 bool KLocalizedDate::isNull() const
164 {
165  return date().isNull();
166 }
167 
168 bool KLocalizedDate::isValid() const
169 {
170  return calendar()->isValid(date());
171 }
172 
173 /*****************************************************************************
174  *
175  * Date Setting Section
176  *
177  *****************************************************************************/
178 
179 bool KLocalizedDate::setDate(const QDate &date)
180 {
181  d->m_date = date;
182  return isValid();
183 }
184 
185 bool KLocalizedDate::setDate(int year, int month, int day)
186 {
187  calendar()->setDate(d->m_date, year, month, day);
188  return isValid();
189 }
190 
191 bool KLocalizedDate::setDate(int year, int dayOfYear)
192 {
193  calendar()->setDate(d->m_date, year, dayOfYear);
194  return isValid();
195 }
196 
197 bool KLocalizedDate::setDate(QString eraName, int yearInEra, int month, int day)
198 {
199  calendar()->setDate(d->m_date, eraName, yearInEra, month, day);
200  return isValid();
201 }
202 
203 bool KLocalizedDate::setDate(KLocale::WeekNumberSystem weekNumberSystem, int year, int isoWeekNumber, int dayOfIsoWeek)
204 {
205  Q_UNUSED(weekNumberSystem); // Only support ISO Week at the moment
206  calendar()->setDateIsoWeek(d->m_date, year, isoWeekNumber, dayOfIsoWeek);
207  return isValid();
208 }
209 
210 bool KLocalizedDate::setCurrentDate()
211 {
212  d->m_date = QDate::currentDate();
213  return isValid();
214 }
215 
216 /*****************************************************************************
217  *
218  * Static Date Creation Section
219  *
220  *****************************************************************************/
221 
222 KLocalizedDate KLocalizedDate::currentDate()
223 {
224  return KLocalizedDate(QDate::currentDate());
225 }
226 
227 KLocalizedDate KLocalizedDate::fromDate(const QDate &date)
228 {
229  return KLocalizedDate(date);
230 }
231 
232 KLocalizedDate KLocalizedDate::fromJulianDay(int jd)
233 {
234  return KLocalizedDate(QDate::fromJulianDay(jd));
235 }
236 
237 /*****************************************************************************
238  *
239  * Date Componant Section
240  *
241  *****************************************************************************/
242 
243 int KLocalizedDate::toJulianDay() const
244 {
245  return d->m_date.toJulianDay();
246 }
247 
248 QDate KLocalizedDate::date() const
249 {
250  return d->m_date;
251 }
252 
253 void KLocalizedDate::getDate(int *year, int *month, int *day) const
254 {
255  calendar()->getDate(date(), year, month, day);
256 }
257 
258 int KLocalizedDate::year() const
259 {
260  return calendar()->year(date());
261 }
262 
263 int KLocalizedDate::month() const
264 {
265  return calendar()->month(date());
266 }
267 
268 int KLocalizedDate::day() const
269 {
270  return calendar()->day(date());
271 }
272 
273 QString KLocalizedDate::eraName() const
274 {
275  return formatDate(KLocale::EraName);
276 }
277 
278 QString KLocalizedDate::eraYear() const
279 {
280  return formatDate(KLocale::EraYear);
281 }
282 
283 int KLocalizedDate::yearInEra() const
284 {
285  return calendar()->yearInEra(date());
286 }
287 
288 int KLocalizedDate::dayOfYear() const
289 {
290  return calendar()->dayOfYear(date());
291 }
292 
293 int KLocalizedDate::dayOfWeek() const
294 {
295  return calendar()->dayOfWeek(date());
296 }
297 
298 int KLocalizedDate::week(int *yearNum) const
299 {
300  return calendar()->week(date(), yearNum);
301 }
302 
303 int KLocalizedDate::week(KLocale::WeekNumberSystem weekNumberSystem, int *yearNum) const
304 {
305  return calendar()->week(date(), weekNumberSystem, yearNum);
306 }
307 
308 int KLocalizedDate::monthsInYear() const
309 {
310  return calendar()->monthsInYear(date());
311 }
312 
313 int KLocalizedDate::weeksInYear() const
314 {
315  return calendar()->weeksInYear(date());
316 }
317 
318 int KLocalizedDate::weeksInYear(KLocale::WeekNumberSystem weekNumberSystem) const
319 {
320  return calendar()->weeksInYear(date(), weekNumberSystem);
321 }
322 
323 int KLocalizedDate::daysInYear() const
324 {
325  return calendar()->daysInYear(date());
326 }
327 
328 int KLocalizedDate::daysInMonth() const
329 {
330  return calendar()->daysInMonth(date());
331 }
332 
333 int KLocalizedDate::daysInWeek() const
334 {
335  return calendar()->daysInWeek(date());
336 }
337 
338 bool KLocalizedDate::isLeapYear() const
339 {
340  return calendar()->isLeapYear(date());
341 }
342 
343 /*****************************************************************************
344  *
345  * Date Formatting Section
346  *
347  *****************************************************************************/
348 
349 QString KLocalizedDate::formatDate(KLocale::DateFormat toFormat) const
350 {
351  return calendar()->formatDate(date(), toFormat);
352 }
353 
354 QString KLocalizedDate::formatDate(const QString &toFormat, KLocale::DateTimeFormatStandard formatStandard) const
355 {
356  return calendar()->formatDate(date(), toFormat, formatStandard);
357 }
358 
359 QString KLocalizedDate::formatDate(KLocale::DateTimeComponent component,
360  KLocale::DateTimeComponentFormat format,
361  KLocale::WeekNumberSystem weekNumberSystem) const
362 {
363  return calendar()->formatDate(date(), component, format, weekNumberSystem);
364 }
365 
366 /*****************************************************************************
367  *
368  * Date Parsing Section
369  *
370  *****************************************************************************/
371 
372 KLocalizedDate KLocalizedDate::readDate(const QString &dateString,
373  KLocale::DateTimeParseMode parseMode,
374  const KCalendarSystem *calendar)
375 {
376  Q_UNUSED(parseMode);
377  if (!calendar) {
378  calendar = KGlobal::locale()->calendar();
379  }
380  return KLocalizedDate(calendar->readDate(dateString));
381 }
382 
383 KLocalizedDate KLocalizedDate::readDate(const QString &dateString,
384  KLocale::ReadDateFlags formatFlags,
385  KLocale::DateTimeParseMode parseMode,
386  const KCalendarSystem *calendar)
387 {
388  Q_UNUSED(parseMode);
389  if (!calendar) {
390  calendar = KGlobal::locale()->calendar();
391  }
392  return KLocalizedDate(calendar->readDate(dateString, formatFlags));
393 }
394 
395 KLocalizedDate KLocalizedDate::readDate(const QString &dateString,
396  const QString &dateFormat,
397  KLocale::DateTimeParseMode parseMode,
398  KLocale::DateTimeFormatStandard formatStandard,
399  const KCalendarSystem *calendar)
400 {
401  Q_UNUSED(parseMode);
402  if (!calendar) {
403  calendar = KGlobal::locale()->calendar();
404  }
405  return KLocalizedDate(calendar->readDate(dateString, dateFormat, 0, formatStandard));
406 }
407 
408 /*****************************************************************************
409  *
410  * Date Maths Section
411  *
412  *****************************************************************************/
413 
414 KLocalizedDate KLocalizedDate::addYears(int years) const
415 {
416  KLocalizedDate newDate;
417  newDate = *this;
418  newDate.setDate(calendar()->addYears(date(), years));
419  return newDate;
420 }
421 
422 bool KLocalizedDate::addYearsTo(int years)
423 {
424  d->m_date = calendar()->addYears(date(), years);
425  return isValid();
426 }
427 
428 KLocalizedDate KLocalizedDate::addMonths(int months) const
429 {
430  KLocalizedDate newDate(*this);
431  newDate.setDate(calendar()->addMonths(date(), months));
432  return newDate;
433 }
434 
435 bool KLocalizedDate::addMonthsTo(int months)
436 {
437  d->m_date = calendar()->addMonths(date(), months);
438  return isValid();
439 }
440 
441 KLocalizedDate KLocalizedDate::addDays(int days) const
442 {
443  KLocalizedDate newDate(*this);
444  newDate.setDate(calendar()->addDays(date(), days));
445  return newDate;
446 }
447 
448 bool KLocalizedDate::addDaysTo(int days)
449 {
450  d->m_date = calendar()->addDays(date(), days);
451  return isValid();
452 }
453 
454 void KLocalizedDate::dateDifference(const KLocalizedDate &toDate,
455  int *yearsDiff, int *monthsDiff, int *daysDiff, int *direction) const
456 {
457  dateDifference(toDate.date(), yearsDiff, monthsDiff, daysDiff, direction);
458 }
459 
460 void KLocalizedDate::dateDifference(const QDate &toDate,
461  int *yearsDiff, int *monthsDiff, int *daysDiff, int *direction) const
462 {
463  calendar()->dateDifference(date(), toDate, yearsDiff, monthsDiff, daysDiff, direction);
464 }
465 
466 int KLocalizedDate::yearsDifference(const KLocalizedDate &toDate) const
467 {
468  return yearsDifference(toDate.date());
469 }
470 
471 int KLocalizedDate::yearsDifference(const QDate &toDate) const
472 {
473  return calendar()->yearsDifference(date(), toDate);
474 }
475 
476 int KLocalizedDate::monthsDifference(const KLocalizedDate &toDate) const
477 {
478  return monthsDifference(toDate.date());
479 }
480 
481 int KLocalizedDate::monthsDifference(const QDate &toDate) const
482 {
483  return calendar()->monthsDifference(date(), toDate);
484 }
485 
486 int KLocalizedDate::daysDifference(const KLocalizedDate &toDate) const
487 {
488  return daysDifference(toDate.date());
489 }
490 
491 int KLocalizedDate::daysDifference(const QDate &toDate) const
492 {
493  return calendar()->daysDifference(date(), toDate);
494 }
495 
496 KLocalizedDate KLocalizedDate::firstDayOfYear() const
497 {
498  KLocalizedDate newDate(*this);
499  newDate.setDate(calendar()->firstDayOfYear(date()));
500  return newDate;
501 }
502 
503 KLocalizedDate KLocalizedDate::lastDayOfYear() const
504 {
505  KLocalizedDate newDate(*this);
506  newDate.setDate(calendar()->lastDayOfYear(date()));
507  return newDate;
508 }
509 
510 KLocalizedDate KLocalizedDate::firstDayOfMonth() const
511 {
512  KLocalizedDate newDate(*this);
513  newDate.setDate(calendar()->firstDayOfMonth(date()));
514  return newDate;
515 }
516 
517 KLocalizedDate KLocalizedDate::lastDayOfMonth() const
518 {
519  KLocalizedDate newDate(*this);
520  newDate.setDate(calendar()->lastDayOfMonth(date()));
521  return newDate;
522 }
523 
524 /*****************************************************************************
525  *
526  * Date Operators Section
527  *
528  *****************************************************************************/
529 
530 bool KLocalizedDate::operator==(const KLocalizedDate &rhs) const
531 {
532  return (date() == rhs.date());
533 }
534 
535 bool KLocalizedDate::operator==(const QDate &rhs) const
536 {
537  return (date() == rhs);
538 }
539 
540 bool KLocalizedDate::operator!=(const KLocalizedDate &rhs) const
541 {
542  return (date() != rhs.date());
543 }
544 
545 bool KLocalizedDate::operator!=(const QDate &rhs) const
546 {
547  return (date() != rhs);
548 }
549 
550 bool KLocalizedDate::operator<(const KLocalizedDate &rhs) const
551 {
552  return (date() < rhs.date());
553 }
554 
555 bool KLocalizedDate::operator<(const QDate &rhs) const
556 {
557  return (date() < rhs);
558 }
559 
560 bool KLocalizedDate::operator<=(const KLocalizedDate &rhs) const
561 {
562  return (d->m_date <= rhs.date());
563 }
564 
565 bool KLocalizedDate::operator<=(const QDate &rhs) const
566 {
567  return (date() <= rhs);
568 }
569 
570 bool KLocalizedDate::operator>(const KLocalizedDate &rhs) const
571 {
572  return (date() > rhs.date());
573 }
574 
575 bool KLocalizedDate::operator>(const QDate &rhs) const
576 {
577  return (date() > rhs);
578 }
579 
580 bool KLocalizedDate::operator>=(const KLocalizedDate &rhs) const
581 {
582  return (date() >= rhs.date());
583 }
584 
585 bool KLocalizedDate::operator>=(const QDate &rhs) const
586 {
587  return (date() >= rhs);
588 }
589 
590 QDataStream &operator<<(QDataStream &out, const KLocalizedDate &date)
591 {
592  return out << (quint32)(date.toJulianDay()) << date.calendar()->calendarSystem();
593 }
594 
595 QDataStream &operator>>(QDataStream &in, KLocalizedDate &date)
596 {
597  quint32 jd;
598  int calendarSystem;
599  in >> jd >> calendarSystem;
600  date.setDate(QDate::fromJulianDay(jd));
601  date.setCalendarSystem((KLocale::CalendarSystem)calendarSystem);
602  return in;
603 }
604 
605 QDebug operator<<(QDebug dbg, const KLocalizedDate &date)
606 {
607  if (date.calendar()->calendarSystem() == KLocale::QDateCalendar) {
608  dbg.nospace() << "KLocalizedDate(" << date.formatDate(KLocale::IsoDate) << ", "
609  << date.calendar()->calendarLabel() << ')';
610  } else {
611  dbg.nospace() << "KLocalizedDate(" << date.formatDate(KLocale::IsoDate) << ", "
612  << date.calendar()->calendarLabel() << ')'
613  << " = QDate(" << date.date().toString(Qt::ISODate) << ')';
614  }
615  return dbg.space();
616 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jun 1 2013 20:18:00 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.10.4 API Reference

Skip menu "kdelibs-4.10.4 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal