KDELibs4Support

klocalizeddate.cpp
1 /*
2  Copyright 2010 John Layt <[email protected]>
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 "kcalendarsystem.h"
23 
24 #include <QDebug>
25 #include <QDataStream>
26 
27 /*****************************************************************************
28  *
29  * Private Section
30  *
31  *****************************************************************************/
32 
33 class KLocalizedDatePrivate : public QSharedData
34 {
35 public:
36  KDELIBS4SUPPORT_DEPRECATED explicit KLocalizedDatePrivate(const QDate &date, const KCalendarSystem *calendar, bool manageCalendar);
37  KLocalizedDatePrivate(const KLocalizedDatePrivate &rhs);
38  KLocalizedDatePrivate &operator=(const KLocalizedDatePrivate &rhs);
39  virtual ~KLocalizedDatePrivate();
40 
41  QDate m_date;
42  const KCalendarSystem *m_calendar;
43  bool m_manageCalendar;
44 };
45 
46 KLocalizedDatePrivate::KLocalizedDatePrivate(const QDate &date, const KCalendarSystem *calendar, bool manageCalendar)
47  : QSharedData(),
48  m_date(date),
49  m_calendar(calendar),
50  m_manageCalendar(manageCalendar)
51 {
52 }
53 
54 KLocalizedDatePrivate::KLocalizedDatePrivate(const KLocalizedDatePrivate &rhs)
55  : QSharedData(rhs),
56  m_date(rhs.m_date),
57  m_calendar(rhs.m_calendar),
58  m_manageCalendar(rhs.m_manageCalendar)
59 {
60  // If we're managing the calendar object, then take a copy,
61  // i.e. user called setCalendarSystem() rather than passing a custom one into the constructor
62  if (m_manageCalendar) {
63  m_calendar = KCalendarSystem::create(m_calendar->calendarSystem(), new KLocale(*m_calendar->locale()));
64  }
65 }
66 
67 KLocalizedDatePrivate &KLocalizedDatePrivate::operator=(const KLocalizedDatePrivate &rhs)
68 {
69  m_date = rhs.m_date;
70  m_calendar = rhs.m_calendar;
71  m_manageCalendar = rhs.m_manageCalendar;
72  // If we're managing the calendar object, then take a copy,
73  // i.e. user called setCalendarSystem() rather than passing a custom one into the constructor
74  if (rhs.m_manageCalendar) {
75  m_calendar = KCalendarSystem::create(m_calendar->calendarSystem(), new KLocale(*m_calendar->locale()));
76  }
77  return *this;
78 }
79 
80 KLocalizedDatePrivate::~KLocalizedDatePrivate()
81 {
82  // If we're managing the calendar object, then delete it,
83  // i.e. user called setCalendarSystem() rather than passing a custom one into the constructor
84  if (m_manageCalendar) {
85  delete m_calendar;
86  }
87 }
88 
89 /*****************************************************************************
90  *
91  * Date Creation Section
92  *
93  *****************************************************************************/
94 
96  : d(new KLocalizedDatePrivate(date, calendarSystem, false))
97 {
98 }
99 
101  : d(new KLocalizedDatePrivate(QDate(), calendarSystem, false))
102 {
103  setDate(year, month, day);
104 }
105 
107  : d(new KLocalizedDatePrivate(*rhs.d))
108 {
109 }
110 
112 {
113  *d = *rhs.d;
114  return *this;
115 }
116 
118 {
119  d->m_date = rhs;
120  return *this;
121 }
122 
124 {
125 }
126 
127 /*****************************************************************************
128  *
129  * Calendar System Section
130  *
131  *****************************************************************************/
132 
134 {
135  if (calendarSystem == calendar()->calendarSystem()) {
136  return;
137  }
138  KCalendarSystem *newCalendar = KCalendarSystem::create(calendarSystem,
139  new KLocale(*calendar()->locale()));
140  if (d->m_manageCalendar) {
141  delete d->m_calendar;
142  }
143  d->m_calendar = newCalendar;
144 }
145 
147 {
148  return calendar()->calendarSystem();
149 }
150 
152 {
153  if (d->m_calendar) {
154  return d->m_calendar;
155  }
156  return KLocale::global()->calendar();
157 }
158 
159 /*****************************************************************************
160  *
161  * Date Status Section
162  *
163  *****************************************************************************/
164 
166 {
167  return date().isNull();
168 }
169 
171 {
172  return calendar()->isValid(date());
173 }
174 
175 /*****************************************************************************
176  *
177  * Date Setting Section
178  *
179  *****************************************************************************/
180 
182 {
183  d->m_date = date;
184  return isValid();
185 }
186 
188 {
189  calendar()->setDate(d->m_date, year, month, day);
190  return isValid();
191 }
192 
194 {
195  calendar()->setDate(d->m_date, year, dayOfYear);
196  return isValid();
197 }
198 
200 {
201  calendar()->setDate(d->m_date, eraName, yearInEra, month, day);
202  return isValid();
203 }
204 
205 bool KLocalizedDate::setDate(KLocale::WeekNumberSystem weekNumberSystem, int year, int isoWeekNumber, int dayOfIsoWeek)
206 {
207  Q_UNUSED(weekNumberSystem); // Only support ISO Week at the moment
208  calendar()->setDateIsoWeek(d->m_date, year, isoWeekNumber, dayOfIsoWeek);
209  return isValid();
210 }
211 
213 {
214  d->m_date = QDate::currentDate();
215  return isValid();
216 }
217 
218 /*****************************************************************************
219  *
220  * Static Date Creation Section
221  *
222  *****************************************************************************/
223 
225 {
227 }
228 
230 {
231  return KLocalizedDate(date);
232 }
233 
235 {
237 }
238 
239 /*****************************************************************************
240  *
241  * Date Componant Section
242  *
243  *****************************************************************************/
244 
246 {
247  return d->m_date.toJulianDay();
248 }
249 
251 {
252  return d->m_date;
253 }
254 
255 void KLocalizedDate::getDate(int *year, int *month, int *day) const
256 {
257  calendar()->getDate(date(), year, month, day);
258 }
259 
261 {
262  return calendar()->year(date());
263 }
264 
266 {
267  return calendar()->month(date());
268 }
269 
271 {
272  return calendar()->day(date());
273 }
274 
276 {
278 }
279 
281 {
283 }
284 
286 {
287  return calendar()->yearInEra(date());
288 }
289 
291 {
292  return calendar()->dayOfYear(date());
293 }
294 
296 {
297  return calendar()->dayOfWeek(date());
298 }
299 
300 int KLocalizedDate::week(int *yearNum) const
301 {
302  return calendar()->week(date(), yearNum);
303 }
304 
305 int KLocalizedDate::week(KLocale::WeekNumberSystem weekNumberSystem, int *yearNum) const
306 {
307  return calendar()->week(date(), weekNumberSystem, yearNum);
308 }
309 
311 {
312  return calendar()->monthsInYear(date());
313 }
314 
316 {
317  return calendar()->weeksInYear(date());
318 }
319 
321 {
322  return calendar()->weeksInYear(date(), weekNumberSystem);
323 }
324 
326 {
327  return calendar()->daysInYear(date());
328 }
329 
331 {
332  return calendar()->daysInMonth(date());
333 }
334 
336 {
337  return calendar()->daysInWeek(date());
338 }
339 
341 {
342  return calendar()->isLeapYear(date());
343 }
344 
345 /*****************************************************************************
346  *
347  * Date Formatting Section
348  *
349  *****************************************************************************/
350 
352 {
353  return calendar()->formatDate(date(), toFormat);
354 }
355 
357 {
358  return calendar()->formatDate(date(), toFormat, formatStandard);
359 }
360 
363  KLocale::WeekNumberSystem weekNumberSystem) const
364 {
365  return calendar()->formatDate(date(), component, format, weekNumberSystem);
366 }
367 
368 /*****************************************************************************
369  *
370  * Date Parsing Section
371  *
372  *****************************************************************************/
373 
375  KLocale::DateTimeParseMode parseMode,
376  const KCalendarSystem *calendar)
377 {
378  Q_UNUSED(parseMode);
379  if (!calendar) {
380  calendar = KLocale::global()->calendar();
381  }
382  return KLocalizedDate(calendar->readDate(dateString));
383 }
384 
386  KLocale::ReadDateFlags formatFlags,
387  KLocale::DateTimeParseMode parseMode,
388  const KCalendarSystem *calendar)
389 {
390  Q_UNUSED(parseMode);
391  if (!calendar) {
392  calendar = KLocale::global()->calendar();
393  }
394  return KLocalizedDate(calendar->readDate(dateString, formatFlags));
395 }
396 
398  const QString &dateFormat,
399  KLocale::DateTimeParseMode parseMode,
400  KLocale::DateTimeFormatStandard formatStandard,
401  const KCalendarSystem *calendar)
402 {
403  Q_UNUSED(parseMode);
404  if (!calendar) {
405  calendar = KLocale::global()->calendar();
406  }
407  return KLocalizedDate(calendar->readDate(dateString, dateFormat, nullptr, formatStandard));
408 }
409 
410 /*****************************************************************************
411  *
412  * Date Maths Section
413  *
414  *****************************************************************************/
415 
417 {
418  KLocalizedDate newDate;
419  newDate = *this;
420  newDate.setDate(calendar()->addYears(date(), years));
421  return newDate;
422 }
423 
425 {
426  d->m_date = calendar()->addYears(date(), years);
427  return isValid();
428 }
429 
431 {
432  KLocalizedDate newDate(*this);
433  newDate.setDate(calendar()->addMonths(date(), months));
434  return newDate;
435 }
436 
438 {
439  d->m_date = calendar()->addMonths(date(), months);
440  return isValid();
441 }
442 
444 {
445  KLocalizedDate newDate(*this);
446  newDate.setDate(calendar()->addDays(date(), days));
447  return newDate;
448 }
449 
451 {
452  d->m_date = calendar()->addDays(date(), days);
453  return isValid();
454 }
455 
457  int *yearsDiff, int *monthsDiff, int *daysDiff, int *direction) const
458 {
459  dateDifference(toDate.date(), yearsDiff, monthsDiff, daysDiff, direction);
460 }
461 
463  int *yearsDiff, int *monthsDiff, int *daysDiff, int *direction) const
464 {
465  calendar()->dateDifference(date(), toDate, yearsDiff, monthsDiff, daysDiff, direction);
466 }
467 
469 {
470  return yearsDifference(toDate.date());
471 }
472 
473 int KLocalizedDate::yearsDifference(const QDate &toDate) const
474 {
475  return calendar()->yearsDifference(date(), toDate);
476 }
477 
479 {
480  return monthsDifference(toDate.date());
481 }
482 
483 int KLocalizedDate::monthsDifference(const QDate &toDate) const
484 {
485  return calendar()->monthsDifference(date(), toDate);
486 }
487 
489 {
490  return daysDifference(toDate.date());
491 }
492 
493 int KLocalizedDate::daysDifference(const QDate &toDate) const
494 {
495  return calendar()->daysDifference(date(), toDate);
496 }
497 
499 {
500  KLocalizedDate newDate(*this);
501  newDate.setDate(calendar()->firstDayOfYear(date()));
502  return newDate;
503 }
504 
506 {
507  KLocalizedDate newDate(*this);
508  newDate.setDate(calendar()->lastDayOfYear(date()));
509  return newDate;
510 }
511 
513 {
514  KLocalizedDate newDate(*this);
515  newDate.setDate(calendar()->firstDayOfMonth(date()));
516  return newDate;
517 }
518 
520 {
521  KLocalizedDate newDate(*this);
522  newDate.setDate(calendar()->lastDayOfMonth(date()));
523  return newDate;
524 }
525 
526 /*****************************************************************************
527  *
528  * Date Operators Section
529  *
530  *****************************************************************************/
531 
533 {
534  return (date() == rhs.date());
535 }
536 
537 bool KLocalizedDate::operator==(const QDate &rhs) const
538 {
539  return (date() == rhs);
540 }
541 
543 {
544  return (date() != rhs.date());
545 }
546 
547 bool KLocalizedDate::operator!=(const QDate &rhs) const
548 {
549  return (date() != rhs);
550 }
551 
553 {
554  return (date() < rhs.date());
555 }
556 
557 bool KLocalizedDate::operator<(const QDate &rhs) const
558 {
559  return (date() < rhs);
560 }
561 
563 {
564  return (d->m_date <= rhs.date());
565 }
566 
567 bool KLocalizedDate::operator<=(const QDate &rhs) const
568 {
569  return (date() <= rhs);
570 }
571 
573 {
574  return (date() > rhs.date());
575 }
576 
577 bool KLocalizedDate::operator>(const QDate &rhs) const
578 {
579  return (date() > rhs);
580 }
581 
583 {
584  return (date() >= rhs.date());
585 }
586 
587 bool KLocalizedDate::operator>=(const QDate &rhs) const
588 {
589  return (date() >= rhs);
590 }
591 
593 {
594  return out << (quint32)(date.toJulianDay()) << date.calendar()->calendarSystem();
595 }
596 
598 {
599  quint32 jd;
600  int calendarSystem;
601  in >> jd >> calendarSystem;
602  date.setDate(QDate::fromJulianDay(jd));
603  date.setCalendarSystem((KLocale::CalendarSystem)calendarSystem);
604  return in;
605 }
606 
608 {
610  dbg.nospace() << "KLocalizedDate(" << date.formatDate(KLocale::IsoDate) << ", "
611  << date.calendar()->calendarLabel() << ')';
612  } else {
613  dbg.nospace() << "KLocalizedDate(" << date.formatDate(KLocale::IsoDate) << ", "
614  << date.calendar()->calendarLabel() << ')'
615  << " = QDate(" << date.date().toString(Qt::ISODate) << ')';
616  }
617  return dbg.space();
618 }
friend QDataStream & operator>>(QDataStream &in, KLocalizedDate &date)
Data stream input operator.
bool setCurrentDate()
Set the date to today&#39;s date.
void dateDifference(const KLocalizedDate &toDate, int *yearsDiff, int *monthsDiff, int *daysDiff, int *direction) const
Returns the difference between this and another date in years, months and days in the current Calenda...
bool operator<(const KLocalizedDate &other) const
KLocalizedDate less than operator.
The Era Name portion of a date.
Definition: klocale.h:736
virtual QDate readDate(const QString &str, bool *ok=nullptr) const
Converts a localized date string to a QDate.
bool setDateIsoWeek(QDate &date, int year, int isoWeekNumber, int dayOfIsoWeek) const
KLocale(KSharedConfig::Ptr config=KSharedConfig::Ptr())
Constructs a KLocale.
Definition: klocale.cpp:42
QString toString(Qt::DateFormat format) const const
void setCalendarSystem(KLocale::CalendarSystem calendarSystem)
Set the Calendar System used for this date instance only.
int daysInWeek() const
Returns the number of days in the week.
KLocalizedDate lastDayOfYear() const
Returns a KLocalizedDate containing the last day of the currently set year.
int week(int *yearNum=nullptr) const
Returns the localized Week Number for the date.
void getDate(int *year, int *month, int *day) const
Returns the year, month and day portion of the date in the current Calendar System.
int yearInEra() const
Returns the Year In Era portion of the date in the current calendar system, for example 1 for "1 BC"...
DateFormat
Format for date string.
Definition: klocale.h:783
QDebug & nospace()
KLocalizedDate firstDayOfMonth() const
Returns a KLocalizedDate containing the first day of the currently set month.
virtual int day(const QDate &date) const
Returns the day portion of a given date in the current calendar system.
bool isNull() const
Returns whether the date is null, i.e.
QDate date() const
Returns the currently set date as a QDate.
int daysInWeek(const QDate &date) const
Returns the number of days in the given week.
static KLocalizedDate fromJulianDay(int jd)
Returns a KLocalizedDate set the required Julian Day number in the Global Locale and Calendar System...
QString eraName() const
Returns the Era Name portion of the date in the current calendar system, for example "AD" or "Anno Do...
int day() const
Returns the day portion of the date in the current calendar system.
static QString calendarLabel(KLocale::CalendarSystem calendarSystem, const KLocale *locale=KLocale::global())
KCalendarSystem abstract base class, provides support for local Calendar Systems in KDE...
DateTimeComponentFormat
Definition: klocale.h:770
int weeksInYear(const QDate &date, KLocale::WeekNumberSystem weekNumberSystem=KLocale::DefaultWeekNumber) const
KLocalizedDate firstDayOfYear() const
Returns a KLocalizedDate containing the first day of the currently set year.
KLocalizedDate addYears(int years) const
Returns a KLocalizedDate containing a date years years later.
virtual KLocale::CalendarSystem calendarSystem() const =0
int weeksInYear() const
Returns the number of localized weeks in the currently set year.
bool operator>(const KLocalizedDate &other) const
KLocalizedDate greater than operator.
const KCalendarSystem * calendar() const
Returns a pointer to the calendar system object.
Definition: klocale.cpp:626
int toJulianDay() const
Returns the currently set date as a Julian Day number.
virtual int dayOfYear(const QDate &date) const
Returns the day number of year for the given date.
CalendarSystem
Definition: klocale.h:646
bool isLeapYear(int year) const
Returns whether a given year is a leap year.
KLocale::CalendarSystem calendarSystem() const
Definition: klocale.cpp:621
virtual QString formatDate(const QDate &fromDate, KLocale::DateFormat toFormat=KLocale::LongDate) const
Returns a string formatted to the current locale&#39;s conventions regarding dates.
int monthsInYear(const QDate &date) const
Returns number of months in the given year.
bool isNull() const const
bool setDate(const QDate &date)
Set the date using a QDate.
~KLocalizedDate()
Destructor.
A class representing a date localized using the local calendar system, language and formats...
WeekNumberSystem
Definition: klocale.h:681
int monthsInYear() const
Returns number of months in the year.
virtual int month(const QDate &date) const
Returns the month portion of a given date in the current calendar system.
int dayOfYear() const
Returns the day number of year for the date.
int week(const QDate &date, int *yearNum) const
Returns the localized Week Number for the date.
KLocalizedDate addMonths(int months) const
Returns a KLocalizedDate containing a date months months later.
int yearsDifference(const QDate &fromDate, const QDate &toDate) const
Returns the difference between two dates in completed calendar years.
const KCalendarSystem * calendar() const
Returns a pointer to the Calendar System object used by this date instance.
void dateDifference(const QDate &fromDate, const QDate &toDate, int *yearsDiff, int *monthsDiff, int *daysDiff, int *direction) const
Returns the difference between two dates in years, months and days.
ReadDateFlags
Flags for readDate()
Definition: klocale.h:1108
bool isValid() const
Returns whether the date is valid in the current Calendar System.
KLocalizedDate & operator=(const KLocalizedDate &rhs)
Assignment operator.
bool operator==(const KLocalizedDate &other) const
KLocalizedDate equality operator.
int monthsDifference(const KLocalizedDate &toDate) const
Returns the difference between this and another date in completed calendar months in the current Cale...
DateTimeComponent
Definition: klocale.h:728
QString formatDate(KLocale::DateFormat dateFormat=KLocale::LongDate) const
Returns the Date as a localized string in the requested standard Locale format.
static KLocale * global()
Return the global KLocale instance.
Definition: klocale.cpp:309
static KLocalizedDate fromDate(const QDate &date)
Returns a KLocalizedDate set the required date in the Global Locale and Calendar System.
int month() const
Returns the month portion of the date in the current calendar system.
int monthsDifference(const QDate &fromDate, const QDate &toDate) const
Returns the difference between two dates in completed calendar months The returned value will be nega...
QDebug & space()
QString eraYear() const
Returns the Era Year portion of the date in the current calendar system, for example "2000 AD" or "He...
int daysInMonth() const
Returns the number of days in the month.
bool isValid(int year, int month, int day) const
Returns whether a given date is valid in this calendar system.
friend QDataStream & operator<<(QDataStream &out, const KLocalizedDate &date)
Data stream output operator.
bool operator!=(const KLocalizedDate &other) const
KLocalizedDate inequality operator.
virtual int year(const QDate &date) const
Returns the year portion of a given date in the current calendar system.
KLocalizedDate lastDayOfMonth() const
Returns a KLocalizedDate containing the last day of the currently set month.
The Era and Year portion of a date.
Definition: klocale.h:737
int dayOfWeek() const
Returns the weekday number for the date.
static KCalendarSystem * create(KLocale::CalendarSystem calendarSystem, const KLocale *locale)
int yearInEra(const QDate &date) const
KLocalizedDate addDays(int days) const
Returns a KLocalizedDate containing a date days days later.
virtual QDate addMonths(const QDate &date, int nmonths) const
Returns a QDate containing a date nmonths months later.
int daysInYear(const QDate &date) const
Returns the number of days in the given year.
KLocale provides support for language and country specific stuff.
Definition: klocale.h:75
DateTimeFormatStandard
Definition: klocale.h:694
bool isLeapYear() const
Returns whether the currently set date falls in a Leap Year in the current Calendar System...
QDate currentDate()
static KLocalizedDate readDate(const QString &dateString, KLocale::DateTimeParseMode parseMode=KLocale::LiberalParsing, const KCalendarSystem *calendar=nullptr)
Converts a localized date string to a KLocalizedDate using either the Global Calendar System and Loca...
ISO-8601 Date format YYYY-MM-DD, e.g.
Definition: klocale.h:792
KLocalizedDate(const QDate &date=QDate(), const KCalendarSystem *calendar=nullptr)
Constructs a localized date with the given date.
int daysDifference(const KLocalizedDate &toDate) const
Returns the difference between this and another date in days The returned value will be negative if t...
bool addMonthsTo(int months)
Add months onto this date instance.
virtual QDate addYears(const QDate &date, int nyears) const
Returns a QDate containing a date nyears years later.
QDate fromJulianDay(qint64 jd)
QDate addDays(const QDate &date, int ndays) const
Returns a QDate containing a date ndays days later.
bool addDaysTo(int days)
Add days onto this date instance.
int daysDifference(const QDate &fromDate, const QDate &toDate) const
Returns the difference between two dates in days The returned value will be negative if fromDate > to...
int daysInYear() const
Returns the number of days in the year.
KLocale::CalendarSystem calendarSystem()
Returns the Calendar System used by this localized date instance.
void getDate(const QDate date, int *year, int *month, int *day) const
virtual bool setDate(QDate &date, int year, int month, int day) const
Changes the date&#39;s year, month and day.
static KLocalizedDate currentDate()
Returns a KLocalizedDate set to today&#39;s date in the Global Locale and Calendar System.
bool addYearsTo(int years)
Add years onto this date instance.
int dayOfWeek(const QDate &date) const
Returns the weekday number for the given date.
DateTimeParseMode
Definition: klocale.h:705
int year() const
Returns the year portion of the date in the current calendar system.
bool operator>=(const KLocalizedDate &other) const
KLocalizedDate greater than or equal to operator.
int yearsDifference(const KLocalizedDate &toDate) const
Returns the difference between this and another date in completed calendar years in the current Calen...
int daysInMonth(const QDate &date) const
Returns the number of days in the given month.
bool operator<=(const KLocalizedDate &other) const
KLocalizedDate less than or equal to operator.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Oct 28 2020 23:03:26 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.