KDECore
klocalizeddate.cpp
Go to the documentation of this file.
00001 /* 00002 Copyright 2010 John Layt <john@layt.net> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "klocalizeddate.h" 00021 00022 #include "kglobal.h" 00023 #include "kdebug.h" 00024 00025 /***************************************************************************** 00026 * 00027 * Private Section 00028 * 00029 *****************************************************************************/ 00030 00031 class KLocalizedDatePrivate : public QSharedData 00032 { 00033 public: 00034 explicit KLocalizedDatePrivate(const QDate &date, const KCalendarSystem *calendar, bool manageCalendar); 00035 KLocalizedDatePrivate(const KLocalizedDatePrivate &rhs); 00036 KLocalizedDatePrivate &operator=(const KLocalizedDatePrivate &rhs); 00037 virtual ~KLocalizedDatePrivate(); 00038 00039 QDate m_date; 00040 const KCalendarSystem *m_calendar; 00041 bool m_manageCalendar; 00042 }; 00043 00044 KLocalizedDatePrivate::KLocalizedDatePrivate(const QDate &date, const KCalendarSystem *calendar, bool manageCalendar) 00045 : QSharedData(), 00046 m_date(date), 00047 m_calendar(calendar), 00048 m_manageCalendar(manageCalendar) 00049 { 00050 } 00051 00052 KLocalizedDatePrivate::KLocalizedDatePrivate(const KLocalizedDatePrivate &rhs) 00053 : QSharedData(rhs), 00054 m_date(rhs.m_date), 00055 m_calendar(rhs.m_calendar), 00056 m_manageCalendar(rhs.m_manageCalendar) 00057 { 00058 // If we're managing the calendar object, then take a copy, 00059 // i.e. user called setCalendarSystem() rather than passing a custom one into the constructor 00060 if (m_manageCalendar) { 00061 m_calendar = KCalendarSystem::create(m_calendar->calendarSystem(), new KLocale(*m_calendar->locale())); 00062 } 00063 } 00064 00065 KLocalizedDatePrivate &KLocalizedDatePrivate::operator=(const KLocalizedDatePrivate &rhs) 00066 { 00067 m_date = rhs.m_date; 00068 m_calendar = rhs.m_calendar; 00069 m_manageCalendar = rhs.m_manageCalendar; 00070 // If we're managing the calendar object, then take a copy, 00071 // i.e. user called setCalendarSystem() rather than passing a custom one into the constructor 00072 if (rhs.m_manageCalendar) { 00073 m_calendar = KCalendarSystem::create(m_calendar->calendarSystem(), new KLocale(*m_calendar->locale())); 00074 } 00075 return *this; 00076 } 00077 00078 KLocalizedDatePrivate::~KLocalizedDatePrivate() 00079 { 00080 // If we're managing the calendar object, then delete it, 00081 // i.e. user called setCalendarSystem() rather than passing a custom one into the constructor 00082 if (m_manageCalendar) { 00083 delete m_calendar; 00084 } 00085 } 00086 00087 /***************************************************************************** 00088 * 00089 * Date Creation Section 00090 * 00091 *****************************************************************************/ 00092 00093 KLocalizedDate::KLocalizedDate(const QDate &date, const KCalendarSystem *calendarSystem) 00094 : d(new KLocalizedDatePrivate(date, calendarSystem, false)) 00095 { 00096 } 00097 00098 KLocalizedDate::KLocalizedDate(int year, int month, int day, const KCalendarSystem *calendarSystem) 00099 : d(new KLocalizedDatePrivate(QDate(), calendarSystem, false)) 00100 { 00101 setDate(year, month, day); 00102 } 00103 00104 KLocalizedDate::KLocalizedDate(const KLocalizedDate &rhs) 00105 : d(new KLocalizedDatePrivate(*rhs.d)) 00106 { 00107 } 00108 00109 KLocalizedDate &KLocalizedDate::operator=(const KLocalizedDate &rhs) 00110 { 00111 *d = *rhs.d; 00112 return *this; 00113 } 00114 00115 KLocalizedDate &KLocalizedDate::operator=(const QDate &rhs) 00116 { 00117 d->m_date = rhs; 00118 return *this; 00119 } 00120 00121 KLocalizedDate::~KLocalizedDate() 00122 { 00123 } 00124 00125 /***************************************************************************** 00126 * 00127 * Calendar System Section 00128 * 00129 *****************************************************************************/ 00130 00131 void KLocalizedDate::setCalendarSystem(KLocale::CalendarSystem calendarSystem) 00132 { 00133 if (calendarSystem == calendar()->calendarSystem()) { 00134 return; 00135 } 00136 KCalendarSystem *newCalendar = KCalendarSystem::create(calendarSystem, 00137 new KLocale(*calendar()->locale())); 00138 if (d->m_manageCalendar) { 00139 delete d->m_calendar; 00140 } 00141 d->m_calendar = newCalendar; 00142 } 00143 00144 KLocale::CalendarSystem KLocalizedDate::calendarSystem() 00145 { 00146 return calendar()->calendarSystem(); 00147 } 00148 00149 const KCalendarSystem *KLocalizedDate::calendar() const 00150 { 00151 if (d->m_calendar) { 00152 return d->m_calendar; 00153 } 00154 return KGlobal::locale()->calendar(); 00155 } 00156 00157 /***************************************************************************** 00158 * 00159 * Date Status Section 00160 * 00161 *****************************************************************************/ 00162 00163 bool KLocalizedDate::isNull() const 00164 { 00165 return date().isNull(); 00166 } 00167 00168 bool KLocalizedDate::isValid() const 00169 { 00170 return calendar()->isValid(date()); 00171 } 00172 00173 /***************************************************************************** 00174 * 00175 * Date Setting Section 00176 * 00177 *****************************************************************************/ 00178 00179 bool KLocalizedDate::setDate(const QDate &date) 00180 { 00181 d->m_date = date; 00182 return isValid(); 00183 } 00184 00185 bool KLocalizedDate::setDate(int year, int month, int day) 00186 { 00187 calendar()->setDate(d->m_date, year, month, day); 00188 return isValid(); 00189 } 00190 00191 bool KLocalizedDate::setDate(int year, int dayOfYear) 00192 { 00193 calendar()->setDate(d->m_date, year, dayOfYear); 00194 return isValid(); 00195 } 00196 00197 bool KLocalizedDate::setDate(QString eraName, int yearInEra, int month, int day) 00198 { 00199 calendar()->setDate(d->m_date, eraName, yearInEra, month, day); 00200 return isValid(); 00201 } 00202 00203 bool KLocalizedDate::setDate(KLocale::WeekNumberSystem weekNumberSystem, int year, int isoWeekNumber, int dayOfIsoWeek) 00204 { 00205 Q_UNUSED(weekNumberSystem); // Only support ISO Week at the moment 00206 calendar()->setDateIsoWeek(d->m_date, year, isoWeekNumber, dayOfIsoWeek); 00207 return isValid(); 00208 } 00209 00210 bool KLocalizedDate::setCurrentDate() 00211 { 00212 d->m_date = QDate::currentDate(); 00213 return isValid(); 00214 } 00215 00216 /***************************************************************************** 00217 * 00218 * Static Date Creation Section 00219 * 00220 *****************************************************************************/ 00221 00222 KLocalizedDate KLocalizedDate::currentDate() 00223 { 00224 return KLocalizedDate(QDate::currentDate()); 00225 } 00226 00227 KLocalizedDate KLocalizedDate::fromDate(const QDate &date) 00228 { 00229 return KLocalizedDate(date); 00230 } 00231 00232 KLocalizedDate KLocalizedDate::fromJulianDay(int jd) 00233 { 00234 return KLocalizedDate(QDate::fromJulianDay(jd)); 00235 } 00236 00237 /***************************************************************************** 00238 * 00239 * Date Componant Section 00240 * 00241 *****************************************************************************/ 00242 00243 int KLocalizedDate::toJulianDay() const 00244 { 00245 return d->m_date.toJulianDay(); 00246 } 00247 00248 QDate KLocalizedDate::date() const 00249 { 00250 return d->m_date; 00251 } 00252 00253 void KLocalizedDate::getDate(int *year, int *month, int *day) const 00254 { 00255 calendar()->getDate(date(), year, month, day); 00256 } 00257 00258 int KLocalizedDate::year() const 00259 { 00260 return calendar()->year(date()); 00261 } 00262 00263 int KLocalizedDate::month() const 00264 { 00265 return calendar()->month(date()); 00266 } 00267 00268 int KLocalizedDate::day() const 00269 { 00270 return calendar()->day(date()); 00271 } 00272 00273 QString KLocalizedDate::eraName() const 00274 { 00275 return formatDate(KLocale::EraName); 00276 } 00277 00278 QString KLocalizedDate::eraYear() const 00279 { 00280 return formatDate(KLocale::EraYear); 00281 } 00282 00283 int KLocalizedDate::yearInEra() const 00284 { 00285 return calendar()->yearInEra(date()); 00286 } 00287 00288 int KLocalizedDate::dayOfYear() const 00289 { 00290 return calendar()->dayOfYear(date()); 00291 } 00292 00293 int KLocalizedDate::dayOfWeek() const 00294 { 00295 return calendar()->dayOfWeek(date()); 00296 } 00297 00298 int KLocalizedDate::week(int *yearNum) const 00299 { 00300 return calendar()->week(date(), yearNum); 00301 } 00302 00303 int KLocalizedDate::week(KLocale::WeekNumberSystem weekNumberSystem, int *yearNum) const 00304 { 00305 return calendar()->week(date(), weekNumberSystem, yearNum); 00306 } 00307 00308 int KLocalizedDate::monthsInYear() const 00309 { 00310 return calendar()->monthsInYear(date()); 00311 } 00312 00313 int KLocalizedDate::weeksInYear() const 00314 { 00315 return calendar()->weeksInYear(date()); 00316 } 00317 00318 int KLocalizedDate::weeksInYear(KLocale::WeekNumberSystem weekNumberSystem) const 00319 { 00320 return calendar()->weeksInYear(date(), weekNumberSystem); 00321 } 00322 00323 int KLocalizedDate::daysInYear() const 00324 { 00325 return calendar()->daysInYear(date()); 00326 } 00327 00328 int KLocalizedDate::daysInMonth() const 00329 { 00330 return calendar()->daysInMonth(date()); 00331 } 00332 00333 int KLocalizedDate::daysInWeek() const 00334 { 00335 return calendar()->daysInWeek(date()); 00336 } 00337 00338 bool KLocalizedDate::isLeapYear() const 00339 { 00340 return calendar()->isLeapYear(date()); 00341 } 00342 00343 /***************************************************************************** 00344 * 00345 * Date Formatting Section 00346 * 00347 *****************************************************************************/ 00348 00349 QString KLocalizedDate::formatDate(KLocale::DateFormat toFormat) const 00350 { 00351 return calendar()->formatDate(date(), toFormat); 00352 } 00353 00354 QString KLocalizedDate::formatDate(const QString &toFormat, KLocale::DateTimeFormatStandard formatStandard) const 00355 { 00356 return calendar()->formatDate(date(), toFormat, formatStandard); 00357 } 00358 00359 QString KLocalizedDate::formatDate(KLocale::DateTimeComponent component, 00360 KLocale::DateTimeComponentFormat format, 00361 KLocale::WeekNumberSystem weekNumberSystem) const 00362 { 00363 return calendar()->formatDate(date(), component, format, weekNumberSystem); 00364 } 00365 00366 /***************************************************************************** 00367 * 00368 * Date Parsing Section 00369 * 00370 *****************************************************************************/ 00371 00372 KLocalizedDate KLocalizedDate::readDate(const QString &dateString, 00373 KLocale::DateTimeParseMode parseMode, 00374 const KCalendarSystem *calendar) 00375 { 00376 Q_UNUSED(parseMode); 00377 if (!calendar) { 00378 calendar = KGlobal::locale()->calendar(); 00379 } 00380 return KLocalizedDate(calendar->readDate(dateString)); 00381 } 00382 00383 KLocalizedDate KLocalizedDate::readDate(const QString &dateString, 00384 KLocale::ReadDateFlags formatFlags, 00385 KLocale::DateTimeParseMode parseMode, 00386 const KCalendarSystem *calendar) 00387 { 00388 Q_UNUSED(parseMode); 00389 if (!calendar) { 00390 calendar = KGlobal::locale()->calendar(); 00391 } 00392 return KLocalizedDate(calendar->readDate(dateString, formatFlags)); 00393 } 00394 00395 KLocalizedDate KLocalizedDate::readDate(const QString &dateString, 00396 const QString &dateFormat, 00397 KLocale::DateTimeParseMode parseMode, 00398 KLocale::DateTimeFormatStandard formatStandard, 00399 const KCalendarSystem *calendar) 00400 { 00401 Q_UNUSED(parseMode); 00402 if (!calendar) { 00403 calendar = KGlobal::locale()->calendar(); 00404 } 00405 return KLocalizedDate(calendar->readDate(dateString, dateFormat, 0, formatStandard)); 00406 } 00407 00408 /***************************************************************************** 00409 * 00410 * Date Maths Section 00411 * 00412 *****************************************************************************/ 00413 00414 KLocalizedDate KLocalizedDate::addYears(int years) const 00415 { 00416 KLocalizedDate newDate; 00417 newDate = *this; 00418 newDate.setDate(calendar()->addYears(date(), years)); 00419 return newDate; 00420 } 00421 00422 bool KLocalizedDate::addYearsTo(int years) 00423 { 00424 d->m_date = calendar()->addYears(date(), years); 00425 return isValid(); 00426 } 00427 00428 KLocalizedDate KLocalizedDate::addMonths(int months) const 00429 { 00430 KLocalizedDate newDate(*this); 00431 newDate.setDate(calendar()->addMonths(date(), months)); 00432 return newDate; 00433 } 00434 00435 bool KLocalizedDate::addMonthsTo(int months) 00436 { 00437 d->m_date = calendar()->addMonths(date(), months); 00438 return isValid(); 00439 } 00440 00441 KLocalizedDate KLocalizedDate::addDays(int days) const 00442 { 00443 KLocalizedDate newDate(*this); 00444 newDate.setDate(calendar()->addDays(date(), days)); 00445 return newDate; 00446 } 00447 00448 bool KLocalizedDate::addDaysTo(int days) 00449 { 00450 d->m_date = calendar()->addDays(date(), days); 00451 return isValid(); 00452 } 00453 00454 void KLocalizedDate::dateDifference(const KLocalizedDate &toDate, 00455 int *yearsDiff, int *monthsDiff, int *daysDiff, int *direction) const 00456 { 00457 dateDifference(toDate.date(), yearsDiff, monthsDiff, daysDiff, direction); 00458 } 00459 00460 void KLocalizedDate::dateDifference(const QDate &toDate, 00461 int *yearsDiff, int *monthsDiff, int *daysDiff, int *direction) const 00462 { 00463 calendar()->dateDifference(date(), toDate, yearsDiff, monthsDiff, daysDiff, direction); 00464 } 00465 00466 int KLocalizedDate::yearsDifference(const KLocalizedDate &toDate) const 00467 { 00468 return yearsDifference(toDate.date()); 00469 } 00470 00471 int KLocalizedDate::yearsDifference(const QDate &toDate) const 00472 { 00473 return calendar()->yearsDifference(date(), toDate); 00474 } 00475 00476 int KLocalizedDate::monthsDifference(const KLocalizedDate &toDate) const 00477 { 00478 return monthsDifference(toDate.date()); 00479 } 00480 00481 int KLocalizedDate::monthsDifference(const QDate &toDate) const 00482 { 00483 return calendar()->monthsDifference(date(), toDate); 00484 } 00485 00486 int KLocalizedDate::daysDifference(const KLocalizedDate &toDate) const 00487 { 00488 return daysDifference(toDate.date()); 00489 } 00490 00491 int KLocalizedDate::daysDifference(const QDate &toDate) const 00492 { 00493 return calendar()->daysDifference(date(), toDate); 00494 } 00495 00496 KLocalizedDate KLocalizedDate::firstDayOfYear() const 00497 { 00498 KLocalizedDate newDate(*this); 00499 newDate.setDate(calendar()->firstDayOfYear(date())); 00500 return newDate; 00501 } 00502 00503 KLocalizedDate KLocalizedDate::lastDayOfYear() const 00504 { 00505 KLocalizedDate newDate(*this); 00506 newDate.setDate(calendar()->lastDayOfYear(date())); 00507 return newDate; 00508 } 00509 00510 KLocalizedDate KLocalizedDate::firstDayOfMonth() const 00511 { 00512 KLocalizedDate newDate(*this); 00513 newDate.setDate(calendar()->firstDayOfMonth(date())); 00514 return newDate; 00515 } 00516 00517 KLocalizedDate KLocalizedDate::lastDayOfMonth() const 00518 { 00519 KLocalizedDate newDate(*this); 00520 newDate.setDate(calendar()->lastDayOfMonth(date())); 00521 return newDate; 00522 } 00523 00524 /***************************************************************************** 00525 * 00526 * Date Operators Section 00527 * 00528 *****************************************************************************/ 00529 00530 bool KLocalizedDate::operator==(const KLocalizedDate &rhs) const 00531 { 00532 return (date() == rhs.date()); 00533 } 00534 00535 bool KLocalizedDate::operator==(const QDate &rhs) const 00536 { 00537 return (date() == rhs); 00538 } 00539 00540 bool KLocalizedDate::operator!=(const KLocalizedDate &rhs) const 00541 { 00542 return (date() != rhs.date()); 00543 } 00544 00545 bool KLocalizedDate::operator!=(const QDate &rhs) const 00546 { 00547 return (date() != rhs); 00548 } 00549 00550 bool KLocalizedDate::operator<(const KLocalizedDate &rhs) const 00551 { 00552 return (date() < rhs.date()); 00553 } 00554 00555 bool KLocalizedDate::operator<(const QDate &rhs) const 00556 { 00557 return (date() < rhs); 00558 } 00559 00560 bool KLocalizedDate::operator<=(const KLocalizedDate &rhs) const 00561 { 00562 return (d->m_date <= rhs.date()); 00563 } 00564 00565 bool KLocalizedDate::operator<=(const QDate &rhs) const 00566 { 00567 return (date() <= rhs); 00568 } 00569 00570 bool KLocalizedDate::operator>(const KLocalizedDate &rhs) const 00571 { 00572 return (date() > rhs.date()); 00573 } 00574 00575 bool KLocalizedDate::operator>(const QDate &rhs) const 00576 { 00577 return (date() > rhs); 00578 } 00579 00580 bool KLocalizedDate::operator>=(const KLocalizedDate &rhs) const 00581 { 00582 return (date() >= rhs.date()); 00583 } 00584 00585 bool KLocalizedDate::operator>=(const QDate &rhs) const 00586 { 00587 return (date() >= rhs); 00588 } 00589 00590 QDataStream &operator<<(QDataStream &out, const KLocalizedDate &date) 00591 { 00592 return out << (quint32)(date.toJulianDay()) << date.calendar()->calendarSystem(); 00593 } 00594 00595 QDataStream &operator>>(QDataStream &in, KLocalizedDate &date) 00596 { 00597 quint32 jd; 00598 int calendarSystem; 00599 in >> jd >> calendarSystem; 00600 date.setDate(QDate::fromJulianDay(jd)); 00601 date.setCalendarSystem((KLocale::CalendarSystem)calendarSystem); 00602 return in; 00603 } 00604 00605 QDebug operator<<(QDebug dbg, const KLocalizedDate &date) 00606 { 00607 if (date.calendar()->calendarSystem() == KLocale::QDateCalendar) { 00608 dbg.nospace() << "KLocalizedDate(" << date.formatDate(KLocale::IsoDate) << ", " 00609 << date.calendar()->calendarLabel() << ')'; 00610 } else { 00611 dbg.nospace() << "KLocalizedDate(" << date.formatDate(KLocale::IsoDate) << ", " 00612 << date.calendar()->calendarLabel() << ')' 00613 << " = QDate(" << date.date().toString(Qt::ISODate) << ')'; 00614 } 00615 return dbg.space(); 00616 }
KDE 4.7 API Reference