KDELibs4Support

kcalendarsystem.h
1 /*
2  Copyright (c) 2002 Carlos Moro <[email protected]>
3  Copyright (c) 2002-2003 Hans Petter Bieker <[email protected]>
4  Copyright 2007, 2009, 2010 John Layt <[email protected]>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #ifndef KCALENDARSYSTEM_H
23 #define KCALENDARSYSTEM_H
24 
25 #include <kdelibs4support_export.h>
26 #include "klocale.h" // needed for enums
27 
28 #include <QStringList>
29 #include <QDate>
30 
31 class KCalendarSystemPrivate;
32 class KCalendarEra;
33 
39 class KDELIBS4SUPPORT_EXPORT KCalendarSystem
40 {
41 public:
42 
46  enum StringFormat {
48  LongFormat
49  };
50 
59  NarrowName
60  };
61 
68  NarrowDayName
69  };
70 
80  static KCalendarSystem *create(KLocale::CalendarSystem calendarSystem,
81  const KLocale *locale);
82 
95  static KCalendarSystem *create(KLocale::CalendarSystem calendarSystem = KLocale::QDateCalendar,
96  KSharedConfig::Ptr config = KSharedConfig::Ptr(),
97  const KLocale *locale = nullptr);
98 
106  static QList<KLocale::CalendarSystem> calendarSystemsList();
107 
120  static QString calendarLabel(KLocale::CalendarSystem calendarSystem, const KLocale *locale = KLocale::global());
121 
125  virtual ~KCalendarSystem();
126 
134 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
135  KDELIBS4SUPPORT_DEPRECATED virtual QString calendarType() const = 0;
136 #endif
137 
145  virtual KLocale::CalendarSystem calendarSystem() const = 0;
146 
154  QString calendarLabel() const;
155 
171  virtual QDate epoch() const = 0;
172 
183  virtual QDate earliestValidDate() const = 0;
184 
193  virtual QDate latestValidDate() const = 0;
194 
203  bool isValid(int year, int month, int day) const;
204 
214  bool isValid(int year, int dayOfYear) const;
215 
227  bool isValid(const QString &eraName, int yearInEra, int month, int day) const;
228 
239  bool isValidIsoWeekDate(int year, int isoWeekNumber, int dayOfIsoWeek) const;
240 
247  inline bool isValid(const QDate &date) const
248  {
249  return date.isValid() && date >= earliestValidDate() && date <= latestValidDate();
250  }
251 
264  virtual bool setDate(QDate &date, int year, int month, int day) const;
265 
276  bool setDate(QDate &date, int year, int dayOfYear) const;
277 
290  bool setDate(QDate &date, QString eraName, int yearInEra, int month, int day) const;
291 
303  bool setDateIsoWeek(QDate &date, int year, int isoWeekNumber, int dayOfIsoWeek) const;
304 
321 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
322  KDELIBS4SUPPORT_DEPRECATED virtual bool setYMD(QDate &date, int y, int m, int d) const
323  {
324  return setDate(date, y, m, d);
325  }
326 #endif
327 
338  void getDate(const QDate date, int *year, int *month, int *day) const;
339 
346  virtual int year(const QDate &date) const;
347 
354  virtual int month(const QDate &date) const;
355 
362  virtual int day(const QDate &date) const;
363 
374  QString eraName(const QDate &date, StringFormat format = ShortFormat) const;
375 
386  QString eraYear(const QDate &date, StringFormat format = ShortFormat) const;
387 
397  int yearInEra(const QDate &date) const;
398 
406  virtual QDate addYears(const QDate &date, int nyears) const;
407 
415  virtual QDate addMonths(const QDate &date, int nmonths) const;
416 
424  inline QDate addDays(const QDate &date, int ndays) const
425  {
426  QDate d = date.addDays(ndays);
427  return isValid(d) ? d : QDate();
428  }
429 
448  void dateDifference(const QDate &fromDate, const QDate &toDate,
449  int *yearsDiff, int *monthsDiff, int *daysDiff, int *direction) const;
450 
461  int yearsDifference(const QDate &fromDate, const QDate &toDate) const;
462 
475  int monthsDifference(const QDate &fromDate, const QDate &toDate) const;
476 
485  inline int daysDifference(const QDate &fromDate, const QDate &toDate) const
486  {
487  return isValid(fromDate) && isValid(toDate) ? toDate.toJulianDay() - fromDate.toJulianDay() : 0;
488  }
489 
496  int monthsInYear(const QDate &date) const;
497 
506  int monthsInYear(int year) const;
507 
519  inline int weeksInYear(const QDate &date, KLocale::WeekNumberSystem weekNumberSystem = KLocale::DefaultWeekNumber) const
520  {
521  return isValid(date) ? weeksInYear(year(date), weekNumberSystem) : -1;
522  }
523 
535  int weeksInYear(int year, KLocale::WeekNumberSystem weekNumberSystem = KLocale::DefaultWeekNumber) const;
536 
543  int daysInYear(const QDate &date) const;
544 
553  int daysInYear(int year) const;
554 
561  int daysInMonth(const QDate &date) const;
562 
572  int daysInMonth(int year, int month) const;
573 
583  int daysInWeek(const QDate &date) const;
584 
593  virtual int dayOfYear(const QDate &date) const;
594 
605  int dayOfWeek(const QDate &date) const;
606 
622 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
623  KDELIBS4SUPPORT_DEPRECATED inline int weekNumber(const QDate &date, int *yearNum = nullptr) const
624  {
625  return week(date, KLocale::IsoWeekNumber, yearNum);
626  }
627 #endif
628 
646  inline int week(const QDate &date, int *yearNum) const
647  {
648  return week(date, KLocale::DefaultWeekNumber, yearNum);
649  }
650 
671  int week(const QDate &date, KLocale::WeekNumberSystem weekNumberSystem = KLocale::DefaultWeekNumber, int *yearNum = nullptr) const;
672 
682  bool isLeapYear(int year) const;
683 
693  bool isLeapYear(const QDate &date) const;
694 
703  QDate firstDayOfYear(int year) const;
704 
713  QDate lastDayOfYear(int year) const;
714 
723  QDate firstDayOfYear(const QDate &date = QDate::currentDate()) const;
724 
733  QDate lastDayOfYear(const QDate &date = QDate::currentDate()) const;
734 
744  QDate firstDayOfMonth(int year, int month) const;
745 
755  QDate lastDayOfMonth(int year, int month) const;
756 
765  QDate firstDayOfMonth(const QDate &date = QDate::currentDate()) const;
766 
775  QDate lastDayOfMonth(const QDate &date = QDate::currentDate()) const;
776 
786  virtual QString monthName(int month, int year, MonthNameFormat format = LongName) const = 0;
787 
795  virtual QString monthName(const QDate &date, MonthNameFormat format = LongName) const;
796 
805  virtual QString weekDayName(int weekDay, WeekDayNameFormat format = LongDayName) const = 0;
806 
814  virtual QString weekDayName(const QDate &date, WeekDayNameFormat format = LongDayName) const;
815 
826 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
827  KDELIBS4SUPPORT_DEPRECATED virtual QString yearString(const QDate &date, StringFormat format = LongFormat) const
828  {
829  if (format == ShortFormat) {
830  return formatDate(date, KLocale::Year, KLocale::ShortNumber);
831  } else {
832  return formatDate(date, KLocale::Year, KLocale::LongNumber);
833  }
834  }
835 #endif
836 
847 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
848  KDELIBS4SUPPORT_DEPRECATED virtual QString monthString(const QDate &date, StringFormat format = LongFormat) const
849  {
850  if (format == ShortFormat) {
851  return formatDate(date, KLocale::Month, KLocale::ShortNumber);
852  } else {
853  return formatDate(date, KLocale::Month, KLocale::LongNumber);
854  }
855  }
856 #endif
857 
868 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
869  KDELIBS4SUPPORT_DEPRECATED virtual QString dayString(const QDate &date, StringFormat format = LongFormat) const
870  {
871  if (format == ShortFormat) {
872  return formatDate(date, KLocale::Day, KLocale::ShortNumber);
873  } else {
874  return formatDate(date, KLocale::Day, KLocale::LongNumber);
875  }
876  }
877 #endif
878 
890 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
891  KDELIBS4SUPPORT_DEPRECATED QString yearInEraString(const QDate &date, StringFormat format = ShortFormat) const
892  {
893  if (format == ShortFormat) {
894  return formatDate(date, KLocale::YearInEra, KLocale::ShortNumber);
895  } else {
896  return formatDate(date, KLocale::YearInEra, KLocale::LongNumber);
897  }
898  }
899 #endif
900 
913 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
914  KDELIBS4SUPPORT_DEPRECATED QString dayOfYearString(const QDate &date, StringFormat format = LongFormat) const
915  {
916  if (format == ShortFormat) {
917  return formatDate(date, KLocale::DayOfYear, KLocale::ShortNumber);
918  } else {
919  return formatDate(date, KLocale::DayOfYear, KLocale::LongNumber);
920  }
921  }
922 #endif
923 
935 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
936  KDELIBS4SUPPORT_DEPRECATED QString dayOfWeekString(const QDate &date) const
937  {
938  return formatDate(date, KLocale::DayOfWeek, KLocale::ShortNumber);
939  }
940 #endif
941 
954 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
955  KDELIBS4SUPPORT_DEPRECATED QString weekNumberString(const QDate &date, StringFormat format = LongFormat) const
956  {
957  if (format == ShortFormat) {
958  return formatDate(date, KLocale::Week, KLocale::ShortNumber);
959  } else {
960  return formatDate(date, KLocale::Week, KLocale::LongNumber);
961  }
962  }
963 #endif
964 
977 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
978  KDELIBS4SUPPORT_DEPRECATED QString monthsInYearString(const QDate &date, StringFormat format = LongFormat) const
979  {
980  if (format == ShortFormat) {
981  return formatDate(date, KLocale::MonthsInYear, KLocale::ShortNumber);
982  } else {
983  return formatDate(date, KLocale::MonthsInYear, KLocale::LongNumber);
984  }
985  }
986 #endif
987 
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 
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 
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 
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 
1084  virtual int yearStringToInteger(const QString &sNum, int &iLength) const;
1085 
1095  virtual int monthStringToInteger(const QString &sNum, int &iLength) const;
1096 
1106  virtual int dayStringToInteger(const QString &sNum, int &iLength) const;
1107 
1124  virtual QString formatDate(const QDate &fromDate, KLocale::DateFormat toFormat = KLocale::LongDate) const;
1125 
1242  QString formatDate(const QDate &fromDate, const QString &toFormat,
1243  KLocale::DateTimeFormatStandard formatStandard = KLocale::KdeFormat) const;
1244 
1261  QString formatDate(const QDate &fromDate, const QString &toFormat, KLocale::DigitSet digitSet,
1262  KLocale::DateTimeFormatStandard formatStandard = KLocale::KdeFormat) const;
1263 
1282  QString formatDate(const QDate &date, KLocale::DateTimeComponent component,
1284  KLocale::WeekNumberSystem weekNumberSystem = KLocale::DefaultWeekNumber) const;
1285 
1302  virtual QDate readDate(const QString &str, bool *ok = nullptr) const;
1303 
1322  virtual QDate readDate(const QString &str, KLocale::ReadDateFlags flags, bool *ok = nullptr) const;
1323 
1337  virtual QDate readDate(const QString &dateString, const QString &dateFormat, bool *ok = nullptr) const;
1338 
1398  QDate readDate(const QString &dateString, const QString &dateFormat, bool *ok,
1399  KLocale::DateTimeFormatStandard formatStandard) const;
1400 
1430  int shortYearWindowStartYear() const;
1431 
1448  int applyShortYearWindow(int inputYear) const;
1449 
1462  inline int weekStartDay() const
1463  {
1464  return locale()->weekStartDay();
1465  }
1466 
1478  KDELIBS4SUPPORT_DEPRECATED inline int weekDayOfPray() const
1479  {
1480  return locale()->weekDayOfPray();
1481  }
1482 
1488  virtual bool isLunar() const = 0;
1489 
1495  virtual bool isLunisolar() const = 0;
1496 
1502  virtual bool isSolar() const = 0;
1503 
1512  virtual bool isProleptic() const = 0;
1513 
1514 protected:
1531  virtual bool julianDayToDate(qint64 jd, int &year, int &month, int &day) const = 0;
1532 
1549  virtual bool dateToJulianDay(int year, int month, int day, qint64 &jd) const = 0;
1550 
1573  const KLocale *locale() const;
1574 
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
The Year In Era portion of a date.
Definition: klocale.h:738
The Day Of Week / Weekday portion of a date, may be number or name.
Definition: klocale.h:741
The Days In Year portion of a date.
Definition: klocale.h:747
Long name possessive format, e.g.
The Day portion of a date, may be number or name.
Definition: klocale.h:733
Short string format, e.g.
virtual QString yearString(const QDate &date, StringFormat format=LongFormat) const
The Days In Week portion of a date.
Definition: klocale.h:749
int weekStartDay() const
Use this to determine which day is the first day of the week.
QString weekNumberString(const QDate &date, StringFormat format=LongFormat) const
DateFormat
Format for date string.
Definition: klocale.h:783
QString daysInMonthString(const QDate &date, StringFormat format=LongFormat) const
The Months In Year portion of a date.
Definition: klocale.h:745
QString daysInYearString(const QDate &date, StringFormat format=LongFormat) const
The system locale default for the componant.
Definition: klocale.h:771
The Year portion of a date, may be number or name.
Definition: klocale.h:729
MonthNameFormat
Format for returned month / day name.
StringFormat
Format for returned year number / month number / day number as string.
Number at its natural width, e.g.
Definition: klocale.h:772
KCalendarSystem abstract base class, provides support for local Calendar Systems in KDE...
DateTimeComponentFormat
Definition: klocale.h:770
int weeksInYear(const QDate &date, KLocale::WeekNumberSystem weekNumberSystem=KLocale::DefaultWeekNumber) const
int weekDayOfPray() const
bool isValid(const QDate &date) const
Returns whether a given date is valid in this calendar system.
The Weeks In Year portion of a date.
Definition: klocale.h:746
CalendarSystem
Definition: klocale.h:646
Number padded to a required width, e.g.
Definition: klocale.h:773
QString monthsInYearString(const QDate &date, StringFormat format=LongFormat) const
ISO Week Number.
Definition: klocale.h:683
qint64 toJulianDay() const const
A class representing a date localized using the local calendar system, language and formats...
The Day Of Year portion of a date, may be number or name.
Definition: klocale.h:739
QString yearInEraString(const QDate &date, StringFormat format=ShortFormat) const
WeekNumberSystem
Definition: klocale.h:681
bool isValid() const const
QString dayOfWeekString(const QDate &date) const
int week(const QDate &date, int *yearNum) const
Returns the localized Week Number for the date.
ReadDateFlags
Flags for readDate()
Definition: klocale.h:1108
WeekDayNameFormat
Format for returned month / day name.
Short name possessive format, e.g.
DateTimeComponent
Definition: klocale.h:728
KDE Standard.
Definition: klocale.h:695
static KLocale * global()
Return the global KLocale instance.
Definition: klocale.cpp:309
virtual QString monthString(const QDate &date, StringFormat format=LongFormat) const
The system locale default.
Definition: klocale.h:682
Short name format, e.g.
KLocale provides support for language and country specific stuff.
Definition: klocale.h:75
DateTimeFormatStandard
Definition: klocale.h:694
QString weeksInYearString(const QDate &date, StringFormat format=LongFormat) const
QString dayOfYearString(const QDate &date, StringFormat format=LongFormat) const
QDate currentDate()
virtual bool setYMD(QDate &date, int y, int m, int d) const
Short name format, e.g.
Locale Long date format, e.g.
Definition: klocale.h:785
The Days In Month portion of a date.
Definition: klocale.h:748
The Month portion of a date, may be number or name.
Definition: klocale.h:731
QDate addDays(qint64 ndays) const const
DigitSet
Definition: klocale.h:176
QDate addDays(const QDate &date, int ndays) const
Returns a QDate containing a date ndays days later.
int weekNumber(const QDate &date, int *yearNum=nullptr) const
int daysDifference(const QDate &fromDate, const QDate &toDate) const
Returns the difference between two dates in days The returned value will be negative if fromDate > to...
The Week Number portion of a date.
Definition: klocale.h:743
Long name format, e.g.
virtual QString dayString(const QDate &date, StringFormat format=LongFormat) const
Long name format, e.g.
QString daysInWeekString(const QDate &date) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Dec 3 2020 22:59:35 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.