File indexing completed on 2024-10-13 09:31:26

0001 /*
0002     Copyright 2010 John Layt <john@layt.net>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Library General Public
0006     License as published by the Free Software Foundation; either
0007     version 2 of the License, or (at your option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Library General Public License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to
0016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017     Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #ifndef KLOCALIZEDDATE_H
0021 #define KLOCALIZEDDATE_H
0022 
0023 #include <QString>
0024 #include <QDate>
0025 
0026 #include <kdelibs4support_export.h>
0027 #include "klocale.h"
0028 
0029 class KLocalizedDatePrivate;
0030 /**
0031  * @short A class representing a date localized using the local calendar system, language and formats
0032  *
0033  * Topics:
0034  *  - @ref intro
0035  *  - @ref calsys
0036  *  - @ref custom
0037  *  - @ref formatting
0038  *  - @ref maths
0039  *
0040  * @section intro Introduction
0041  *
0042  * This class provides a simple and convenient way to localize dates
0043  *
0044  * @section calsys Calendar System
0045  *
0046  * KDE supports the use of different calendar systems.
0047  *
0048  * @section custom Default and Custom Locale and Calendar System
0049  *
0050  * In most cases you will want to use the default Global Locale and Calendar
0051  * System, in which case you need only create a default KLocalizedDate.  If
0052  * however you specifically need a different Calendar System or Locale settings
0053  * then you need to take some extra steps.
0054  *
0055  * The simplest method is just changing the Calendar System while keeping the
0056  * current Locale settings.  This is easily done using setCalendarSystem()
0057  * which will copy the current Locale being used and apply this to the new
0058  * Calendar System.  Note this means any changes to the old locale settings,
0059  * either the Global Locale or a custom Locale (see below) will not apply
0060  * to that date instance.
0061  *
0062  * You may however wish to use a custom Locale with the Calendar System.
0063  * For example, if you want your app to normally show dates using the Global
0064  * Locale and Calendar System, but wish to show an info box with the Islamic
0065  * date in Arabic language and format, then you need a custom Locale to do
0066  * this.
0067  *
0068  * \code
0069  * KLocale *myLocale = new KLocale("myapp", "ar", "eg");
0070  * KCalendarSystem *myCalendar = KCalendarSystem::create(KLocale::IslamicCivilCalendar, myLocale);
0071  * KLocalizedDate myDate(QDate(2010,1,1), myCalendar);
0072  * \endcode
0073  *
0074  * In this case you are responsible for the memory management of the KLocale
0075  * and KCalendarSystem.  This allows you to reuse this calendar across multiple
0076  * date instances without it being deleted under you.  It also allows you to
0077  * change any setting in the Locale and have it apply across all those date
0078  * instances.  @warning Don't try changing the Calendar System via your Locale
0079  * instance, your KCalendarSystem instance will be deleted and all the dates
0080  * will be invalid!
0081  *
0082  * @see
0083  *
0084  * @section formatting Date Formatting
0085  *
0086  * When you display dates or date components to users in a GUI, they will
0087  * expect them to be displayed in their language and digit set following their
0088  * local date formatting conventions.  Directly displaying values returned by
0089  * the normal date component methods such as day() will not conform to these
0090  * expectations, so you need to use different methods to obtain the localized
0091  * string form of the date or component.
0092  *
0093  * You can either format the entire date, or just a single component of the
0094  * date such as the month or day.
0095  *
0096  * When formatting a full date, it is preferred to use one of the standard date
0097  * formats defined in the Locale, although you can provide your own format in
0098  * either the KDE, POSIX, or UNICODE standards.
0099  *
0100  * @see formatDate() formatDate()
0101  *
0102  * @section parsing Date Parsing
0103  *
0104  * Basic concepts on date parsing, then full details on KLocale::ReadDateFlags
0105  * formats, definging your own date format strings, and setting how strictly
0106  * the format is appplied.
0107  *
0108  * You can choose how strictly a date format is applied in parsing.  Currently
0109  * only liberal Parsing is supported.
0110  *
0111  * The KLocale::LiberalParsing mode applies the following rules:
0112  *
0113  * 1) You must supply a format and string containing at least one of the following combinations to
0114  * create a valid date:
0115  * @li a month and day of month
0116  * @li a day of year
0117  * @li a ISO week number and day of week
0118  *
0119  * 2) If a year number is not supplied then the current year will be assumed.
0120  *
0121  * 3) All date components must be separated by a non-numeric character.
0122  *
0123  * 4) The format is not applied strictly to the input string:
0124  * @li extra whitespace is ignored
0125  * @li leading 0's on numbers are ignored
0126  * @li capitalisation of literals is ignored
0127  *
0128  * @see readDate()
0129  *
0130  * @section maths Date Maths
0131  *
0132  * A full set of date maths functions are provided which operate in a consistent
0133  * manner, i.e. you can safely round-trip.
0134  *
0135  */
0136 
0137 class KDELIBS4SUPPORT_EXPORT KLocalizedDate
0138 {
0139 public:
0140 
0141     /**
0142      * Constructs a localized date with the given date.
0143      *
0144      * By default, uses the global Calendar System and Locale.
0145      *
0146      * If you pass in a custom Calendar System then you retain ownership of it
0147      * and are responsible for deleting it.  This allows you to reuse the same
0148      * custom Calendar System for many localized date instances.
0149      *
0150      * See @ref custom for more details on using custom Calendar Systems.
0151      *
0152      * @param date the QDate to set the KLocalizedDate to, defaults to invalid date
0153      * @param calendar the calendar system to use, defaults to the global
0154      */
0155     KDELIBS4SUPPORT_DEPRECATED explicit KLocalizedDate(const QDate &date = QDate(), const KCalendarSystem *calendar = nullptr);
0156 
0157     /**
0158      * Constructs a localized date with the given year, month and day.
0159      *
0160      * By default, uses the global Calendar System and Locale.
0161      *
0162      * If you pass in a custom Calendar System then you retain ownership of it
0163      * and are responsible for deleting it.  This allows you to reuse the same
0164      * custom Calendar System for many localized date instances.
0165      *
0166      * See @ref custom for more details on using custom Calendar Systems.
0167      *
0168      * @param year the year to set the KLocalizedDate to
0169      * @param month the month to set the KLocalizedDate to
0170      * @param day the day to set the KLocalizedDate to
0171      * @param calendar the calendar system to use, defaults to the global
0172      */
0173     KLocalizedDate(int year, int month, int day, const KCalendarSystem *calendar = nullptr);
0174 
0175     /**
0176      * Copy constructor
0177      *
0178      * @param rhs the date to copy
0179      */
0180     KLocalizedDate(const KLocalizedDate &rhs);
0181 
0182     /**
0183      * Assignment operator
0184      *
0185      * @param rhs the date to assign
0186      */
0187     KLocalizedDate &operator=(const KLocalizedDate &rhs);
0188 
0189     /**
0190      * Assignment operator
0191      *
0192      * @param rhs the date to assign
0193      */
0194     KLocalizedDate &operator=(const QDate &rhs);
0195 
0196     /**
0197      * Destructor.
0198      */
0199     ~KLocalizedDate();
0200 
0201     /**
0202      * Set the Calendar System used for this date instance only.
0203      *
0204      * This method is mostly useful for when you quickly want to see what the
0205      * currently set date would look like in a different Calendar System but
0206      * using the same Locale.
0207      *
0208      * When the Calendar System is changed, a copy will be taken of the Locale
0209      * previously used and this copy will be applied to the new Calendar System.
0210      * Any changes to the old Locale settings, either the Global or a Custom
0211      * Locale, will not be applied to this date instance.
0212      *
0213      * See @ref custom for more details on using custom Calendar Systems.
0214      *
0215      * @see KLocale::CalendarSystem
0216      * @see calendarSystem()
0217      * @param calendarSystem the Calendar System to use
0218      */
0219     void setCalendarSystem(KLocale::CalendarSystem calendarSystem);
0220 
0221     /**
0222      * Returns the Calendar System used by this localized date instance
0223      *
0224      * @see KLocale::CalendarSystem
0225      * @see setCalendarSystem()
0226      * @return the Calendar System currently used
0227      */
0228     KLocale::CalendarSystem calendarSystem();
0229 
0230     /**
0231      * Returns a pointer to the Calendar System object used by this date instance.
0232      *
0233      * Usually this will be the Global Calendar System, but this may have been
0234      * changed.
0235      *
0236      * Normally you will not need to access this object unless the KLocalizedDate
0237      * API does not provide the methods you require.
0238      *
0239      * @see KCalendarSystem
0240      * @see calendarSystem
0241      * @see setCalendarSystem
0242      * @return the current calendar system instance
0243      */
0244     const KCalendarSystem *calendar() const;
0245 
0246     /**
0247      * Returns whether the date is null, i.e. invalid in any Calendar System.
0248      *
0249      * @see isValid
0250      * @return @c true if the date is null, @c false otherwise
0251      */
0252     bool isNull() const;
0253 
0254     /**
0255      * Returns whether the date is valid in the current Calendar System.
0256      *
0257      * @see isNull
0258      * @return @c true if the date is valid, @c false otherwise
0259      */
0260     bool isValid() const;
0261 
0262     /**
0263      * Set the date using a QDate.
0264      *
0265      * @param date the QDate to set to
0266      * @return @c true if the date is valid, @c false otherwise
0267      */
0268     bool setDate(const QDate &date);
0269 
0270     /**
0271      * Set the date's year, month and day.
0272      *
0273      * The range of the year, month and day, and the validity of the date as a
0274      * whole depends on which Calendar System is being used.
0275      *
0276      * @see getDate()
0277      * @param year year number
0278      * @param month month of year number
0279      * @param day day of month
0280      * @return @c true if the date is valid, @c false otherwise
0281      */
0282     bool setDate(int year, int month, int day);
0283 
0284     /**
0285      * Set the date using the year number and day of year number only.
0286      *
0287      * @see dayOfYear()
0288      * @param year year
0289      * @param dayOfYear day of year
0290      * @return @c true if the date is valid, @c false otherwise
0291      */
0292     bool setDate(int year, int dayOfYear);
0293 
0294     /**
0295      * Set the date using the era, year in era number, month and day
0296      *
0297      * @see eraName()
0298      * @see yearInEra()
0299      * @param eraName Era string
0300      * @param yearInEra Year In Era number
0301      * @param month Month number
0302      * @param day Day Of Month number
0303      * @return @c true if the date is valid, @c false otherwise
0304      */
0305     bool setDate(QString eraName, int yearInEra, int month, int day);
0306 
0307     /**
0308      * Set the date using the year, week and day of week.
0309      *
0310      * Currently only the ISO Week Number System is supported.
0311      *
0312      * @see week()
0313      * @see dayOfWeek()
0314      * @param weekNumberSystem the week number system to use
0315      * @param year year
0316      * @param weekOfYear week of year
0317      * @param dayOfWeek day of week Mon..Sun (1..7)
0318      * @return @c true if the date is valid, @c false otherwise
0319      */
0320     bool setDate(KLocale::WeekNumberSystem weekNumberSystem, int year, int weekOfYear, int dayOfWeek);
0321 
0322     /**
0323      * Set the date to today's date
0324      *
0325      * @see currentDate()
0326      * @return @c true if the date is valid, @c false otherwise
0327      */
0328     bool setCurrentDate();
0329 
0330     /**
0331      * Returns a KLocalizedDate set to today's date in the Global Locale and
0332      * Calendar System.
0333      *
0334      * @see setCurrentDate()
0335      * @return today's localized date
0336      */
0337     static KLocalizedDate currentDate();
0338 
0339     /**
0340      * Returns a KLocalizedDate set the required date in the Global Locale and
0341      * Calendar System.
0342      *
0343      * @param date the date to set to
0344      * @return a localized date
0345      */
0346     static KLocalizedDate fromDate(const QDate &date);
0347 
0348     /**
0349      * Returns a KLocalizedDate set the required Julian Day number in the Global
0350      * Locale and Calendar System.
0351      *
0352      * @see toJulianDay()
0353      * @param jd the Julian Day number to set to
0354      * @return a localized date
0355      */
0356     static KLocalizedDate fromJulianDay(int jd);
0357 
0358     /**
0359      * Returns the currently set date as a Julian Day number
0360      *
0361      * @see fromJulianDay()
0362      * @return the currently set date as a Julian Day number
0363      */
0364     int toJulianDay() const;
0365 
0366     /**
0367      * Returns the currently set date as a QDate
0368      *
0369      * @return the currently set date as a QDate
0370      */
0371     QDate date() const;
0372 
0373     /**
0374      * Returns the year, month and day portion of the date in the current
0375      * Calendar System.
0376      *
0377      * See @ref formatting for why you should never display this value.
0378      *
0379      * @see setDate()
0380      * @see formatDate()
0381      * @param year year number returned in this variable
0382      * @param month month number returned in this variable
0383      * @param day day of month returned in this variable
0384      */
0385     void getDate(int *year, int *month, int *day) const;
0386 
0387     /**
0388      * Returns the year portion of the date in the current calendar system
0389      *
0390      * See @ref formatting for why you should never display this value.
0391      *
0392      * @see formatDate()
0393      * @return the localized year number
0394      */
0395     int year() const;
0396 
0397     /**
0398      * Returns the month portion of the date in the current calendar system
0399      *
0400      * See @ref formatting for why you should never display this value.
0401      *
0402      * @see formatDate()
0403      * @return the localized month number, 0 if date is invalid
0404      */
0405     int month() const;
0406 
0407     /**
0408      * Returns the day portion of the date in the current calendar system
0409      *
0410      * See @ref formatting for why you should never display this value.
0411      *
0412      * @see formatDate()
0413      * @return the localized day number, 0 if date is invalid
0414      */
0415     int day() const;
0416 
0417     /**
0418      * Returns the Era Name portion of the date in the current calendar system,
0419      * for example "AD" or "Anno Domini" for the Gregorian calendar and Christian Era.
0420      *
0421      * See @ref formatting for more details on Date Formatting.
0422      *
0423      * @see formatDate()
0424      * @return the localized era name, empty string if date is invalid
0425      */
0426     QString eraName() const;
0427 
0428     /**
0429      * Returns the Era Year portion of the date in the current
0430      * calendar system, for example "2000 AD" or "Heisei 22".
0431      *
0432      * See @ref formatting for more details on Date Formatting.
0433      *
0434      * @see formatDate()
0435      * @return the localized era year string, empty string if date is invalid
0436      */
0437     QString eraYear() const;
0438 
0439     /**
0440      * Returns the Year In Era portion of the date in the current calendar
0441      * system, for example 1 for "1 BC".
0442      *
0443      * See @ref formatting for why you should never display this value.
0444      *
0445      * @see formatDate()
0446      * @see formatYearInEra()
0447      * @return the localized Year In Era number, -1 if date is invalid
0448      */
0449     int yearInEra() const;
0450 
0451     /**
0452      * Returns the day number of year for the date
0453      *
0454      * The days are numbered 1..daysInYear()
0455      *
0456      * See @ref formatting for why you should never display this value.
0457      *
0458      * @see formatDate()
0459      * @return day of year number, -1 if date not valid
0460      */
0461     int dayOfYear() const;
0462 
0463     /**
0464      * Returns the weekday number for the date
0465      *
0466      * The weekdays are numbered 1..7 for Monday..Sunday.
0467      *
0468      * This value is @em not affected by the value of KLocale::weekStartDay()
0469      *
0470      * See @ref formatting for why you should never display this value.
0471      *
0472      * @see formatDate()
0473      * @return day of week number, -1 if date not valid
0474      */
0475     int dayOfWeek() const;
0476 
0477     /**
0478      * Returns the localized Week Number for the date.
0479      *
0480      * See @ref formatting for why you should never display this value.
0481      *
0482      * This may be ISO, US, or any other supported week numbering scheme.  If
0483      * you specifically require the ISO Week or any other scheme, you should use
0484      * the week(KLocale::WeekNumberSystem) form.
0485      *
0486      * If the date falls in the last week of the previous year or the first
0487      * week of the following year, then the yearNum returned will be set to the
0488      * appropriate year.
0489      *
0490      * @see weeksInYear()
0491      * @see formatDate()
0492      * @param yearNum returns the year the date belongs to
0493      * @return localized week number, -1 if input date invalid
0494      */
0495     int week(int *yearNum = nullptr) const;
0496 
0497     /**
0498      * Returns the Week Number for the date in the required Week Number System.
0499      *
0500      * See @ref formatting for why you should never display this value.
0501      *
0502      * Unless you want a specific Week Number System (e.g. ISO Week), you should
0503      * use the localized Week Number form of week().
0504      *
0505      * If the date falls in the last week of the previous year or the first
0506      * week of the following year, then the yearNum returned will be set to the
0507      * appropriate year.
0508      *
0509      * Technically, the ISO Week Number only applies to the ISO/Gregorian Calendar
0510      * System, but the same rules will be applied to the current Calendar System.
0511      *
0512      * @see weeksInYear()
0513      * @see formatDate()
0514      * @param weekNumberSystem the Week Number System to use
0515      * @param yearNum returns the year the date belongs to
0516      * @return week number, -1 if input date invalid
0517      */
0518     int week(KLocale::WeekNumberSystem weekNumberSystem, int *yearNum = nullptr) const;
0519 
0520     /**
0521      * Returns number of months in the year
0522      *
0523      * See @ref formatting for why you should never display this value.
0524      *
0525      * @see formatDate()
0526      * @return number of months in the year, -1 if  date invalid
0527      */
0528     int monthsInYear() const;
0529 
0530     /**
0531      * Returns the number of localized weeks in the currently set year.
0532      *
0533      * See @ref formatting for why you should never display this value.
0534      *
0535      * If you specifically require the number of ISO Weeks, you should use
0536      * weeksInYear(KLocale::IsoWeekNumber)
0537      *
0538      * @see week()
0539      * @see formatDate()
0540      * @return number of weeks in the year, -1 if  date invalid
0541      */
0542     int weeksInYear() const;
0543 
0544     /**
0545      * Returns the number of Weeks in the currently set year using the required
0546      * Week Number System.
0547      *
0548      * See @ref formatting for why you should never display this value.
0549      *
0550      * Unless you specifically want a particular Week Number System (e.g. ISO Weeks)
0551      * you should use the localized number of weeks provided by weeksInYear().
0552      *
0553      * @see week()
0554      * @see formatDate()
0555      * @param weekNumberSystem the week number system to use
0556      * @return number of weeks in the year, -1 if  date invalid
0557      */
0558     int weeksInYear(KLocale::WeekNumberSystem weekNumberSystem) const;
0559 
0560     /**
0561      * Returns the number of days in the year.
0562      *
0563      * For example, in the Gregorian calendar most years have 365 days but Leap
0564      * Years have 366 years.  Other Calendar Systems have different length years.
0565      *
0566      * See @ref formatting for why you should never display this value.
0567      *
0568      * @see formatDate()
0569      * @return number of days in year, -1 if date invalid
0570      */
0571     int daysInYear() const;
0572 
0573     /**
0574      * Returns the number of days in the month.
0575      *
0576      * See @ref formatting for why you should never display this value.
0577      *
0578      * @see formatDate()
0579      * @return number of days in month, -1 if date invalid
0580      */
0581     int daysInMonth() const;
0582 
0583     /**
0584      * Returns the number of days in the week.
0585      *
0586      * See @ref formatting for why you should never display this value.
0587      *
0588      * @see formatDate()
0589      * @return number of days in week, -1 if date invalid
0590      */
0591     int daysInWeek() const;
0592 
0593     /**
0594      * Returns whether the currently set date falls in a Leap Year in the
0595      * current Calendar System.
0596      *
0597      * @return true if the date falls in a leap year
0598      */
0599     bool isLeapYear() const;
0600 
0601     /**
0602      * Returns the Date as a localized string in the requested standard Locale
0603      * format.
0604      *
0605      * See @ref formatting for more details on Date Formatting and valid Locale
0606      * formats.
0607      *
0608      * @see formatDate()
0609      * @param dateFormat the standard date format to use
0610      * @return The date as a localized string
0611      */
0612     QString formatDate(KLocale::DateFormat dateFormat = KLocale::LongDate) const;
0613 
0614     /**
0615      * Returns the Date as a localized string in the requested format.
0616      *
0617      * See @ref formatting for more details on Date Formatting and valid format
0618      * codes.
0619      *
0620      * Please use with care and only in situations where the standard Locale
0621      * formats or the component format methods do not provide what you
0622      * need.  You should almost always translate your @p formatString as
0623      * documented above.  Using the standard DateFormat options instead would
0624      * take care of the translation for you.
0625      *
0626      * The toFormat parameter is a good candidate to be made translatable,
0627      * so that translators can adapt it to their language's convention.
0628      * There should also be a context using the "kdedt-format" keyword (for
0629      * automatic validation of translations) and stating the format's purpose:
0630      * \code
0631      * QDate reportDate;
0632      * KLocale::global()->calendar()->setDate(reportDate, reportYear, reportMonth, 1);
0633      * dateFormat = i18nc("(kdedt-format) Report month and year in report header", "%B %Y"));
0634      * dateString = KLocale::global()->calendar()->formatDate(reportDate, dateFormat);
0635      * \endcode
0636      *
0637      * The date format string can be defined using either the KDE, POSIX or the Qt
0638      * subset of the UNICODE standards.
0639      *
0640      * The KDE standard closely follows the POSIX standard but with some exceptions.
0641      * Always use the KDE standard within KDE, but where interaction is required with
0642      * external POSIX compliant systems (e.g. Gnome, glibc, etc) the POSIX standard
0643      * should be used.  The UNICODE standard is provided for comaptability with QDate
0644      * and so is not yet the full standard, only what Qt currently supports.
0645      *
0646      * Date format strings are made up of date components and string literals.
0647      * Date components are prefixed by a % escape character and are made up of
0648      * optional padding and case modifier flags, an optional width value, and a
0649      * compulsary code for the actual date component:
0650      *   %[Flags][Width][Componant]
0651      * e.g. %_^5Y
0652      * No spaces are allowed.
0653      *
0654      * The Flags can modify the padding character and/or case of the Date Componant.
0655      * The Flags are optional and may be combined and/or repeated in any order,
0656      * in which case the last Padding Flag and last Case Flag will be the
0657      * ones used.  The Flags must be immediately after the % and before any Width.
0658      *
0659      * The Width can modify how wide the date Componant is padded to.  The Width
0660      * is an optional interger value and must be after any Flags but before the
0661      * Componant.  If the Width is less than the minimum defined for a Componant
0662      * then the default minimum will be used instead.
0663      *
0664      * By default most numeric Date Componants are right-aligned with leading 0's.
0665      *
0666      * By default all string name fields are capital case and unpadded.
0667      *
0668      * The following Flags may be specified:
0669      * @li - (hyphen) no padding (e.g. 1 Jan and "%-j" = "1")
0670      * @li _ (underscore) pad with spaces (e.g. 1 Jan and "%-j" = "  1")
0671      * @li 0 (zero) pad with 0's  (e.g. 1 Jan and "%0j" = "001")
0672      * @li ^ (caret) make uppercase (e.g. 1 Jan and "%^B" = "JANUARY")
0673      * @li # (hash) invert case (e.g. 1 Jan and "%#B" = "???")
0674      *
0675      * The following Date Componants can be specified:
0676      * @li %Y the year to 4 digits (e.g. "1984" for 1984, "0584" for 584, "0084" for 84)
0677      * @li %C the 'century' portion of the year to 2 digits (e.g. "19" for 1984, "05" for 584, "00" for 84)
0678      * @li %y the lower 2 digits of the year to 2 digits (e.g. "84" for 1984, "05" for 2005)
0679      * @li %EY the full local era year (e.g. "2000 AD")
0680      * @li %EC the era name short form (e.g. "AD")
0681      * @li %Ey the year in era to 1 digit (e.g. 1 or 2000)
0682      * @li %m the month number to 2 digits (January="01", December="12")
0683      * @li %n the month number to 1 digit (January="1", December="12"), see notes!
0684      * @li %d the day number of the month to 2 digits (e.g. "01" on the first of March)
0685      * @li %e the day number of the month to 1 digit (e.g. "1" on the first of March)
0686      * @li %B the month name long form (e.g. "January")
0687      * @li %b the month name short form (e.g. "Jan" for January)
0688      * @li %h the month name short form (e.g. "Jan" for January)
0689      * @li %A the weekday name long form (e.g. "Wednesday" for Wednesday)
0690      * @li %a the weekday name short form (e.g. "Wed" for Wednesday)
0691      * @li %j the day of the year number to 3 digits (e.g. "001"  for 1 Jan)
0692      * @li %V the ISO week of the year number to 2 digits (e.g. "01"  for ISO Week 1)
0693      * @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)
0694      * @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)
0695      * @li %u the day of the week number to 1 digit (e.g. "1"  for Monday)
0696      * @li %D the US short date format (e.g. "%m/%d/%y")
0697      * @li %F the ISO short date format (e.g. "%Y-%m-%d")
0698      * @li %x the KDE locale short date format
0699      * @li %% the literal "%"
0700      * @li %t a tab character
0701      *
0702      * Everything else in the format string will be taken as literal text.
0703      *
0704      * Examples:
0705      * "%Y-%m-%d" = "2009-01-01"
0706      * "%Y-%-m-%_4d" = "2009-1-   1"
0707      *
0708      * The following format codes behave differently in the KDE and POSIX standards
0709      * @li %e in GNU/POSIX is space padded to 2 digits, in KDE is not padded
0710      * @li %n in GNU/POSIX is newline, in KDE is short month number
0711      *
0712      * The following POSIX format codes are currently not supported:
0713      * @li %U US week number
0714      * @li %w US day of week
0715      * @li %W US week number
0716      * @li %O locale's alternative numeric symbols, in KDE is not supported
0717      *
0718      * %0 is not supported as the returned result is always in the locale's chosen numeric symbol digit set.
0719      *
0720      * @see formatDate()
0721      * @param formatString the date format to use
0722      * @param formatStandard the standard the @p dateFormat uses, defaults to KDE Standard
0723      * @return The date as a localized string
0724      */
0725     QString formatDate(const QString &formatString,
0726                        KLocale::DateTimeFormatStandard formatStandard = KLocale::KdeFormat) const;
0727 
0728     /**
0729      * Returns a Date Component as a localized string in the requested format.
0730      *
0731      * See @ref formatting for more details on Date Formatting.
0732      *
0733      * Each format size may vary depending on Locale and Calendar System but will
0734      * generally match the format description.  Some formats may not be directly
0735      * valid but a sensible value will always be returned.
0736      *
0737      * For example for 2010-01-01 the KLocale::Month with en_US Locale and Gregorian calendar may return:
0738      *   KLocale::ShortNumber = "1"
0739      *   KLocale::LongNumber  = "01"
0740      *   KLocale::NarrowName  = "J"
0741      *   KLocale::ShortName   = "Jan"
0742      *   KLocale::LongName    = "January"
0743      *
0744      * @see formatDate()
0745      * @param component The date component to return
0746      * @param format The format to return the @p component in
0747      * @param weekNumberSystem To override the default Week Number System to use
0748      * @return The string form of the date component
0749      */
0750     QString formatDate(KLocale::DateTimeComponent component,
0751                        KLocale::DateTimeComponentFormat format = KLocale::DefaultComponentFormat,
0752                        KLocale::WeekNumberSystem weekNumberSystem = KLocale::DefaultWeekNumber) const;
0753 
0754     /**
0755      * Converts a localized date string to a KLocalizedDate using either the
0756      * Global Calendar System and Locale, or the provided Calendar System.
0757      *
0758      * See @ref parsing for more details on Date Parsing from strings.
0759      *
0760      * This method is more liberal and will return a valid date if the
0761      * @p dateString matches any of the KLocale::ReadDateFlags formats
0762      * for the Locale.
0763      *
0764      * If you require a certain KLocale::ReadDateFlags format or a customized
0765      * format string, use one of the other readDate() methods.
0766      *
0767      * @param dateString the string to parse
0768      * @param parseMode how strictly to apply the locale formats to the @p dateString
0769      * @param calendar the Calendar System to use when parsing the date
0770      * @return the localized date parsed from the string
0771      */
0772     static KLocalizedDate readDate(const QString &dateString,
0773                                    KLocale::DateTimeParseMode parseMode = KLocale::LiberalParsing,
0774                                    const KCalendarSystem *calendar = nullptr);
0775 
0776     /**
0777      * Converts a localized date string to a KLocalizedDate using either the
0778      * Global Calendar System and Locale, or the provided Calendar System.
0779      *
0780      * See @ref parsing for more details on Date Parsing from strings.
0781      *
0782      * This method is stricter and will return a valid date only if the
0783      * @p dateString matches one of the @p dateFlags formats requested.
0784      *
0785      * If you require any KLocale::ReadDateFlags format or a customized format
0786      * string, use one of the other readDate() methods.
0787      *
0788      * @param dateString the string to parse
0789      * @param formatFlags the locale format(s) to try parse the string with
0790      * @param parseMode how strictly to apply the @p formatFlags to the @p dateString
0791      * @param calendar the Calendar System to use when parsing the date
0792      * @return the localized date parsed from the string
0793      */
0794     static KLocalizedDate readDate(const QString &dateString,
0795                                    KLocale::ReadDateFlags formatFlags,
0796                                    KLocale::DateTimeParseMode parseMode = KLocale::LiberalParsing,
0797                                    const KCalendarSystem *calendar = nullptr);
0798 
0799     /**
0800      * Converts a localized date string to a KLocalizedDate using either the
0801      * Global Calendar System and Locale, or the provided Calendar System.
0802      *
0803      * See @ref parsing for more details on Date Parsing from strings.
0804      *
0805      * This method allows you to define your own date format to parse the date
0806      * string with.
0807      *
0808      * If you require one of the standard any KLocale::ReadDateFlags formats
0809      * then use one of the other readDate() methods.
0810      *
0811      * @param dateString the string to parse
0812      * @param dateFormat the date format to try parse the string with
0813      * @param parseMode how strictly to apply the @p dateFormat to the @p dateString
0814      * @param formatStandard the standard the @p dateFormat format uses
0815      * @param calendar the Calendar System to use when parsing the date
0816      * @return the localized date parsed from the string
0817      */
0818     static KLocalizedDate readDate(const QString &dateString,
0819                                    const QString &dateFormat,
0820                                    KLocale::DateTimeParseMode parseMode = KLocale::LiberalParsing,
0821                                    KLocale::DateTimeFormatStandard formatStandard = KLocale::KdeFormat,
0822                                    const KCalendarSystem *calendar = nullptr);
0823 
0824     /**
0825      * Returns a KLocalizedDate containing a date @p years years later.
0826      *
0827      * @see addYearsTo()
0828      * @see addMonths() addDays()
0829      * @see dateDifference() yearsDifference()
0830      * @param years The number of years to add
0831      * @return The new date, null date if any errors
0832      */
0833     KLocalizedDate addYears(int years) const;
0834 
0835     /**
0836      * Add years onto this date instance.
0837      *
0838      * If the result of the addition is invalid in the current Calendar System
0839      * then the date will become invalid.
0840      *
0841      * @see addYears()
0842      * @see addMonthsTo() addDaysTo()
0843      * @see dateDifference() yearsDifference()
0844      * @param years The number of years to add
0845      * @return if the resulting date is valid
0846      */
0847     bool addYearsTo(int years);
0848 
0849     /**
0850      * Returns a KLocalizedDate containing a date @p months months later.
0851      *
0852      * @see addMonthsTo()
0853      * @see addYears() addDays()
0854      * @see dateDifference() yearsDifference()
0855      * @param months number of months to add
0856      * @return The new date, null date if any errors
0857      */
0858     KLocalizedDate addMonths(int months) const;
0859 
0860     /**
0861      * Add months onto this date instance.
0862      *
0863      * If the result of the addition is invalid in the current Calendar System
0864      * then the date will become invalid.
0865      *
0866      * @see addMonths()
0867      * @see addYearsTo() addDaysTo()
0868      * @see dateDifference() yearsDifference()
0869      * @param months The number of months to add
0870      * @return if the resulting date is valid
0871      */
0872     bool addMonthsTo(int months);
0873 
0874     /**
0875      * Returns a KLocalizedDate containing a date @p days days later.
0876      *
0877      * @see addDaysTo()
0878      * @see addYears() addMonths()
0879      * @see dateDifference() yearsDifference()
0880      * @param days number of days to add
0881      * @return The new date, null date if any errors
0882      */
0883     KLocalizedDate addDays(int days) const;
0884 
0885     /**
0886      * Add days onto this date instance.
0887      *
0888      * If the result of the addition is invalid in the current Calendar System
0889      * then the date will become invalid.
0890      *
0891      * @see addDays()
0892      * @see addYearsTo(), addMonthsTo()
0893      * @see dateDifference() yearsDifference()
0894      * @param days The number of days to add
0895      * @return if the resulting date is valid
0896      */
0897     bool addDaysTo(int days);
0898 
0899     /**
0900      * Returns the difference between this and another date in years, months and days
0901      * in the current Calendar System.
0902      *
0903      * The difference is always calculated from the earlier date to the later
0904      * date in year, month and day order, with the @p direction parameter
0905      * indicating which direction the difference is applied from this date.
0906      * In other words, this difference can be added onto the earlier date in
0907      * year, month, day order to reach the later date.
0908      *
0909      * For example, the difference between 2010-06-10 and 2012-09-5 is 2 years,
0910      * 2 months and 26 days.  Note that the difference between two last days of
0911      * the month is always 1 month, e.g. 2010-01-31 to 2010-02-28 is 1 month
0912      * not 28 days.
0913      *
0914      * @see addYears() addMonths() addDays()
0915      * @see yearsDifference() monthsDifference() daysDifference()
0916      * @param toDate The date to end at
0917      * @param yearsDiff Returns number of years difference
0918      * @param monthsDiff Returns number of months difference
0919      * @param daysDiff Returns number of days difference
0920      * @param direction Returns direction of difference, 1 if this Date <= toDate, -1 otherwise
0921      */
0922     void dateDifference(const KLocalizedDate &toDate,
0923                         int *yearsDiff, int *monthsDiff, int *daysDiff, int *direction) const;
0924 
0925     /**
0926      * Returns the difference between this and another date in years, months and days
0927      * in the current Calendar System.
0928      *
0929      * The difference is always calculated from the earlier date to the later
0930      * date in year, month and day order, with the @p direction parameter
0931      * indicating which direction the difference is applied from this date.
0932      * In other words, this difference can be added onto the earlier date in
0933      * year, month, day order to reach the later date.
0934      *
0935      * For example, the difference between 2010-06-10 and 2012-09-5 is 2 years,
0936      * 2 months and 26 days.  Note that the difference between two last days of
0937      * the month is always 1 month, e.g. 2010-01-31 to 2010-02-28 is 1 month
0938      * not 28 days.
0939      *
0940      * @see addYears() addMonths() addDays()
0941      * @see yearsDifference() monthsDifference() daysDifference()
0942      * @param toDate The date to end at
0943      * @param yearsDiff Returns number of years difference
0944      * @param monthsDiff Returns number of months difference
0945      * @param daysDiff Returns number of days difference
0946      * @param direction Returns direction of difference, 1 if this Date <= toDate, -1 otherwise
0947      */
0948     void dateDifference(const QDate &toDate,
0949                         int *yearsDiff, int *monthsDiff, int *daysDiff, int *direction) const;
0950 
0951     /**
0952      * Returns the difference between this and another date in completed calendar years
0953      * in the current Calendar System.
0954      *
0955      * The returned value will be negative if @p toDate < this date.
0956      *
0957      * For example, the difference between 2010-06-10 and 2012-09-5 is 2 years.
0958      *
0959      * @see addYears()
0960      * @see dateDifference() monthsDifference() daysDifference()
0961      * @param toDate The date to end at
0962      * @return The number of years difference
0963      */
0964     int yearsDifference(const KLocalizedDate &toDate) const;
0965 
0966     /**
0967      * Returns the difference between this and another date in completed calendar years
0968      * in the current Calendar System.
0969      *
0970      * The returned value will be negative if @p toDate < this date.
0971      *
0972      * For example, the difference between 2010-06-10 and 2012-09-5 is 2 years.
0973      *
0974      * @see addYears()
0975      * @see dateDifference() monthsDifference() daysDifference()
0976      * @param toDate The date to end at
0977      * @return The number of years difference
0978      */
0979     int yearsDifference(const QDate &toDate) const;
0980 
0981     /**
0982      * Returns the difference between this and another date in completed calendar months
0983      * in the current Calendar System.
0984      *
0985      * The returned value will be negative if @p toDate < this date.
0986      *
0987      * For example, the difference between 2010-06-10 and 2012-09-5 is 26 months.
0988      * Note that the difference between two last days of the month is always 1
0989      * month, e.g. 2010-01-31 to 2010-02-28 is 1 month not 28 days.
0990      *
0991      * @see addMonths()
0992      * @see dateDifference() yearsDifference() daysDifference()
0993      * @param toDate The date to end at
0994      * @return The number of months difference
0995      */
0996     int monthsDifference(const KLocalizedDate &toDate) const;
0997 
0998     /**
0999      * Returns the difference between this and another date in completed calendar months
1000      * in the current Calendar System.
1001      *
1002      * The returned value will be negative if @p toDate < this date.
1003      *
1004      * For example, the difference between 2010-06-10 and 2012-09-5 is 26 months.
1005      * Note that the difference between two last days of the month is always 1
1006      * month, e.g. 2010-01-31 to 2010-02-28 is 1 month not 28 days.
1007      *
1008      * @see addMonths()
1009      * @see dateDifference() yearsDifference() daysDifference()
1010      * @param toDate The date to end at
1011      * @return The number of months difference
1012      */
1013     int monthsDifference(const QDate &toDate) const;
1014 
1015     /**
1016      * Returns the difference between this and another date in days
1017      * The returned value will be negative if @p toDate < this date.
1018      *
1019      * @see addDays()
1020      * @see dateDifference() yearsDifference() monthsDifference()
1021      * @param toDate The date to end at
1022      * @return The number of days difference
1023      */
1024     int daysDifference(const KLocalizedDate &toDate) const;
1025 
1026     /**
1027      * Returns the difference between this and another date in days
1028      * The returned value will be negative if @p toDate < this date.
1029      *
1030      * @see addDays()
1031      * @see dateDifference() yearsDifference() monthsDifference()
1032      * @param toDate The date to end at
1033      * @return The number of days difference
1034      */
1035     int daysDifference(const QDate &toDate) const;
1036 
1037     /**
1038      * Returns a KLocalizedDate containing the first day of the currently set year
1039      *
1040      * @see lastDayOfYear()
1041      * @return The first day of the year
1042      */
1043     KLocalizedDate firstDayOfYear() const;
1044 
1045     /**
1046      * Returns a KLocalizedDate containing the last day of the currently set year
1047      *
1048      * @see firstDayOfYear()
1049      * @return The last day of the year
1050      */
1051     KLocalizedDate lastDayOfYear() const;
1052 
1053     /**
1054      * Returns a KLocalizedDate containing the first day of the currently set month
1055      *
1056      * @see lastDayOfMonth()
1057      * @return The first day of the month
1058      */
1059     KLocalizedDate firstDayOfMonth() const;
1060 
1061     /**
1062      * Returns a KLocalizedDate containing the last day of the currently set month
1063      *
1064      * @see firstDayOfMonth()
1065      * @return The last day of the month
1066      */
1067     KLocalizedDate lastDayOfMonth() const;
1068 
1069     /**
1070      * KLocalizedDate equality operator
1071      *
1072      * @param other the date to compare
1073      */
1074     bool operator==(const KLocalizedDate &other) const;
1075 
1076     /**
1077      * QDate equality operator
1078      *
1079      * @param other the date to compare
1080      */
1081     bool operator==(const QDate &other) const;
1082 
1083     /**
1084      * KLocalizedDate inequality operator
1085      *
1086      * @param other the date to compare
1087      */
1088     bool operator!=(const KLocalizedDate &other) const;
1089 
1090     /**
1091      * QDate inequality operator
1092      *
1093      * @param other the date to compare
1094      */
1095     bool operator!=(const QDate &other) const;
1096 
1097     /**
1098      * KLocalizedDate less than operator
1099      *
1100      * @param other the date to compare
1101      */
1102     bool operator<(const KLocalizedDate &other) const;
1103 
1104     /**
1105      * QDate less than operator
1106      *
1107      * @param other the date to compare
1108      */
1109     bool operator<(const QDate &other) const;
1110 
1111     /**
1112      * KLocalizedDate less than or equal to operator
1113      *
1114      * @param other the date to compare
1115      */
1116     bool operator<=(const KLocalizedDate &other) const;
1117 
1118     /**
1119      * QDate less than or equal to operator
1120      *
1121      * @param other the date to compare
1122      */
1123     bool operator<=(const QDate &other) const;
1124 
1125     /**
1126      * KLocalizedDate greater than operator
1127      *
1128      * @param other the date to compare
1129      */
1130     bool operator>(const KLocalizedDate &other) const;
1131 
1132     /**
1133      * QDate greater than operator
1134      *
1135      * @param other the date to compare
1136      */
1137     bool operator>(const QDate &other) const;
1138 
1139     /**
1140      * KLocalizedDate greater than or equal to operator
1141      *
1142      * @param other the date to compare
1143      */
1144     bool operator>=(const KLocalizedDate &other) const;
1145 
1146     /**
1147      * QDate greater than or equal to operator
1148      *
1149      * @param other the date to compare
1150      */
1151     bool operator>=(const QDate &other) const;
1152 
1153 private:
1154 
1155     friend QDataStream KDELIBS4SUPPORT_EXPORT &operator<<(QDataStream &out, const KLocalizedDate &date);
1156     friend QDataStream KDELIBS4SUPPORT_EXPORT &operator>>(QDataStream &in, KLocalizedDate &date);
1157     friend QDebug KDELIBS4SUPPORT_EXPORT operator<<(QDebug, const KLocalizedDate &);
1158 
1159     QSharedDataPointer<KLocalizedDatePrivate> d;
1160 };
1161 
1162 Q_DECLARE_METATYPE(KLocalizedDate)
1163 
1164 /**
1165  * Data stream output operator
1166  *
1167  * @param out the datastream to write to
1168  * @param date the date to write to the stream
1169  */
1170 QDataStream KDELIBS4SUPPORT_EXPORT &operator<<(QDataStream &out, const KLocalizedDate &date);
1171 
1172 /**
1173  * Data stream input operator
1174  *
1175  * @param in the datastream to read from
1176  * @param date the date to read from the stream
1177  */
1178 QDataStream KDELIBS4SUPPORT_EXPORT &operator>>(QDataStream &in, KLocalizedDate &date);
1179 
1180 /**
1181  * Debug stream output operator
1182  *
1183  * @param debug the debug datastream to write to
1184  * @param date the date to write to the stream
1185  */
1186 QDebug KDELIBS4SUPPORT_EXPORT operator<<(QDebug debug, const KLocalizedDate &date);
1187 
1188 #endif // KLOCALIZEDDATE_H