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

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()->weekNumber(date(), yearNum);
00301 }
00302 
00303 int KLocalizedDate::week(KLocale::WeekNumberSystem weekNumberSystem, int *yearNum) const
00304 {
00305     Q_UNUSED(weekNumberSystem);
00306     return calendar()->weekNumber(date(), yearNum);
00307 }
00308 
00309 int KLocalizedDate::monthsInYear() const
00310 {
00311     return calendar()->monthsInYear(date());
00312 }
00313 
00314 int KLocalizedDate::weeksInYear() const
00315 {
00316     return calendar()->weeksInYear(date());
00317 }
00318 
00319 int KLocalizedDate::weeksInYear(KLocale::WeekNumberSystem weekNumberSystem) const
00320 {
00321     Q_UNUSED(weekNumberSystem);
00322     return calendar()->weeksInYear(date());
00323 }
00324 
00325 int KLocalizedDate::daysInYear() const
00326 {
00327     return calendar()->daysInYear(date());
00328 }
00329 
00330 int KLocalizedDate::daysInMonth() const
00331 {
00332     return calendar()->daysInMonth(date());
00333 }
00334 
00335 int KLocalizedDate::daysInWeek() const
00336 {
00337     return calendar()->daysInWeek(date());
00338 }
00339 
00340 bool KLocalizedDate::isLeapYear() const
00341 {
00342     return calendar()->isLeapYear(date());
00343 }
00344 
00345 /*****************************************************************************
00346  *
00347  *                             Date Formatting Section
00348  *
00349  *****************************************************************************/
00350 
00351 QString KLocalizedDate::formatDate(KLocale::DateFormat toFormat) const
00352 {
00353     return calendar()->formatDate(date(), toFormat);
00354 }
00355 
00356 QString KLocalizedDate::formatDate(const QString &toFormat, KLocale::DateTimeFormatStandard formatStandard) const
00357 {
00358     return calendar()->formatDate(date(), toFormat, formatStandard);
00359 }
00360 
00361 QString KLocalizedDate::formatDate(KLocale::DateTimeComponent component,
00362                                    KLocale::DateTimeComponentFormat format,
00363                                    KLocale::WeekNumberSystem weekNumberSystem) const
00364 {
00365     return calendar()->formatDate(date(), component, format, weekNumberSystem);
00366 }
00367 
00368 /*****************************************************************************
00369  *
00370  *                             Date Parsing Section
00371  *
00372  *****************************************************************************/
00373 
00374 KLocalizedDate KLocalizedDate::readDate(const QString &dateString,
00375                                         KLocale::DateTimeParseMode parseMode,
00376                                         const KCalendarSystem *calendar)
00377 {
00378     Q_UNUSED(parseMode);
00379     if (!calendar) {
00380         calendar = KGlobal::locale()->calendar();
00381     }
00382     return KLocalizedDate(calendar->readDate(dateString));
00383 }
00384 
00385 KLocalizedDate KLocalizedDate::readDate(const QString &dateString,
00386                                         KLocale::ReadDateFlags formatFlags,
00387                                         KLocale::DateTimeParseMode parseMode,
00388                                         const KCalendarSystem *calendar)
00389 {
00390     Q_UNUSED(parseMode);
00391     if (!calendar) {
00392         calendar = KGlobal::locale()->calendar();
00393     }
00394     return KLocalizedDate(calendar->readDate(dateString, formatFlags));
00395 }
00396 
00397 KLocalizedDate KLocalizedDate::readDate(const QString &dateString,
00398                                         const QString &dateFormat,
00399                                         KLocale::DateTimeParseMode parseMode,
00400                                         KLocale::DateTimeFormatStandard formatStandard,
00401                                         const KCalendarSystem *calendar)
00402 {
00403     Q_UNUSED(parseMode);
00404     if (!calendar) {
00405         calendar = KGlobal::locale()->calendar();
00406     }
00407     return KLocalizedDate(calendar->readDate(dateString, dateFormat, 0, formatStandard));
00408 }
00409 
00410 /*****************************************************************************
00411  *
00412  *                             Date Maths Section
00413  *
00414  *****************************************************************************/
00415 
00416 KLocalizedDate KLocalizedDate::addYears(int years) const
00417 {
00418     KLocalizedDate newDate;
00419     newDate = *this;
00420     newDate.setDate(calendar()->addYears(date(), years));
00421     return newDate;
00422 }
00423 
00424 bool KLocalizedDate::addYearsTo(int years)
00425 {
00426     d->m_date = calendar()->addYears(date(), years);
00427     return isValid();
00428 }
00429 
00430 KLocalizedDate KLocalizedDate::addMonths(int months) const
00431 {
00432     KLocalizedDate newDate(*this);
00433     newDate.setDate(calendar()->addMonths(date(), months));
00434     return newDate;
00435 }
00436 
00437 bool KLocalizedDate::addMonthsTo(int months)
00438 {
00439     d->m_date = calendar()->addMonths(date(), months);
00440     return isValid();
00441 }
00442 
00443 KLocalizedDate KLocalizedDate::addDays(int days) const
00444 {
00445     KLocalizedDate newDate(*this);
00446     newDate.setDate(calendar()->addDays(date(), days));
00447     return newDate;
00448 }
00449 
00450 bool KLocalizedDate::addDaysTo(int days)
00451 {
00452     d->m_date = calendar()->addDays(date(), days);
00453     return isValid();
00454 }
00455 
00456 void KLocalizedDate::dateDifference(const KLocalizedDate &toDate,
00457                            int *yearsDiff, int *monthsDiff, int *daysDiff, int *direction) const
00458 {
00459     dateDifference(toDate.date(), yearsDiff, monthsDiff, daysDiff, direction);
00460 }
00461 
00462 void KLocalizedDate::dateDifference(const QDate &toDate,
00463                            int *yearsDiff, int *monthsDiff, int *daysDiff, int *direction) const
00464 {
00465     calendar()->dateDifference(date(), toDate, yearsDiff, monthsDiff, daysDiff, direction);
00466 }
00467 
00468 int KLocalizedDate::yearsDifference(const KLocalizedDate &toDate) const
00469 {
00470     return yearsDifference(toDate.date());
00471 }
00472 
00473 int KLocalizedDate::yearsDifference(const QDate &toDate) const
00474 {
00475     return calendar()->yearsDifference(date(), toDate);
00476 }
00477 
00478 int KLocalizedDate::monthsDifference(const KLocalizedDate &toDate) const
00479 {
00480     return monthsDifference(toDate.date());
00481 }
00482 
00483 int KLocalizedDate::monthsDifference(const QDate &toDate) const
00484 {
00485     return calendar()->monthsDifference(date(), toDate);
00486 }
00487 
00488 int KLocalizedDate::daysDifference(const KLocalizedDate &toDate) const
00489 {
00490     return daysDifference(toDate.date());
00491 }
00492 
00493 int KLocalizedDate::daysDifference(const QDate &toDate) const
00494 {
00495     return calendar()->daysDifference(date(), toDate);
00496 }
00497 
00498 KLocalizedDate KLocalizedDate::firstDayOfYear() const
00499 {
00500     KLocalizedDate newDate(*this);
00501     newDate.setDate(calendar()->firstDayOfYear(date()));
00502     return newDate;
00503 }
00504 
00505 KLocalizedDate KLocalizedDate::lastDayOfYear() const
00506 {
00507     KLocalizedDate newDate(*this);
00508     newDate.setDate(calendar()->lastDayOfYear(date()));
00509     return newDate;
00510 }
00511 
00512 KLocalizedDate KLocalizedDate::firstDayOfMonth() const
00513 {
00514     KLocalizedDate newDate(*this);
00515     newDate.setDate(calendar()->firstDayOfMonth(date()));
00516     return newDate;
00517 }
00518 
00519 KLocalizedDate KLocalizedDate::lastDayOfMonth() const
00520 {
00521     KLocalizedDate newDate(*this);
00522     newDate.setDate(calendar()->lastDayOfMonth(date()));
00523     return newDate;
00524 }
00525 
00526 /*****************************************************************************
00527  *
00528  *                             Date Operators Section
00529  *
00530  *****************************************************************************/
00531 
00532 bool KLocalizedDate::operator==(const KLocalizedDate &rhs) const
00533 {
00534     return (date() == rhs.date());
00535 }
00536 
00537 bool KLocalizedDate::operator==(const QDate &rhs) const
00538 {
00539     return (date() == rhs);
00540 }
00541 
00542 bool KLocalizedDate::operator!=(const KLocalizedDate &rhs) const
00543 {
00544     return (date() != rhs.date());
00545 }
00546 
00547 bool KLocalizedDate::operator!=(const QDate &rhs) const
00548 {
00549     return (date() != rhs);
00550 }
00551 
00552 bool KLocalizedDate::operator<(const KLocalizedDate &rhs) const
00553 {
00554     return (date() < rhs.date());
00555 }
00556 
00557 bool KLocalizedDate::operator<(const QDate &rhs) const
00558 {
00559     return (date() < rhs);
00560 }
00561 
00562 bool KLocalizedDate::operator<=(const KLocalizedDate &rhs) const
00563 {
00564     return (d->m_date <= rhs.date());
00565 }
00566 
00567 bool KLocalizedDate::operator<=(const QDate &rhs) const
00568 {
00569     return (date() <= rhs);
00570 }
00571 
00572 bool KLocalizedDate::operator>(const KLocalizedDate &rhs) const
00573 {
00574     return (date() > rhs.date());
00575 }
00576 
00577 bool KLocalizedDate::operator>(const QDate &rhs) const
00578 {
00579     return (date() > rhs);
00580 }
00581 
00582 bool KLocalizedDate::operator>=(const KLocalizedDate &rhs) const
00583 {
00584     return (date() >= rhs.date());
00585 }
00586 
00587 bool KLocalizedDate::operator>=(const QDate &rhs) const
00588 {
00589     return (date() >= rhs);
00590 }
00591 
00592 QDataStream &operator<<(QDataStream &out, const KLocalizedDate &date)
00593 {
00594     return out << (quint32)(date.toJulianDay()) << date.calendar()->calendarSystem();
00595 }
00596 
00597 QDataStream &operator>>(QDataStream &in, KLocalizedDate &date)
00598 {
00599     quint32 jd;
00600     int calendarSystem;
00601     in >> jd >> calendarSystem;
00602     date.setDate(QDate::fromJulianDay(jd));
00603     date.setCalendarSystem((KLocale::CalendarSystem)calendarSystem);
00604     return in;
00605 }
00606 
00607 QDebug operator<<(QDebug dbg, const KLocalizedDate &date)
00608 {
00609     if (date.calendar()->calendarType() == QLatin1String("gregorian")) {
00610         dbg.nospace() << "KLocalizedDate(" << date.formatDate(KLocale::IsoDate) << ", "
00611                       << date.calendar()->calendarLabel() << ')';
00612     } else {
00613         dbg.nospace() << "KLocalizedDate(" << date.formatDate(KLocale::IsoDate) << ", "
00614                       << date.calendar()->calendarLabel() << ')'
00615                       << " = QDate(" << date.date().toString(Qt::ISODate) << ')';
00616     }
00617     return dbg.space();
00618 }

KDECore

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • 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
Generated for kdelibs by doxygen 1.7.3
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal