File indexing completed on 2023-09-24 04:04:42
0001 /* 0002 Copyright (c) 2002 Carlos Moro <cfmoro@correo.uniovi.es> 0003 Copyright (c) 2002-2003 Hans Petter Bieker <bieker@kde.org> 0004 Copyright 2007, 2009, 2010 John Layt <john@layt.net> 0005 0006 This library is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU Library General Public 0008 License as published by the Free Software Foundation; either 0009 version 2 of the License, or (at your option) any later version. 0010 0011 This library is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 Library General Public License for more details. 0015 0016 You should have received a copy of the GNU Library General Public License 0017 along with this library; see the file COPYING.LIB. If not, write to 0018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0019 Boston, MA 02110-1301, USA. 0020 */ 0021 0022 #ifndef KCALENDARSYSTEM_H 0023 #define KCALENDARSYSTEM_H 0024 0025 #include <kdelibs4support_export.h> 0026 #include "klocale.h" // needed for enums 0027 0028 #include <QStringList> 0029 #include <QDate> 0030 0031 class KCalendarSystemPrivate; 0032 class KCalendarEra; 0033 0034 /** 0035 * KCalendarSystem abstract base class, provides support for local Calendar Systems in KDE 0036 * 0037 * Derived classes must be created through the create() static method 0038 */ 0039 class KDELIBS4SUPPORT_EXPORT KCalendarSystem 0040 { 0041 public: 0042 0043 /** 0044 * Format for returned year number / month number / day number as string. 0045 */ 0046 enum StringFormat { 0047 ShortFormat, /**< Short string format, e.g. 2000 = "00" or 6 = "6" */ 0048 LongFormat /**< Long string format, e.g. 2000 = "2000" or 6 = "06" */ 0049 }; 0050 0051 /** 0052 * Format for returned month / day name. 0053 */ 0054 enum MonthNameFormat { 0055 ShortName, /**< Short name format, e.g. "Dec" */ 0056 LongName, /**< Long name format, e.g. "December" */ 0057 ShortNamePossessive, /**< Short name possessive format, e.g. "of Dec" */ 0058 LongNamePossessive, /**< Long name possessive format, e.g. "of December" */ 0059 NarrowName /**< Narrow name format, e.g. "D". @since 4.7 */ 0060 }; 0061 0062 /** 0063 * Format for returned month / day name. 0064 */ 0065 enum WeekDayNameFormat { 0066 ShortDayName, /**< Short name format, e.g. "Fri" */ 0067 LongDayName, /**< Long name format, e.g. "Friday" */ 0068 NarrowDayName /**< Narrow name format, e.g. "F". @since 4.7 */ 0069 }; 0070 0071 /** 0072 * @since 4.6 0073 * 0074 * Creates a KCalendarSystem object for the required Calendar System 0075 * 0076 * @param calendarSystem the Calendar System to create, defaults to QDate compatible 0077 * @param locale locale to use for translations. The global locale is used if null. 0078 * @return a KCalendarSystem object 0079 */ 0080 static KCalendarSystem *create(KLocale::CalendarSystem calendarSystem, 0081 const KLocale *locale); 0082 0083 /** 0084 * @since 4.6 0085 * 0086 * Creates a KCalendarSystem object for the required Calendar System 0087 * 0088 * @param calendarSystem the Calendar System to create 0089 * @param config a configuration file with a 'KCalendarSystem %calendarType' group detailing 0090 * locale-related preferences (such as era options). The global config is used 0091 if null. 0092 * @param locale locale to use for translations. The global locale is used if null. 0093 * @return a KCalendarSystem object 0094 */ 0095 static KCalendarSystem *create(KLocale::CalendarSystem calendarSystem = KLocale::QDateCalendar, 0096 KSharedConfig::Ptr config = KSharedConfig::Ptr(), 0097 const KLocale *locale = nullptr); 0098 0099 /** 0100 * @since 4.6 0101 * 0102 * Returns the list of currently supported Calendar Systems 0103 * 0104 * @return list of Calendar Systems 0105 */ 0106 static QList<KLocale::CalendarSystem> calendarSystemsList(); 0107 0108 /** 0109 * @since 4.6 0110 * 0111 * Returns a localized label to display for the required Calendar System type. 0112 * 0113 * Use with calendarSystemsList() to populate selection lists of available 0114 * calendar systems. 0115 * 0116 * @param calendarSystem the specific calendar type to return the label for 0117 * @param locale the locale to use for the label, defaults to global 0118 * @return label for calendar 0119 */ 0120 static QString calendarLabel(KLocale::CalendarSystem calendarSystem, const KLocale *locale = KLocale::global()); 0121 0122 /** 0123 * Destructor. 0124 */ 0125 virtual ~KCalendarSystem(); 0126 0127 /** 0128 * @deprecated use calendarSystem() instead 0129 * 0130 * Returns the calendar system type. 0131 * 0132 * @return type of calendar system 0133 */ 0134 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED 0135 KDELIBS4SUPPORT_DEPRECATED virtual QString calendarType() const = 0; 0136 #endif 0137 0138 /** 0139 * @since 4.6 0140 * 0141 * Returns the Calendar System type of the KCalendarSystem object 0142 * 0143 * @return type of calendar system 0144 */ 0145 virtual KLocale::CalendarSystem calendarSystem() const = 0; 0146 0147 /** 0148 * @since 4.6 0149 * 0150 * Returns a localized label to display for the current Calendar System type. 0151 * 0152 * @return localized label for this Calendar System 0153 */ 0154 QString calendarLabel() const; 0155 0156 /** 0157 * Returns a QDate holding the epoch of the calendar system. Usually YMD 0158 * of 1/1/1, access the returned QDates method toJulianDay() if you 0159 * require the actual Julian day number. Note: a particular calendar 0160 * system implementation may not include the epoch in its supported range, 0161 * or the calendar system may be proleptic in which case it supports dates 0162 * before the epoch. 0163 * 0164 * @see KCalendarSystem::earliestValidDate 0165 * @see KCalendarSystem::latestValidDate 0166 * @see KCalendarSystem::isProleptic 0167 * @see KCalendarSystem::isValid 0168 * 0169 * @return epoch of calendar system 0170 */ 0171 virtual QDate epoch() const = 0; 0172 0173 /** 0174 * Returns the earliest date valid in this calendar system implementation. 0175 * 0176 * If the calendar system is proleptic then this may be before epoch. 0177 * 0178 * @see KCalendarSystem::epoch 0179 * @see KCalendarSystem::latestValidDate 0180 * 0181 * @return date the earliest valid date 0182 */ 0183 virtual QDate earliestValidDate() const = 0; 0184 0185 /** 0186 * Returns the latest date valid in this calendar system implementation. 0187 * 0188 * @see KCalendarSystem::epoch 0189 * @see KCalendarSystem::earliestValidDate 0190 * 0191 * @return date the latest valid date 0192 */ 0193 virtual QDate latestValidDate() const = 0; 0194 0195 /** 0196 * Returns whether a given date is valid in this calendar system. 0197 * 0198 * @param year the year portion of the date to check 0199 * @param month the month portion of the date to check 0200 * @param day the day portion of the date to check 0201 * @return @c true if the date is valid, @c false otherwise 0202 */ 0203 bool isValid(int year, int month, int day) const; 0204 0205 /** 0206 * @since 4.4 0207 * 0208 * Returns whether a given date is valid in this calendar system. 0209 * 0210 * @param year the year portion of the date to check 0211 * @param dayOfYear the day of year portion of the date to check 0212 * @return @c true if the date is valid, @c false otherwise 0213 */ 0214 bool isValid(int year, int dayOfYear) const; 0215 0216 /** 0217 * @since 4.5 0218 * 0219 * Returns whether a given date is valid in this calendar system. 0220 * 0221 * @param eraName the Era Name portion of the date to check 0222 * @param yearInEra the Year In Era portion of the date to check 0223 * @param month the Month portion of the date to check 0224 * @param day the Day portion of the date to check 0225 * @return @c true if the date is valid, @c false otherwise 0226 */ 0227 bool isValid(const QString &eraName, int yearInEra, int month, int day) const; 0228 0229 /** 0230 * @since 4.4 0231 * 0232 * Returns whether a given date is valid in this calendar system. 0233 * 0234 * @param year the year portion of the date to check 0235 * @param isoWeekNumber the ISO week portion of the date to check 0236 * @param dayOfIsoWeek the day of week portion of the date to check 0237 * @return @c true if the date is valid, @c false otherwise 0238 */ 0239 bool isValidIsoWeekDate(int year, int isoWeekNumber, int dayOfIsoWeek) const; 0240 0241 /** 0242 * Returns whether a given date is valid in this calendar system. 0243 * 0244 * @param date the date to check 0245 * @return @c true if the date is valid, @c false otherwise 0246 */ 0247 inline bool isValid(const QDate &date) const 0248 { 0249 return date.isValid() && date >= earliestValidDate() && date <= latestValidDate(); 0250 } 0251 0252 /** 0253 * Changes the date's year, month and day. The range of the year, month 0254 * and day depends on which calendar is being used. All years entered 0255 * are treated literally, i.e. no Y2K translation is applied to years 0256 * entered in the range 00 to 99. Replaces setYMD. 0257 * 0258 * @param date date to change 0259 * @param year year 0260 * @param month month number 0261 * @param day day of month 0262 * @return @c true if the date is valid, @c false otherwise 0263 */ 0264 virtual bool setDate(QDate &date, int year, int month, int day) const; 0265 0266 /** 0267 * @since 4.4 0268 * 0269 * Set a date using the year number and day of year number only. 0270 * 0271 * @param date date to change 0272 * @param year year 0273 * @param dayOfYear day of year 0274 * @return @c true if the date is valid, @c false otherwise 0275 */ 0276 bool setDate(QDate &date, int year, int dayOfYear) const; 0277 0278 /** 0279 * @since 4.5 0280 * 0281 * Set a date using the era, year in era number, month and day 0282 * 0283 * @param date date to change 0284 * @param eraName Era string 0285 * @param yearInEra Year In Era number 0286 * @param month Month number 0287 * @param day Day Of Month number 0288 * @return @c true if the date is valid, @c false otherwise 0289 */ 0290 bool setDate(QDate &date, QString eraName, int yearInEra, int month, int day) const; 0291 0292 /** 0293 * @since 4.4 0294 * 0295 * Set a date using the year number, ISO week number and day of week number. 0296 * 0297 * @param date date to change 0298 * @param year year 0299 * @param isoWeekNumber ISO week of year 0300 * @param dayOfIsoWeek day of week Mon..Sun (1..7) 0301 * @return @c true if the date is valid, @c false otherwise 0302 */ 0303 bool setDateIsoWeek(QDate &date, int year, int isoWeekNumber, int dayOfIsoWeek) const; 0304 0305 /** 0306 * @deprecated Use setDate() instead 0307 * 0308 * Some implementations reject year range 00 to 99, but extended date 0309 * ranges now require these to be accepted. Equivalent in QDate is 0310 * obsoleted. 0311 * 0312 * Changes the date's year, month and day. The range of the year, month 0313 * and day depends on which calendar is being used. 0314 * 0315 * @param date Date to change 0316 * @param y Year 0317 * @param m Month number 0318 * @param d Day of month 0319 * @return true if the date is valid; otherwise returns false. 0320 */ 0321 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED 0322 KDELIBS4SUPPORT_DEPRECATED virtual bool setYMD(QDate &date, int y, int m, int d) const 0323 { 0324 return setDate(date, y, m, d); 0325 } 0326 #endif 0327 0328 /** 0329 * @since 4.5 0330 * 0331 * Returns the year, month and day portion of a given date in the current calendar system 0332 * 0333 * @param date date to get year, month and day for 0334 * @param year year number returned in this variable 0335 * @param month month number returned in this variable 0336 * @param day day of month returned in this variable 0337 */ 0338 void getDate(const QDate date, int *year, int *month, int *day) const; 0339 0340 /** 0341 * Returns the year portion of a given date in the current calendar system 0342 * 0343 * @param date date to return year for 0344 * @return year, 0 if input date is invalid 0345 */ 0346 virtual int year(const QDate &date) const; 0347 0348 /** 0349 * Returns the month portion of a given date in the current calendar system 0350 * 0351 * @param date date to return month for 0352 * @return month of year, 0 if input date is invalid 0353 */ 0354 virtual int month(const QDate &date) const; 0355 0356 /** 0357 * Returns the day portion of a given date in the current calendar system 0358 * 0359 * @param date date to return day for 0360 * @return day of the month, 0 if input date is invalid 0361 */ 0362 virtual int day(const QDate &date) const; 0363 0364 /** 0365 * @since 4.5 0366 * 0367 * Returns the Era Name portion of a given date in the current calendar system, 0368 * for example "AD" or "Anno Domini" for the Gregorian calendar and Christian Era. 0369 * 0370 * @param date date to return Era Name for 0371 * @param format format to return, either short or long 0372 * @return era name, empty string if input date is invalid 0373 */ 0374 QString eraName(const QDate &date, StringFormat format = ShortFormat) const; 0375 0376 /** 0377 * @since 4.5 0378 * 0379 * Returns the Era Year portion of a given date in the current 0380 * calendar system, for example "2000 AD" or "Heisei 22". 0381 * 0382 * @param date date to return Era Year for 0383 * @param format format to return, either short or long 0384 * @return era name, empty string if input date is invalid 0385 */ 0386 QString eraYear(const QDate &date, StringFormat format = ShortFormat) const; 0387 0388 /** 0389 * @since 4.5 0390 * 0391 * Returns the Year In Era portion of a given date in the current calendar 0392 * system, for example 1 for "1 BC". 0393 * 0394 * @param date date to return Year In Era for 0395 * @return Year In Era, -1 if input date is invalid 0396 */ 0397 int yearInEra(const QDate &date) const; 0398 0399 /** 0400 * Returns a QDate containing a date @p nyears years later. 0401 * 0402 * @param date The old date 0403 * @param nyears The number of years to add 0404 * @return The new date, null date if any errors 0405 */ 0406 virtual QDate addYears(const QDate &date, int nyears) const; 0407 0408 /** 0409 * Returns a QDate containing a date @p nmonths months later. 0410 * 0411 * @param date The old date 0412 * @param nmonths number of months to add 0413 * @return The new date, null date if any errors 0414 */ 0415 virtual QDate addMonths(const QDate &date, int nmonths) const; 0416 0417 /** 0418 * Returns a QDate containing a date @p ndays days later. 0419 * 0420 * @param date The old date 0421 * @param ndays number of days to add 0422 * @return The new date, null date if any errors 0423 */ 0424 inline QDate addDays(const QDate &date, int ndays) const 0425 { 0426 QDate d = date.addDays(ndays); 0427 return isValid(d) ? d : QDate(); 0428 } 0429 0430 /** 0431 * Returns the difference between two dates in years, months and days. 0432 * The difference is always caculated from the earlier date to the later 0433 * date in year, month and day order, with the @p direction parameter 0434 * indicating which direction the difference is applied from the @p toDate. 0435 * 0436 * For example, the difference between 2010-06-10 and 2012-09-5 is 2 years, 0437 * 2 months and 26 days. Note that the difference between two last days of 0438 * the month is always 1 month, e.g. 2010-01-31 to 2010-02-28 is 1 month 0439 * not 28 days. 0440 * 0441 * @param fromDate The date to start from 0442 * @param toDate The date to end at 0443 * @param yearsDiff Returns number of years difference 0444 * @param monthsDiff Returns number of months difference 0445 * @param daysDiff Returns number of days difference 0446 * @param direction Returns direction of difference, 1 if fromDate <= toDate, -1 otherwise 0447 */ 0448 void dateDifference(const QDate &fromDate, const QDate &toDate, 0449 int *yearsDiff, int *monthsDiff, int *daysDiff, int *direction) const; 0450 0451 /** 0452 * Returns the difference between two dates in completed calendar years. 0453 * The returned value will be negative if @p fromDate > @p toDate. 0454 * 0455 * For example, the difference between 2010-06-10 and 2012-09-5 is 2 years. 0456 * 0457 * @param fromDate The date to start from 0458 * @param toDate The date to end at 0459 * @return The number of years difference 0460 */ 0461 int yearsDifference(const QDate &fromDate, const QDate &toDate) const; 0462 0463 /** 0464 * Returns the difference between two dates in completed calendar months 0465 * The returned value will be negative if @p fromDate > @p toDate. 0466 * 0467 * For example, the difference between 2010-06-10 and 2012-09-5 is 26 months. 0468 * Note that the difference between two last days of the month is always 1 0469 * month, e.g. 2010-01-31 to 2010-02-28 is 1 month not 28 days. 0470 * 0471 * @param fromDate The date to start from 0472 * @param toDate The date to end at 0473 * @return The number of months difference 0474 */ 0475 int monthsDifference(const QDate &fromDate, const QDate &toDate) const; 0476 0477 /** 0478 * Returns the difference between two dates in days 0479 * The returned value will be negative if @p fromDate > @p toDate. 0480 * 0481 * @param fromDate The date to start from 0482 * @param toDate The date to end at 0483 * @return The number of days difference 0484 */ 0485 inline int daysDifference(const QDate &fromDate, const QDate &toDate) const 0486 { 0487 return isValid(fromDate) && isValid(toDate) ? toDate.toJulianDay() - fromDate.toJulianDay() : 0; 0488 } 0489 0490 /** 0491 * Returns number of months in the given year 0492 * 0493 * @param date the date to obtain year from 0494 * @return number of months in the year, -1 if input date invalid 0495 */ 0496 int monthsInYear(const QDate &date) const; 0497 0498 /** 0499 * @since 4.5 0500 * 0501 * Returns number of months in the given year 0502 * 0503 * @param year the required year 0504 * @return number of months in the year, -1 if input date invalid 0505 */ 0506 int monthsInYear(int year) const; 0507 0508 /** 0509 * @since 4.7 0510 * 0511 * Returns the number of Weeks in a year using the specified Week Number System. 0512 * 0513 * @see week() 0514 * @see formatDate() 0515 * @param date the date to obtain year from 0516 * @param weekNumberSystem the week number system to use 0517 * @return number of weeks in the year, -1 if date invalid 0518 */ 0519 inline int weeksInYear(const QDate &date, KLocale::WeekNumberSystem weekNumberSystem = KLocale::DefaultWeekNumber) const 0520 { 0521 return isValid(date) ? weeksInYear(year(date), weekNumberSystem) : -1; 0522 } 0523 0524 /** 0525 * @since 4.7 0526 * 0527 * Returns the number of Weeks in a year using the specified Week Number System. 0528 * 0529 * @see week() 0530 * @see formatDate() 0531 * @param year the year 0532 * @param weekNumberSystem the week number system to use 0533 * @return number of weeks in the year, -1 if date invalid 0534 */ 0535 int weeksInYear(int year, KLocale::WeekNumberSystem weekNumberSystem = KLocale::DefaultWeekNumber) const; 0536 0537 /** 0538 * Returns the number of days in the given year. 0539 * 0540 * @param date the date to obtain year from 0541 * @return number of days in year, -1 if input date invalid 0542 */ 0543 int daysInYear(const QDate &date) const; 0544 0545 /** 0546 * @since 4.5 0547 * 0548 * Returns the number of days in the given year. 0549 * 0550 * @param year the year 0551 * @return number of days in year, -1 if input date invalid 0552 */ 0553 int daysInYear(int year) const; 0554 0555 /** 0556 * Returns the number of days in the given month. 0557 * 0558 * @param date the date to obtain month from 0559 * @return number of days in month, -1 if input date invalid 0560 */ 0561 int daysInMonth(const QDate &date) const; 0562 0563 /** 0564 * @since 4.5 0565 * 0566 * Returns the number of days in the given month. 0567 * 0568 * @param year the year the month is in 0569 * @param month the month 0570 * @return number of days in month, -1 if input date invalid 0571 */ 0572 int daysInMonth(int year, int month) const; 0573 0574 /** 0575 * Returns the number of days in the given week. 0576 * 0577 * @note Every calendar systems ever supported by @c KCalendarSystem 0578 * returns, and has always returned, @c 7, but there is no guarantee that 0579 * all future calendar systems will also do so. 0580 * @param date the date to obtain week from 0581 * @return number of days in week, -1 if input date invalid 0582 */ 0583 int daysInWeek(const QDate &date) const; 0584 0585 /** 0586 * Returns the day number of year for the given date 0587 * 0588 * The days are numbered 1..daysInYear() 0589 * 0590 * @param date the date to obtain day from 0591 * @return day of year number, -1 if input date not valid 0592 */ 0593 virtual int dayOfYear(const QDate &date) const; 0594 0595 /** 0596 * Returns the weekday number for the given date 0597 * 0598 * The weekdays are numbered 1..7 for Monday..Sunday. 0599 * 0600 * This value is @em not affected by the value of weekStartDay() 0601 * 0602 * @param date the date to obtain day from 0603 * @return day of week number, -1 if input date not valid 0604 */ 0605 int dayOfWeek(const QDate &date) const; 0606 0607 /** 0608 * @deprecated use week() instead 0609 * 0610 * Returns the ISO week number for the given date. 0611 * 0612 * ISO 8601 defines the first week of the year as the week containing the first Thursday. 0613 * See http://en.wikipedia.org/wiki/ISO_8601 and http://en.wikipedia.org/wiki/ISO_week_date 0614 * 0615 * If the date falls in the last week of the previous year or the first week of the following 0616 * year, then the yearNum returned will be set to the appropriate year. 0617 * 0618 * @param date the date to obtain week from 0619 * @param yearNum returns the year the date belongs to 0620 * @return ISO week number, -1 if input date invalid 0621 */ 0622 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED 0623 KDELIBS4SUPPORT_DEPRECATED inline int weekNumber(const QDate &date, int *yearNum = nullptr) const 0624 { 0625 return week(date, KLocale::IsoWeekNumber, yearNum); 0626 } 0627 #endif 0628 0629 /** 0630 * Returns the localized Week Number for the date. 0631 * 0632 * This may be ISO, US, or any other supported week numbering scheme. If 0633 * you specifically require the ISO Week or any other scheme, you should use 0634 * the week(KLocale::WeekNumberSystem) form. 0635 * 0636 * If the date falls in the last week of the previous year or the first 0637 * week of the following year, then the yearNum returned will be set to the 0638 * appropriate year. 0639 * 0640 * @see weeksInYear() 0641 * @see formatDate() 0642 * @param date the date to obtain week from 0643 * @param yearNum returns the year the date belongs to 0644 * @return localized week number, -1 if input date invalid 0645 */ 0646 inline int week(const QDate &date, int *yearNum) const 0647 { 0648 return week(date, KLocale::DefaultWeekNumber, yearNum); 0649 } 0650 0651 /** 0652 * Returns the Week Number for the date in the required Week Number System. 0653 * 0654 * Unless you want a specific Week Number System (e.g. ISO Week), you should 0655 * use the localized Week Number form of week(). 0656 * 0657 * If the date falls in the last week of the previous year or the first 0658 * week of the following year, then the yearNum returned will be set to the 0659 * appropriate year. 0660 * 0661 * Technically, the ISO Week Number only applies to the ISO/Gregorian Calendar 0662 * System, but the same rules will be applied to the current Calendar System. 0663 * 0664 * @see weeksInYear() 0665 * @see formatDate() 0666 * @param date the date to obtain week from 0667 * @param weekNumberSystem the Week Number System to use 0668 * @param yearNum returns the year the date belongs to 0669 * @return week number, -1 if input date invalid 0670 */ 0671 int week(const QDate &date, KLocale::WeekNumberSystem weekNumberSystem = KLocale::DefaultWeekNumber, int *yearNum = nullptr) const; 0672 0673 /** 0674 * Returns whether a given year is a leap year. 0675 * 0676 * Input year must be checked for validity in current Calendar System prior to calling, no 0677 * validity checking performed in this routine, behaviour is undefined in invalid case. 0678 * 0679 * @param year the year to check 0680 * @return @c true if the year is a leap year, @c false otherwise 0681 */ 0682 bool isLeapYear(int year) const; 0683 0684 /** 0685 * Returns whether a given date falls in a leap year. 0686 * 0687 * Input date must be checked for validity in current Calendar System prior to calling, no 0688 * validity checking performed in this routine, behaviour is undefined in invalid case. 0689 * 0690 * @param date the date to check 0691 * @return @c true if the date falls in a leap year, @c false otherwise 0692 */ 0693 bool isLeapYear(const QDate &date) const; 0694 0695 /** 0696 * @since 4.6 0697 * 0698 * Returns a QDate containing the first day of the year 0699 * 0700 * @param year The year to return the date for 0701 * @return The first day of the year 0702 */ 0703 QDate firstDayOfYear(int year) const; 0704 0705 /** 0706 * @since 4.6 0707 * 0708 * Returns a QDate containing the last day of the year 0709 * 0710 * @param year The year to return the date for 0711 * @return The last day of the year 0712 */ 0713 QDate lastDayOfYear(int year) const; 0714 0715 /** 0716 * @since 4.6 0717 * 0718 * Returns a QDate containing the first day of the year 0719 * 0720 * @param date The year to return the date for, defaults to today 0721 * @return The first day of the year 0722 */ 0723 QDate firstDayOfYear(const QDate &date = QDate::currentDate()) const; 0724 0725 /** 0726 * @since 4.6 0727 * 0728 * Returns a QDate containing the last day of the year 0729 * 0730 * @param date The year to return the date for, defaults to today 0731 * @return The last day of the year 0732 */ 0733 QDate lastDayOfYear(const QDate &date = QDate::currentDate()) const; 0734 0735 /** 0736 * @since 4.6 0737 * 0738 * Returns a QDate containing the first day of the month 0739 * 0740 * @param year The year to return the date for 0741 * @param month The month to return the date for 0742 * @return The first day of the month 0743 */ 0744 QDate firstDayOfMonth(int year, int month) const; 0745 0746 /** 0747 * @since 4.6 0748 * 0749 * Returns a QDate containing the last day of the month 0750 * 0751 * @param year The year to return the date for 0752 * @param month The month to return the date for 0753 * @return The last day of the month 0754 */ 0755 QDate lastDayOfMonth(int year, int month) const; 0756 0757 /** 0758 * @since 4.6 0759 * 0760 * Returns a QDate containing the first day of the month 0761 * 0762 * @param date The month to return the date for, defaults to today 0763 * @return The first day of the month 0764 */ 0765 QDate firstDayOfMonth(const QDate &date = QDate::currentDate()) const; 0766 0767 /** 0768 * @since 4.6 0769 * 0770 * Returns a QDate containing the last day of the month 0771 * 0772 * @param date The month to return the date for, defaults to today 0773 * @return The last day of the month 0774 */ 0775 QDate lastDayOfMonth(const QDate &date = QDate::currentDate()) const; 0776 0777 /** 0778 * Gets specific calendar type month name for a given month number 0779 * If an invalid month is specified, QString() is returned. 0780 * 0781 * @param month the month number 0782 * @param year the year the month belongs to 0783 * @param format specifies whether the short month name or long month name should be used 0784 * @return name of the month, empty string if any error 0785 */ 0786 virtual QString monthName(int month, int year, MonthNameFormat format = LongName) const = 0; 0787 0788 /** 0789 * Gets specific calendar type month name for a given date 0790 * 0791 * @param date date to obtain month from 0792 * @param format specifies whether the short month name or long month name should be used 0793 * @return name of the month, empty string if any error 0794 */ 0795 virtual QString monthName(const QDate &date, MonthNameFormat format = LongName) const; 0796 0797 /** 0798 * Gets specific calendar type week day name. 0799 * If an invalid week day is specified, QString() is returned. 0800 * 0801 * @param weekDay number of day in week (Monday = 1, ..., Sunday = 7) 0802 * @param format specifies whether the short month name or long month name should be used 0803 * @return day name, empty string if any error 0804 */ 0805 virtual QString weekDayName(int weekDay, WeekDayNameFormat format = LongDayName) const = 0; 0806 0807 /** 0808 * Gets specific calendar type week day name. 0809 * 0810 * @param date the date 0811 * @param format specifies whether the short month name or long month name should be used 0812 * @return day name, empty string if any error 0813 */ 0814 virtual QString weekDayName(const QDate &date, WeekDayNameFormat format = LongDayName) const; 0815 0816 /** 0817 * @deprecated use formatDate(QDate, KLocale::DateTimeComponent, KLocale::DateTimeComponentFormat) 0818 * 0819 * Converts a date into a year literal 0820 * 0821 * @param date date to convert 0822 * @param format format to return, either short or long 0823 * @return year literal of the date, empty string if any error 0824 * @see year() 0825 */ 0826 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED 0827 KDELIBS4SUPPORT_DEPRECATED virtual QString yearString(const QDate &date, StringFormat format = LongFormat) const 0828 { 0829 if (format == ShortFormat) { 0830 return formatDate(date, KLocale::Year, KLocale::ShortNumber); 0831 } else { 0832 return formatDate(date, KLocale::Year, KLocale::LongNumber); 0833 } 0834 } 0835 #endif 0836 0837 /** 0838 * @deprecated use formatDate(QDate, KLocale::DateTimeComponent, KLocale::DateTimeComponentFormat) 0839 * 0840 * Converts a date into a month literal 0841 * 0842 * @param pDate The date to convert 0843 * @param format The format to return, either short or long 0844 * @return The month literal of the date, empty string if any error 0845 * @see month() 0846 */ 0847 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED 0848 KDELIBS4SUPPORT_DEPRECATED virtual QString monthString(const QDate &date, StringFormat format = LongFormat) const 0849 { 0850 if (format == ShortFormat) { 0851 return formatDate(date, KLocale::Month, KLocale::ShortNumber); 0852 } else { 0853 return formatDate(date, KLocale::Month, KLocale::LongNumber); 0854 } 0855 } 0856 #endif 0857 0858 /** 0859 * @deprecated use formatDate(QDate, KLocale::DateTimeComponent, KLocale::DateTimeComponentFormat) 0860 * 0861 * Converts a date into a day literal 0862 * 0863 * @param pDate The date to convert 0864 * @param format The format to return, either short or long 0865 * @return The day literal of the date, empty string if any error 0866 * @see day() 0867 */ 0868 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED 0869 KDELIBS4SUPPORT_DEPRECATED virtual QString dayString(const QDate &date, StringFormat format = LongFormat) const 0870 { 0871 if (format == ShortFormat) { 0872 return formatDate(date, KLocale::Day, KLocale::ShortNumber); 0873 } else { 0874 return formatDate(date, KLocale::Day, KLocale::LongNumber); 0875 } 0876 } 0877 #endif 0878 0879 /** 0880 * @deprecated use formatDate(QDate, KLocale::DateTimeComponent, KLocale::DateTimeComponentFormat) 0881 * 0882 * @since 4.5 0883 * 0884 * Converts a date into a Year In Era literal 0885 * 0886 * @param date date to return Year In Era for 0887 * @param format format to return, either short or long 0888 * @return Year In Era literal of the date, empty string if any error 0889 */ 0890 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED 0891 KDELIBS4SUPPORT_DEPRECATED QString yearInEraString(const QDate &date, StringFormat format = ShortFormat) const 0892 { 0893 if (format == ShortFormat) { 0894 return formatDate(date, KLocale::YearInEra, KLocale::ShortNumber); 0895 } else { 0896 return formatDate(date, KLocale::YearInEra, KLocale::LongNumber); 0897 } 0898 } 0899 #endif 0900 0901 /** 0902 * @deprecated use formatDate(QDate, KLocale::DateTimeComponent, KLocale::DateTimeComponentFormat) 0903 * 0904 * @since 4.4 0905 * 0906 * Converts a date into a day of year literal 0907 * 0908 * @param pDate The date to convert 0909 * @param format The format to return, either short or long 0910 * @return The day of year literal of the date, empty string if any error 0911 * @see dayOfYear() 0912 */ 0913 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED 0914 KDELIBS4SUPPORT_DEPRECATED QString dayOfYearString(const QDate &date, StringFormat format = LongFormat) const 0915 { 0916 if (format == ShortFormat) { 0917 return formatDate(date, KLocale::DayOfYear, KLocale::ShortNumber); 0918 } else { 0919 return formatDate(date, KLocale::DayOfYear, KLocale::LongNumber); 0920 } 0921 } 0922 #endif 0923 0924 /** 0925 * @deprecated use formatDate(QDate, KLocale::DateTimeComponent, KLocale::DateTimeComponentFormat) 0926 * 0927 * @since 4.4 0928 * 0929 * Converts a date into a day of week literal 0930 * 0931 * @param pDate The date to convert 0932 * @return The day of week literal of the date, empty string if any error 0933 * @see dayOfWeek() 0934 */ 0935 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED 0936 KDELIBS4SUPPORT_DEPRECATED QString dayOfWeekString(const QDate &date) const 0937 { 0938 return formatDate(date, KLocale::DayOfWeek, KLocale::ShortNumber); 0939 } 0940 #endif 0941 0942 /** 0943 * @deprecated use formatDate(QDate, KLocale::DateTimeComponent, KLocale::DateTimeComponentFormat) 0944 * 0945 * @since 4.4 0946 * 0947 * Converts a date into a week number literal 0948 * 0949 * @param pDate The date to convert 0950 * @param format The format to return, either short or long 0951 * @return The day literal of the date, empty string if any error 0952 * @see weekNumber() 0953 */ 0954 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED 0955 KDELIBS4SUPPORT_DEPRECATED QString weekNumberString(const QDate &date, StringFormat format = LongFormat) const 0956 { 0957 if (format == ShortFormat) { 0958 return formatDate(date, KLocale::Week, KLocale::ShortNumber); 0959 } else { 0960 return formatDate(date, KLocale::Week, KLocale::LongNumber); 0961 } 0962 } 0963 #endif 0964 0965 /** 0966 * @deprecated use formatDate(QDate, KLocale::DateTimeComponent, KLocale::DateTimeComponentFormat) 0967 * 0968 * @since 4.4 0969 * 0970 * Returns the months in year for a date as a numeric string 0971 * 0972 * @param pDate The date to convert 0973 * @param format The format to return, either short or long 0974 * @return The months in year literal of the date, empty string if any error 0975 * @see monthsInYear() 0976 */ 0977 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED 0978 KDELIBS4SUPPORT_DEPRECATED QString monthsInYearString(const QDate &date, StringFormat format = LongFormat) const 0979 { 0980 if (format == ShortFormat) { 0981 return formatDate(date, KLocale::MonthsInYear, KLocale::ShortNumber); 0982 } else { 0983 return formatDate(date, KLocale::MonthsInYear, KLocale::LongNumber); 0984 } 0985 } 0986 #endif 0987 0988 /** 0989 * @deprecated use formatDate(QDate, KLocale::DateTimeComponent, KLocale::DateTimeComponentFormat) 0990 * 0991 * @since 4.4 0992 * 0993 * Returns the weeks in year for a date as a numeric string 0994 * 0995 * @param pDate The date to convert 0996 * @param format The format to return, either short or long 0997 * @return The weeks in year literal of the date, empty string if any error 0998 * @see weeksInYear() 0999 */ 1000 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED 1001 KDELIBS4SUPPORT_DEPRECATED QString weeksInYearString(const QDate &date, StringFormat format = LongFormat) const 1002 { 1003 if (format == ShortFormat) { 1004 return formatDate(date, KLocale::WeeksInYear, KLocale::ShortNumber); 1005 } else { 1006 return formatDate(date, KLocale::WeeksInYear, KLocale::LongNumber); 1007 } 1008 } 1009 #endif 1010 1011 /** 1012 * @deprecated use formatDate(QDate, KLocale::DateTimeComponent, KLocale::DateTimeComponentFormat) 1013 * 1014 * @since 4.4 1015 * 1016 * Returns the days in year for a date as a numeric string 1017 * 1018 * @param pDate The date to convert 1019 * @param format The format to return, either short or long 1020 * @return The days in year literal of the date, empty string if any error 1021 * @see daysInYear() 1022 */ 1023 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED 1024 KDELIBS4SUPPORT_DEPRECATED QString daysInYearString(const QDate &date, StringFormat format = LongFormat) const 1025 { 1026 if (format == ShortFormat) { 1027 return formatDate(date, KLocale::DaysInYear, KLocale::ShortNumber); 1028 } else { 1029 return formatDate(date, KLocale::DaysInYear, KLocale::LongNumber); 1030 } 1031 } 1032 #endif 1033 1034 /** 1035 * @deprecated use formatDate(QDate, KLocale::DateTimeComponent, KLocale::DateTimeComponentFormat) 1036 * 1037 * @since 4.4 1038 * 1039 * Returns the days in month for a date as a numeric string 1040 * 1041 * @param pDate The date to convert 1042 * @param format The format to return, either short or long 1043 * @return The days in month literal of the date, empty string if any error 1044 * @see daysInMonth() 1045 */ 1046 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED 1047 KDELIBS4SUPPORT_DEPRECATED QString daysInMonthString(const QDate &date, StringFormat format = LongFormat) const 1048 { 1049 if (format == ShortFormat) { 1050 return formatDate(date, KLocale::DaysInMonth, KLocale::ShortNumber); 1051 } else { 1052 return formatDate(date, KLocale::DaysInMonth, KLocale::LongNumber); 1053 } 1054 } 1055 #endif 1056 1057 /** 1058 * @deprecated use formatDate(QDate, KLocale::DateTimeComponent, KLocale::DateTimeComponentFormat) 1059 * 1060 * @since 4.4 1061 * 1062 * Returns the days in week for a date as a numeric string 1063 * 1064 * @param date The date to convert 1065 * @return The days in week literal of the date, empty string if any error 1066 * @see daysInWeek() 1067 */ 1068 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED 1069 KDELIBS4SUPPORT_DEPRECATED QString daysInWeekString(const QDate &date) const 1070 { 1071 return formatDate(date, KLocale::DaysInWeek, KLocale::ShortNumber); 1072 } 1073 #endif 1074 1075 /** 1076 * @deprecated for internal use only 1077 * 1078 * Converts a year literal of a part of a string into a integer starting at the beginning of the string 1079 * 1080 * @param sNum The string to parse 1081 * @param iLength The number of QChars used, and 0 if no valid symbols was found in the string 1082 * @return An integer corresponding to the year 1083 */ 1084 virtual int yearStringToInteger(const QString &sNum, int &iLength) const; 1085 1086 /** 1087 * @deprecated for internal use only 1088 * 1089 * Converts a month literal of a part of a string into a integer starting at the beginning of the string 1090 * 1091 * @param sNum The string to parse 1092 * @param iLength The number of QChars used, and 0 if no valid symbols was found in the string 1093 * @return An integer corresponding to the month 1094 */ 1095 virtual int monthStringToInteger(const QString &sNum, int &iLength) const; 1096 1097 /** 1098 * @deprecated for internal use only 1099 * 1100 * Converts a day literal of a part of a string into a integer starting at the beginning of the string 1101 * 1102 * @param sNum The string to parse 1103 * @param iLength The number of QChars used, and 0 if no valid symbols was found in the string 1104 * @return An integer corresponding to the day 1105 */ 1106 virtual int dayStringToInteger(const QString &sNum, int &iLength) const; 1107 1108 /** 1109 * Returns a string formatted to the current locale's conventions 1110 * regarding dates. 1111 * 1112 * Uses the calendar system's internal locale set when the instance was 1113 * created, which ensures that the correct calendar system and locale 1114 * settings are respected, which would not occur in some cases if using 1115 * the global locale. Defaults to global locale. 1116 * 1117 * @see KLocale::formatDate 1118 * 1119 * @param fromDate the date to be formatted 1120 * @param toFormat category of date format to use 1121 * 1122 * @return The date as a string 1123 */ 1124 virtual QString formatDate(const QDate &fromDate, KLocale::DateFormat toFormat = KLocale::LongDate) const; 1125 1126 /** 1127 * @since 4.4 1128 * 1129 * Returns a string formatted to the given format and localised to the 1130 * correct language and digit set using the requested format standard. 1131 * 1132 * *** WITH GREAT POWER COMES GREAT RESPONSIBILITY *** 1133 * Please use with care and only in situations where the DateFormat enum 1134 * or locale formats or individual string methods do not provide what you 1135 * need. You should almost always translate your format string as 1136 * documented. Using the standard DateFormat options instead would take 1137 * care of the translation for you. 1138 * 1139 * Warning: The %n element differs from the GNU/POSIX standard where it is 1140 * defined as a newline. KDE currently uses this for short day number. It 1141 * is recommended for compatibility purposes to use %-m instead. 1142 * 1143 * The toFormat parameter is a good candidate to be made translatable, 1144 * so that translators can adapt it to their language's convention. 1145 * There should also be a context using the "kdedt-format" keyword (for 1146 * automatic validation of translations) and stating the format's purpose: 1147 * \code 1148 * QDate reportDate; 1149 * KLocale::global()->calendar()->setDate(reportDate, reportYear, reportMonth, 1); 1150 * dateFormat = i18nc("(kdedt-format) Report month and year in report header", "%B %Y")); 1151 * dateString = KLocale::global()->calendar()->formatDate(reportDate, dateFormat); 1152 * \endcode 1153 * 1154 * The date format string can be defined using either the KDE or POSIX standards. 1155 * The KDE standard closely follows the POSIX standard but with some exceptions. 1156 * Always use the KDE standard within KDE, but where interaction is required with 1157 * external POSIX compliant systems (e.g. Gnome, glibc, etc) the POSIX standard 1158 * should be used. 1159 * 1160 * Date format strings are made up of date componants and string literals. 1161 * Date componants are prefixed by a % escape character and are made up of 1162 * optional padding and case modifier flags, an optional width value, and a 1163 * compulsary code for the actual date componant: 1164 * %[Flags][Width][Componant] 1165 * e.g. %_^5Y 1166 * No spaces are allowed. 1167 * 1168 * The Flags can modify the padding character and/or case of the Date Componant. 1169 * The Flags are optional and may be combined and/or repeated in any order, 1170 * in which case the last Padding Flag and last Case Flag will be the 1171 * ones used. The Flags must be immediately after the % and before any Width. 1172 * 1173 * The Width can modify how wide the date Componant is padded to. The Width 1174 * is an optional interger value and must be after any Flags but before the 1175 * Componant. If the Width is less than the minimum defined for a Componant 1176 * then the default minimum will be used instead. 1177 * 1178 * By default most numeric Date Componants are right-aligned with leading 0's. 1179 * 1180 * By default all string name fields are capital case and unpadded. 1181 * 1182 * The following Flags may be specified: 1183 * @li - (hyphen) no padding (e.g. 1 Jan and "%-j" = "1") 1184 * @li _ (underscore) pad with spaces (e.g. 1 Jan and "%-j" = " 1") 1185 * @li 0 (zero) pad with 0's (e.g. 1 Jan and "%0j" = "001") 1186 * @li ^ (caret) make uppercase (e.g. 1 Jan and "%^B" = "JANUARY") 1187 * @li # (hash) invert case (e.g. 1 Jan and "%#B" = "???") 1188 * 1189 * The following Date Componants can be specified: 1190 * @li %Y the year to 4 digits (e.g. "1984" for 1984, "0584" for 584, "0084" for 84) 1191 * @li %C the 'century' portion of the year to 2 digits (e.g. "19" for 1984, "05" for 584, "00" for 84) 1192 * @li %y the lower 2 digits of the year to 2 digits (e.g. "84" for 1984, "05" for 2005) 1193 * @li %EY the full local era year (e.g. "2000 AD") 1194 * @li %EC the era name short form (e.g. "AD") 1195 * @li %Ey the year in era to 1 digit (e.g. 1 or 2000) 1196 * @li %m the month number to 2 digits (January="01", December="12") 1197 * @li %n the month number to 1 digit (January="1", December="12"), see notes! 1198 * @li %d the day number of the month to 2 digits (e.g. "01" on the first of March) 1199 * @li %e the day number of the month to 1 digit (e.g. "1" on the first of March) 1200 * @li %B the month name long form (e.g. "January") 1201 * @li %b the month name short form (e.g. "Jan" for January) 1202 * @li %h the month name short form (e.g. "Jan" for January) 1203 * @li %A the weekday name long form (e.g. "Wednesday" for Wednesday) 1204 * @li %a the weekday name short form (e.g. "Wed" for Wednesday) 1205 * @li %j the day of the year number to 3 digits (e.g. "001" for 1 Jan) 1206 * @li %V the ISO week of the year number to 2 digits (e.g. "01" for ISO Week 1) 1207 * @li %G the year number in long form of the ISO week of the year to 4 digits (e.g. "2004" for 1 Jan 2005) 1208 * @li %g the year number in short form of the ISO week of the year to 2 digits (e.g. "04" for 1 Jan 2005) 1209 * @li %u the day of the week number to 1 digit (e.g. "1" for Monday) 1210 * @li %D the US short date format (e.g. "%m/%d/%y") 1211 * @li %F the ISO short date format (e.g. "%Y-%m-%d") 1212 * @li %x the KDE locale short date format 1213 * @li %% the literal "%" 1214 * @li %t a tab character 1215 * 1216 * Everything else in the format string will be taken as literal text. 1217 * 1218 * Examples: 1219 * "%Y-%m-%d" = "2009-01-01" 1220 * "%Y-%-m-%_4d" = "2009-1- 1" 1221 * 1222 * The following format codes behave differently in the KDE and POSIX standards 1223 * @li %e in GNU/POSIX is space padded to 2 digits, in KDE is not padded 1224 * @li %n in GNU/POSIX is newline, in KDE is short month number 1225 * 1226 * The following POSIX format codes are currently not supported: 1227 * @li %U US week number 1228 * @li %w US day of week 1229 * @li %W US week number 1230 * @li %O locale's alternative numeric symbols, in KDE is not supported 1231 * 1232 * %0 is not supported as the returned result is always in the locale's chosen numeric symbol digit set. 1233 * 1234 * @see KLocale::formatDate 1235 * 1236 * @param fromDate the date to be formatted 1237 * @param toFormat the date format to use 1238 * @param formatStandard the standard the date format uses, defaults to KDE Standard 1239 * 1240 * @return The date as a string 1241 */ 1242 QString formatDate(const QDate &fromDate, const QString &toFormat, 1243 KLocale::DateTimeFormatStandard formatStandard = KLocale::KdeFormat) const; 1244 1245 /** 1246 * @since 4.4 1247 * 1248 * Returns a string formatted to the given format string and Digit Set. 1249 * Only use this version if you need control over the Digit Set and do 1250 * not want to use the locale Digit Set. 1251 * 1252 * @see formatDate 1253 * 1254 * @param fromDate the date to be formatted 1255 * @param toFormat the date format to use 1256 * @param digitSet the Digit Set to format the date in 1257 * @param formatStandard the standard the date format uses, defaults to KDE Standard 1258 * 1259 * @return The date as a string 1260 */ 1261 QString formatDate(const QDate &fromDate, const QString &toFormat, KLocale::DigitSet digitSet, 1262 KLocale::DateTimeFormatStandard formatStandard = KLocale::KdeFormat) const; 1263 1264 /** 1265 * @since 4.6 1266 * 1267 * Returns a Date Component as a localized string in the requested format. 1268 * 1269 * For example for 2010-01-01 the KLocale::Month with en_US Locale and Gregorian calendar may return: 1270 * KLocale::ShortNumber = "1" 1271 * KLocale::LongNumber = "01" 1272 * KLocale::NarrowName = "J" 1273 * KLocale::ShortName = "Jan" 1274 * KLocale::LongName = "January" 1275 * 1276 * @param date The date to format 1277 * @param component The date component to return 1278 * @param format The format to return the @p component in 1279 * @param weekNumberSystem To override the default Week Number System to use 1280 * @return The localized string form of the date component 1281 */ 1282 QString formatDate(const QDate &date, KLocale::DateTimeComponent component, 1283 KLocale::DateTimeComponentFormat format = KLocale::DefaultComponentFormat, 1284 KLocale::WeekNumberSystem weekNumberSystem = KLocale::DefaultWeekNumber) const; 1285 1286 /** 1287 * Converts a localized date string to a QDate. 1288 * The bool pointed by @p ok will be @c false if the date entered was invalid. 1289 * 1290 * Uses the calendar system's internal locale set when the instance was 1291 * created, which ensures that the correct calendar system and locale 1292 * settings are respected, which would not occur in some cases if using 1293 * the global locale. Defaults to global locale. 1294 * 1295 * @see KLocale::readDate 1296 * 1297 * @param str the string to convert 1298 * @param ok if non-null, will be set to @c true if the date is valid, @c false if invalid 1299 * 1300 * @return the string converted to a QDate 1301 */ 1302 virtual QDate readDate(const QString &str, bool *ok = nullptr) const; 1303 1304 /** 1305 * Converts a localized date string to a QDate. 1306 * This method is stricter than readDate(str,&ok): it will either accept 1307 * a date in full format or a date in short format, depending on @p flags. 1308 * 1309 * Uses the calendar system's internal locale set when the instance was 1310 * created, which ensures that the correct calendar system and locale 1311 * settings are respected, which would not occur in some cases if using 1312 * the global locale. Defaults to global locale. 1313 * 1314 * @see KLocale::readDate 1315 * 1316 * @param str the string to convert 1317 * @param flags whether the date string is to be in full format or in short format 1318 * @param ok if non-null, will be set to @c true if the date is valid, @c false if invalid 1319 * 1320 * @return the string converted to a QDate 1321 */ 1322 virtual QDate readDate(const QString &str, KLocale::ReadDateFlags flags, bool *ok = nullptr) const; 1323 1324 /** 1325 * Converts a localized date string to a QDate, using the specified @p format. 1326 * You will usually not want to use this method. Uses teh KDE format standard. 1327 * 1328 * @param dateString the string to convert 1329 * @param dateFormat the date format to use, in KDE format standard 1330 * @param ok if non-null, will be set to @c true if the date is valid, @c false if invalid 1331 * 1332 * @return the string converted to a QDate 1333 * 1334 * @see formatDate 1335 * @see KLocale::readDate 1336 */ 1337 virtual QDate readDate(const QString &dateString, const QString &dateFormat, bool *ok = nullptr) const; 1338 1339 /** 1340 * Converts a localized date string to a QDate, using the specified @p format. 1341 * You will usually not want to use this method. 1342 * 1343 * You must supply a format and string containing at least one of the following combinations to 1344 * create a valid date: 1345 * @li a month and day of month 1346 * @li a day of year 1347 * @li a ISO week number and day of week 1348 * 1349 * If a year number is not supplied then the current year will be assumed. 1350 * 1351 * All date componants must be separated by a non-numeric character. 1352 * 1353 * The format is not applied strictly to the input string: 1354 * @li extra whitespace is ignored 1355 * @li leading 0's on numbers are ignored 1356 * @li capitalisation of literals is ignored 1357 * 1358 * The allowed format componants are almost the same as the formatDate() function. 1359 * The following date componants will be read: 1360 * @li %Y the whole year (e.g. "1984" for 1984) 1361 * @li %y the lower 2 digits of the year (e.g. "84" for 1984) 1362 * @li %EY the full local era year (e.g. "2000 AD") 1363 * @li %EC the era name short form (e.g. "AD") 1364 * @li %Ey the year in era to 1 digit (e.g. 1 or 2000) 1365 * @li %m the month number to two digits (January="01", December="12") 1366 * @li %n the month number (January="1", December="12") 1367 * @li %d the day number of the month to two digits (e.g. "01" on the first of March) 1368 * @li %e the day number of the month (e.g. "1" on the first of March) 1369 * @li %B the month name long form (e.g. "January") 1370 * @li %b the month name short form (e.g. "Jan" for January) 1371 * @li %h the month name short form (e.g. "Jan" for January) 1372 * @li %A the weekday name long form (e.g. "Wednesday" for Wednesday) 1373 * @li %a the weekday name short form (e.g. "Wed" for Wednesday) 1374 * @li %j the day of the year number to three digits (e.g. "001" for 1 Jan) 1375 * @li %V the ISO week of the year number to two digits (e.g. "01" for ISO Week 1) 1376 * @li %u the day of the week number (e.g. "1" for Monday) 1377 * 1378 * The following date componants are NOT supported: 1379 * @li %C the 'century' portion of the year (e.g. "19" for 1984, "5" for 584, "" for 84) 1380 * @li %G the year number in long form of the ISO week of the year (e.g. "2004" for 1 Jan 2005) 1381 * @li %g the year number in short form of the ISO week of the year (e.g. "04" for 1 Jan 2005) 1382 * @li %D the US short date format (e.g. "%m/%d/%y") 1383 * @li %F the ISO short date format (e.g. "%Y-%m-%d") 1384 * @li %x the KDE locale short date format 1385 * @li %% the literal "%" 1386 * @li %t a tab character 1387 * 1388 * @param dateString the string to convert 1389 * @param dateFormat the date format to use 1390 * @param ok if non-null, will be set to @c true if the date is valid, @c false if invalid 1391 * @param formatStandard the standard the date format uses 1392 * 1393 * @return the string converted to a QDate 1394 * 1395 * @see formatDate 1396 * @see KLocale::readDate 1397 */ 1398 QDate readDate(const QString &dateString, const QString &dateFormat, bool *ok, 1399 KLocale::DateTimeFormatStandard formatStandard) const; 1400 1401 /** 1402 * @since 4.6 1403 * 1404 * Returns the Short Year Window Start Year for the current Calendar System. 1405 * 1406 * Use this function to get the Start Year for the Short Year Window to be 1407 * applied when 2 digit years are entered for a Short Year input format, 1408 * e.g. if the Short Year Window Start Year is 1930, then the input Short 1409 * Year value of 40 is interpreted as 1940 and the input Short Year value 1410 * of 10 is interpreted as 2010. 1411 * 1412 * The Short Year Window is only ever applied when reading the Short Year 1413 * format and not the Long Year format, i.e. KLocale::ShortFormat or '%y' 1414 * only and not KLocale::LongFormat or '%Y'. 1415 * 1416 * The Start Year 0 effectively means not to use a Short Year Window 1417 * 1418 * Each Calendar System requires a different Short Year Window as they have 1419 * different epochs. The Gregorian Short Year Window usually pivots around 1420 * the year 2000, whereas the Hebrew Short Year Window usually pivots around 1421 * the year 5000. 1422 * 1423 * This value must always be used when evaluating user input Short Year 1424 * strings. 1425 * 1426 * @see KLocale::shortYearWindowStartYear 1427 * @see KLocale::applyShortYearWindow 1428 * @return the short year window start year 1429 */ 1430 int shortYearWindowStartYear() const; 1431 1432 /** 1433 * @since 4.6 1434 * 1435 * Returns the Year Number after applying the Year Window. 1436 * 1437 * If the @p inputYear is between 0 and 99, then apply the Year Window and 1438 * return the calculated Year Number. 1439 * 1440 * If the @p inputYear is not between 0 and 99, then the original Year Number 1441 * is returned. 1442 * 1443 * @see KLocale::setYearWindowOffset 1444 * @see KLocale::yearWindowOffset 1445 * @param inputYear the year number to apply the year window to 1446 * @return the year number after applying the year window 1447 */ 1448 int applyShortYearWindow(int inputYear) const; 1449 1450 /** 1451 * Use this to determine which day is the first day of the week. 1452 * 1453 * Uses the calendar system's internal locale set when the instance was 1454 * created, which ensures that the the caller will use the same value 1455 * as the calendar system, which would not necessisarily happen if 1456 * @c KLocale::weekStartDay() was used directly. 1457 * 1458 * @see KLocale::weekStartDay 1459 * 1460 * @return an integer (Monday = 1, ..., Sunday = 7) 1461 */ 1462 inline int weekStartDay() const 1463 { 1464 return locale()->weekStartDay(); 1465 } 1466 1467 /** 1468 * @deprecated use KLocale::weekDayOfPray() instead 1469 * 1470 * Returns the day of the week traditionally associated with religious 1471 * observance for this calendar system. Note this may not be accurate 1472 * for the users locale, e.g. Gregorian calendar used in non-Christian 1473 * countries, in use cases where this could be an issue it is recommended 1474 * to use KLocale::weekDayOfPray() instead. 1475 * 1476 * @return day number (None = 0, Monday = 1, ..., Sunday = 7) 1477 */ 1478 KDELIBS4SUPPORT_DEPRECATED inline int weekDayOfPray() const 1479 { 1480 return locale()->weekDayOfPray(); 1481 } 1482 1483 /** 1484 * Returns whether the calendar is lunar based. 1485 * 1486 * @return @c true if the calendar is lunar based, @c false if not 1487 */ 1488 virtual bool isLunar() const = 0; 1489 1490 /** 1491 * Returns whether the calendar is lunisolar based. 1492 * 1493 * @return @c true if the calendar is lunisolar based, @c false if not 1494 */ 1495 virtual bool isLunisolar() const = 0; 1496 1497 /** 1498 * Returns whether the calendar is solar based. 1499 * 1500 * @return @c true if the calendar is solar based, @c false if not 1501 */ 1502 virtual bool isSolar() const = 0; 1503 1504 /** 1505 * Returns whether the calendar system is proleptic, i.e. whether dates 1506 * before the epoch are supported. 1507 * 1508 * @see KCalendarSystem::epoch 1509 * 1510 * @return @c true if the calendar system is proleptic, @c false if not 1511 */ 1512 virtual bool isProleptic() const = 0; 1513 1514 protected: 1515 /** 1516 * Internal method to convert a Julian Day number into the YMD values for 1517 * this calendar system. 1518 * 1519 * All calendar system implementations MUST implement julianDayToDate and 1520 * dateToJulianDay methods as all other methods can be expressed as 1521 * functions of these. Does no internal validity checking. 1522 * 1523 * @see KCalendarSystem::dateToJulianDay 1524 * 1525 * @param jd Julian day number to convert to date 1526 * @param year year number returned in this variable 1527 * @param month month number returned in this variable 1528 * @param day day of month returned in this variable 1529 * @return @c true if the date is valid, @c false otherwise 1530 */ 1531 virtual bool julianDayToDate(qint64 jd, int &year, int &month, int &day) const = 0; 1532 1533 /** 1534 * Internal method to convert YMD values for this calendar system into a 1535 * Julian Day number. 1536 * 1537 * All calendar system implementations MUST implement julianDayToDate and 1538 * dateToJulianDay methods as all other methods can be expressed as 1539 * functions of these. Does no internal validity checking. 1540 * 1541 * @see KCalendarSystem::julianDayToDate 1542 * 1543 * @param year year number 1544 * @param month month number 1545 * @param day day of month 1546 * @param jd Julian day number returned in this variable 1547 * @return @c true if the date is valid, @c false otherwise 1548 */ 1549 virtual bool dateToJulianDay(int year, int month, int day, qint64 &jd) const = 0; 1550 1551 /** 1552 * Returns the locale used for translations and formats for this 1553 * calendar system instance. This allows a calendar system instance to be 1554 * independent of the global translations and formats if required. All 1555 * implementations must refer to this locale. 1556 * 1557 * Only for internal calendar system use; if public access is required then 1558 * provide public methods only for those methods actually required. Any 1559 * app that creates an instance with its own locale overriding global will 1560 * have the original handle to the locale and can manipulate it that way if 1561 * required, e.g. to change default date format. Only expose those methods 1562 * that library widgets require access to internally. 1563 * 1564 * @see KCalendarSystem::formatDate 1565 * @see KLocale::formatDate 1566 * @see KCalendarSystem::weekStartDay 1567 * @see KLocale::weekStartDay 1568 * @see KCalendarSystem::readDate 1569 * @see KLocale::readDate 1570 * 1571 * @return locale to use 1572 */ 1573 const KLocale *locale() const; 1574 1575 /** 1576 * Constructor of abstract calendar class. This will be called by derived classes. 1577 * 1578 * @param dd derived private d-pointer. 1579 * @param config a configuration file with a 'KCalendarSystem %calendarName' group detailing 1580 * locale-related preferences (such as era options). The global config is used 1581 if null. 1582 * @param locale locale to use for translations. The global locale is used if null. 1583 */ 1584 KCalendarSystem(KCalendarSystemPrivate &dd, const KSharedConfig::Ptr config, const KLocale *locale); 1585 1586 KCalendarSystemPrivate *const d_ptr; 1587 1588 private: 1589 // Other classes that need access to protected/private functions 1590 friend class KLocalizedDate; 1591 friend class KLocalizedDatePrivate; 1592 friend class KDateTimeParser; 1593 friend class KDateTable; 1594 1595 // Era functions needed by friends, may be made public later if needed in KCM 1596 QList<KCalendarEra> *eraList() const; 1597 KCalendarEra era(const QDate &eraDate) const; 1598 KCalendarEra era(const QString &eraName, int yearInEra) const; 1599 1600 Q_DISABLE_COPY(KCalendarSystem) 1601 Q_DECLARE_PRIVATE(KCalendarSystem) 1602 }; 1603 1604 #endif