KDELibs4Support

kcalendarsystemgregorian.cpp
1 /*
2  Copyright 2009, 2010 John Layt <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 // Derived gregorian kde calendar class
21 
22 #include "kcalendarsystemgregorian_p.h"
23 #include "kcalendarsystemgregorianprivate_p.h"
24 #include "kcalendarera_p.h"
25 
26 #include "klocale.h"
27 #include "klocalizedstring.h"
28 #include "kconfiggroup.h"
29 
30 #include <QDate>
31 #include <QCharRef>
32 
33 // Shared d pointer base class definitions
34 
35 KCalendarSystemGregorianPrivate::KCalendarSystemGregorianPrivate(KCalendarSystemGregorian *q)
36  : KCalendarSystemPrivate(q)
37 {
38 }
39 
40 KCalendarSystemGregorianPrivate::~KCalendarSystemGregorianPrivate()
41 {
42 }
43 
44 void KCalendarSystemGregorianPrivate::loadDefaultEraList()
45 {
46  QString name, shortName, format;
47 
48  KConfigGroup lcg(config(), QString::fromLatin1("Locale"));
49  KConfigGroup cg = lcg.group(QString::fromLatin1("KCalendarSystem %1").arg(q->calendarType()));
50  m_useCommonEra = cg.readEntry("UseCommonEra", false);
51 
52  if (m_useCommonEra) {
53  name = i18nc("Calendar Era: Gregorian Common Era, years < 0, LongFormat", "Before Common Era");
54  shortName = i18nc("Calendar Era: Gregorian Common Era, years < 0, ShortFormat", "BCE");
55  } else {
56  name = i18nc("Calendar Era: Gregorian Christian Era, years < 0, LongFormat", "Before Christ");
57  shortName = i18nc("Calendar Era: Gregorian Christian Era, years < 0, ShortFormat", "BC");
58  }
59  format = i18nc("(kdedt-format) Gregorian, BC, full era year format used for %EY, e.g. 2000 BC", "%Ey %EC");
60  addEra('-', 1, q->epoch().addDays(-1), -1, q->earliestValidDate(), name, shortName, format);
61 
62  if (m_useCommonEra) {
63  name = i18nc("Calendar Era: Gregorian Common Era, years > 0, LongFormat", "Common Era");
64  shortName = i18nc("Calendar Era: Gregorian Common Era, years > 0, ShortFormat", "CE");
65  } else {
66  name = i18nc("Calendar Era: Gregorian Christian Era, years > 0, LongFormat", "Anno Domini");
67  shortName = i18nc("Calendar Era: Gregorian Christian Era, years > 0, ShortFormat", "AD");
68  }
69  format = i18nc("(kdedt-format) Gregorian, AD, full era year format used for %EY, e.g. 2000 AD", "%Ey %EC");
70  addEra('+', 1, q->epoch(), 1, q->latestValidDate(), name, shortName, format);
71 }
72 
73 int KCalendarSystemGregorianPrivate::monthsInYear(int year) const
74 {
75  Q_UNUSED(year)
76  return 12;
77 }
78 
79 int KCalendarSystemGregorianPrivate::daysInMonth(int year, int month) const
80 {
81  if (hasYearZero() && year <= 0) {
82  --year;
83  }
84  return QDate(year, month, 1).daysInMonth();
85 }
86 
87 int KCalendarSystemGregorianPrivate::daysInYear(int year) const
88 {
89  if (isLeapYear(year)) {
90  return 366;
91  } else {
92  return 365;
93  }
94 }
95 
96 bool KCalendarSystemGregorianPrivate::isLeapYear(int year) const
97 {
98  if (hasYearZero() && year <= 0) {
99  --year;
100  }
101  return QDate::isLeapYear(year);
102 }
103 
104 bool KCalendarSystemGregorianPrivate::hasLeapMonths() const
105 {
106  return false;
107 }
108 
109 bool KCalendarSystemGregorianPrivate::hasYearZero() const
110 {
111  return false;
112 }
113 
114 int KCalendarSystemGregorianPrivate::maxMonthsInYear() const
115 {
116  return 12;
117 }
118 
119 int KCalendarSystemGregorianPrivate::earliestValidYear() const
120 {
121  return -9999;
122 }
123 
124 int KCalendarSystemGregorianPrivate::latestValidYear() const
125 {
126  return 9999;
127 }
128 
129 QString KCalendarSystemGregorianPrivate::monthName(int month, int year, KLocale::DateTimeComponentFormat format, bool possessive) const
130 {
131  Q_UNUSED(year);
132 
133  QStringList languages = locale()->languageList();
134 
135  if (format == KLocale::NarrowName) {
136  switch (month) {
137  case 1:
138  return ki18nc("Gregorian month 1 - KLocale::NarrowName", "J").toString(languages);
139  case 2:
140  return ki18nc("Gregorian month 2 - KLocale::NarrowName", "F").toString(languages);
141  case 3:
142  return ki18nc("Gregorian month 3 - KLocale::NarrowName", "M").toString(languages);
143  case 4:
144  return ki18nc("Gregorian month 4 - KLocale::NarrowName", "A").toString(languages);
145  case 5:
146  return ki18nc("Gregorian month 5 - KLocale::NarrowName", "M").toString(languages);
147  case 6:
148  return ki18nc("Gregorian month 6 - KLocale::NarrowName", "J").toString(languages);
149  case 7:
150  return ki18nc("Gregorian month 7 - KLocale::NarrowName", "J").toString(languages);
151  case 8:
152  return ki18nc("Gregorian month 8 - KLocale::NarrowName", "A").toString(languages);
153  case 9:
154  return ki18nc("Gregorian month 9 - KLocale::NarrowName", "S").toString(languages);
155  case 10:
156  return ki18nc("Gregorian month 10 - KLocale::NarrowName", "O").toString(languages);
157  case 11:
158  return ki18nc("Gregorian month 11 - KLocale::NarrowName", "N").toString(languages);
159  case 12:
160  return ki18nc("Gregorian month 12 - KLocale::NarrowName", "D").toString(languages);
161  default:
162  return QString();
163  }
164  }
165 
166  if (format == KLocale::ShortName && possessive) {
167  switch (month) {
168  case 1:
169  return ki18nc("Gregorian month 1 - KLocale::ShortName Possessive", "of Jan").toString(languages);
170  case 2:
171  return ki18nc("Gregorian month 2 - KLocale::ShortName Possessive", "of Feb").toString(languages);
172  case 3:
173  return ki18nc("Gregorian month 3 - KLocale::ShortName Possessive", "of Mar").toString(languages);
174  case 4:
175  return ki18nc("Gregorian month 4 - KLocale::ShortName Possessive", "of Apr").toString(languages);
176  case 5:
177  return ki18nc("Gregorian month 5 - KLocale::ShortName Possessive", "of May").toString(languages);
178  case 6:
179  return ki18nc("Gregorian month 6 - KLocale::ShortName Possessive", "of Jun").toString(languages);
180  case 7:
181  return ki18nc("Gregorian month 7 - KLocale::ShortName Possessive", "of Jul").toString(languages);
182  case 8:
183  return ki18nc("Gregorian month 8 - KLocale::ShortName Possessive", "of Aug").toString(languages);
184  case 9:
185  return ki18nc("Gregorian month 9 - KLocale::ShortName Possessive", "of Sep").toString(languages);
186  case 10:
187  return ki18nc("Gregorian month 10 - KLocale::ShortName Possessive", "of Oct").toString(languages);
188  case 11:
189  return ki18nc("Gregorian month 11 - KLocale::ShortName Possessive", "of Nov").toString(languages);
190  case 12:
191  return ki18nc("Gregorian month 12 - KLocale::ShortName Possessive", "of Dec").toString(languages);
192  default:
193  return QString();
194  }
195  }
196 
197  if (format == KLocale::ShortName && !possessive) {
198  switch (month) {
199  case 1:
200  return ki18nc("Gregorian month 1 - KLocale::ShortName", "Jan").toString(languages);
201  case 2:
202  return ki18nc("Gregorian month 2 - KLocale::ShortName", "Feb").toString(languages);
203  case 3:
204  return ki18nc("Gregorian month 3 - KLocale::ShortName", "Mar").toString(languages);
205  case 4:
206  return ki18nc("Gregorian month 4 - KLocale::ShortName", "Apr").toString(languages);
207  case 5:
208  return ki18nc("Gregorian month 5 - KLocale::ShortName", "May").toString(languages);
209  case 6:
210  return ki18nc("Gregorian month 6 - KLocale::ShortName", "Jun").toString(languages);
211  case 7:
212  return ki18nc("Gregorian month 7 - KLocale::ShortName", "Jul").toString(languages);
213  case 8:
214  return ki18nc("Gregorian month 8 - KLocale::ShortName", "Aug").toString(languages);
215  case 9:
216  return ki18nc("Gregorian month 9 - KLocale::ShortName", "Sep").toString(languages);
217  case 10:
218  return ki18nc("Gregorian month 10 - KLocale::ShortName", "Oct").toString(languages);
219  case 11:
220  return ki18nc("Gregorian month 11 - KLocale::ShortName", "Nov").toString(languages);
221  case 12:
222  return ki18nc("Gregorian month 12 - KLocale::ShortName", "Dec").toString(languages);
223  default:
224  return QString();
225  }
226  }
227 
228  if (format == KLocale::LongName && possessive) {
229  switch (month) {
230  case 1:
231  return ki18nc("Gregorian month 1 - KLocale::LongName Possessive", "of January").toString(languages);
232  case 2:
233  return ki18nc("Gregorian month 2 - KLocale::LongName Possessive", "of February").toString(languages);
234  case 3:
235  return ki18nc("Gregorian month 3 - KLocale::LongName Possessive", "of March").toString(languages);
236  case 4:
237  return ki18nc("Gregorian month 4 - KLocale::LongName Possessive", "of April").toString(languages);
238  case 5:
239  return ki18nc("Gregorian month 5 - KLocale::LongName Possessive", "of May").toString(languages);
240  case 6:
241  return ki18nc("Gregorian month 6 - KLocale::LongName Possessive", "of June").toString(languages);
242  case 7:
243  return ki18nc("Gregorian month 7 - KLocale::LongName Possessive", "of July").toString(languages);
244  case 8:
245  return ki18nc("Gregorian month 8 - KLocale::LongName Possessive", "of August").toString(languages);
246  case 9:
247  return ki18nc("Gregorian month 9 - KLocale::LongName Possessive", "of September").toString(languages);
248  case 10:
249  return ki18nc("Gregorian month 10 - KLocale::LongName Possessive", "of October").toString(languages);
250  case 11:
251  return ki18nc("Gregorian month 11 - KLocale::LongName Possessive", "of November").toString(languages);
252  case 12:
253  return ki18nc("Gregorian month 12 - KLocale::LongName Possessive", "of December").toString(languages);
254  default:
255  return QString();
256  }
257  }
258 
259  // Default to LongName
260  switch (month) {
261  case 1:
262  return ki18nc("Gregorian month 1 - KLocale::LongName", "January").toString(languages);
263  case 2:
264  return ki18nc("Gregorian month 2 - KLocale::LongName", "February").toString(languages);
265  case 3:
266  return ki18nc("Gregorian month 3 - KLocale::LongName", "March").toString(languages);
267  case 4:
268  return ki18nc("Gregorian month 4 - KLocale::LongName", "April").toString(languages);
269  case 5:
270  return ki18nc("Gregorian month 5 - KLocale::LongName", "May").toString(languages);
271  case 6:
272  return ki18nc("Gregorian month 6 - KLocale::LongName", "June").toString(languages);
273  case 7:
274  return ki18nc("Gregorian month 7 - KLocale::LongName", "July").toString(languages);
275  case 8:
276  return ki18nc("Gregorian month 8 - KLocale::LongName", "August").toString(languages);
277  case 9:
278  return ki18nc("Gregorian month 9 - KLocale::LongName", "September").toString(languages);
279  case 10:
280  return ki18nc("Gregorian month 10 - KLocale::LongName", "October").toString(languages);
281  case 11:
282  return ki18nc("Gregorian month 11 - KLocale::LongName", "November").toString(languages);
283  case 12:
284  return ki18nc("Gregorian month 12 - KLocale::LongName", "December").toString(languages);
285  default:
286  return QString();
287  }
288 }
289 
290 QString KCalendarSystemGregorianPrivate::weekDayName(int weekDay, KLocale::DateTimeComponentFormat format) const
291 {
292  QStringList languages = locale()->languageList();
293 
294  if (format == KLocale::NarrowName) {
295  switch (weekDay) {
296  case 1:
297  return ki18nc("Gregorian weekday 1 - KLocale::NarrowName ", "M").toString(languages);
298  case 2:
299  return ki18nc("Gregorian weekday 2 - KLocale::NarrowName ", "T").toString(languages);
300  case 3:
301  return ki18nc("Gregorian weekday 3 - KLocale::NarrowName ", "W").toString(languages);
302  case 4:
303  return ki18nc("Gregorian weekday 4 - KLocale::NarrowName ", "T").toString(languages);
304  case 5:
305  return ki18nc("Gregorian weekday 5 - KLocale::NarrowName ", "F").toString(languages);
306  case 6:
307  return ki18nc("Gregorian weekday 6 - KLocale::NarrowName ", "S").toString(languages);
308  case 7:
309  return ki18nc("Gregorian weekday 7 - KLocale::NarrowName ", "S").toString(languages);
310  default:
311  return QString();
312  }
313  }
314 
315  if (format == KLocale::ShortName || format == KLocale:: ShortNumber) {
316  switch (weekDay) {
317  case 1:
318  return ki18nc("Gregorian weekday 1 - KLocale::ShortName", "Mon").toString(languages);
319  case 2:
320  return ki18nc("Gregorian weekday 2 - KLocale::ShortName", "Tue").toString(languages);
321  case 3:
322  return ki18nc("Gregorian weekday 3 - KLocale::ShortName", "Wed").toString(languages);
323  case 4:
324  return ki18nc("Gregorian weekday 4 - KLocale::ShortName", "Thu").toString(languages);
325  case 5:
326  return ki18nc("Gregorian weekday 5 - KLocale::ShortName", "Fri").toString(languages);
327  case 6:
328  return ki18nc("Gregorian weekday 6 - KLocale::ShortName", "Sat").toString(languages);
329  case 7:
330  return ki18nc("Gregorian weekday 7 - KLocale::ShortName", "Sun").toString(languages);
331  default: return QString();
332  }
333  }
334 
335  switch (weekDay) {
336  case 1:
337  return ki18nc("Gregorian weekday 1 - KLocale::LongName", "Monday").toString(languages);
338  case 2:
339  return ki18nc("Gregorian weekday 2 - KLocale::LongName", "Tuesday").toString(languages);
340  case 3:
341  return ki18nc("Gregorian weekday 3 - KLocale::LongName", "Wednesday").toString(languages);
342  case 4:
343  return ki18nc("Gregorian weekday 4 - KLocale::LongName", "Thursday").toString(languages);
344  case 5:
345  return ki18nc("Gregorian weekday 5 - KLocale::LongName", "Friday").toString(languages);
346  case 6:
347  return ki18nc("Gregorian weekday 6 - KLocale::LongName", "Saturday").toString(languages);
348  case 7:
349  return ki18nc("Gregorian weekday 7 - KLocale::LongName", "Sunday").toString(languages);
350  default:
351  return QString();
352  }
353 }
354 
355 KCalendarSystemGregorian::KCalendarSystemGregorian(const KSharedConfig::Ptr config,
356  const KLocale *locale)
357  : KCalendarSystem(*new KCalendarSystemGregorianPrivate(this), config, locale)
358 {
359  d_ptr->loadConfig(calendarType());
360 }
361 
362 KCalendarSystemGregorian::KCalendarSystemGregorian(KCalendarSystemGregorianPrivate &dd,
363  const KSharedConfig::Ptr config,
364  const KLocale *locale)
365  : KCalendarSystem(dd, config, locale)
366 {
367  d_ptr->loadConfig(calendarType());
368 }
369 
370 KCalendarSystemGregorian::~KCalendarSystemGregorian()
371 {
372 }
373 
374 QString KCalendarSystemGregorian::calendarType() const
375 {
376  return QLatin1String("gregorian");
377 }
378 
379 KLocale::CalendarSystem KCalendarSystemGregorian::calendarSystem() const
380 {
382 }
383 
384 QDate KCalendarSystemGregorian::epoch() const
385 {
386  return QDate::fromJulianDay(1721426);
387 }
388 
389 QDate KCalendarSystemGregorian::earliestValidDate() const
390 {
391  Q_D(const KCalendarSystemGregorian);
392 
393  // Set to first day of year 9999 until confirm date formats & widgets support > 9999
394  return QDate(d->hasYearZero() ? -10000 : -9999, 1, 1);
395  // return QDate(std::num_limits<int>::min(), 1, 1);
396 }
397 
398 QDate KCalendarSystemGregorian::latestValidDate() const
399 {
400  // Set to last day of year 9999 until confirm date formats & widgets support > 9999
401  // In Gregorian this is 9999-12-31, which is is jd 5373484
402  return QDate(9999, 12, 31);
403  // return QDate(std::num_limits<int>::max(), 12, 31);
404 }
405 
406 QString KCalendarSystemGregorian::monthName(int month, int year, MonthNameFormat format) const
407 {
408  return KCalendarSystem::monthName(month, year, format);
409 }
410 
411 QString KCalendarSystemGregorian::monthName(const QDate &date, MonthNameFormat format) const
412 {
413  return KCalendarSystem::monthName(date, format);
414 }
415 
416 QString KCalendarSystemGregorian::weekDayName(int weekDay, WeekDayNameFormat format) const
417 {
418  return KCalendarSystem::weekDayName(weekDay, format);
419 }
420 
421 QString KCalendarSystemGregorian::weekDayName(const QDate &date, WeekDayNameFormat format) const
422 {
423  return KCalendarSystem::weekDayName(date, format);
424 }
425 
426 int KCalendarSystemGregorian::yearStringToInteger(const QString &sNum, int &iLength) const
427 {
428  return KCalendarSystem::yearStringToInteger(sNum, iLength);
429 }
430 
431 bool KCalendarSystemGregorian::isLunar() const
432 {
433  return false;
434 }
435 
436 bool KCalendarSystemGregorian::isLunisolar() const
437 {
438  return false;
439 }
440 
441 bool KCalendarSystemGregorian::isSolar() const
442 {
443  return true;
444 }
445 
446 bool KCalendarSystemGregorian::isProleptic() const
447 {
448  return true;
449 }
450 
451 bool KCalendarSystemGregorian::julianDayToDate(qint64 jd, int &year, int &month, int &day) const
452 {
453  Q_D(const KCalendarSystemGregorian);
454 
455  QDate date = QDate::fromJulianDay(jd);
456  date.getDate(&year, &month, &day);
457 
458  if (d->hasYearZero() && year < 0) {
459  ++year;
460  }
461 
462  return date.isValid();
463 }
464 
465 bool KCalendarSystemGregorian::dateToJulianDay(int year, int month, int day, qint64 &jd) const
466 {
467  Q_D(const KCalendarSystemGregorian);
468 
469  if (d->hasYearZero() && year <= 0) {
470  --year;
471  }
472 
473  QDate date;
474  date.setDate(year, month, day);
475 
476  jd = date.toJulianDay();
477  return date.isValid();
478 }
Narrow text format, may not be unique, e.g.
Definition: klocale.h:775
QString toString() const
QString name(const QVariant &location)
virtual int day(const QDate &date) const
Returns the day portion of a given date in the current calendar system.
virtual QString weekDayName(int weekDay, WeekDayNameFormat format=LongDayName) const =0
Gets specific calendar type week day name.
KConfigGroup group(const QByteArray &group)
MonthNameFormat
Format for returned month / day name.
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
KSharedConfigPtr config()
Returns the general config object.
Definition: kglobal.cpp:104
CalendarSystem
Definition: klocale.h:646
bool isLeapYear(int year) const
Returns whether a given year is a leap year.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
qint64 toJulianDay() const const
Long text format, e.g.
Definition: klocale.h:777
bool isValid() const const
virtual int month(const QDate &date) const
Returns the month portion of a given date in the current calendar system.
int daysInMonth() const const
const KLocale * locale() const
Returns the locale used for translations and formats for this calendar system instance.
WeekDayNameFormat
Format for returned month / day name.
KDE Default, proleptic Gregorian Calendar as used by QDate.
Definition: klocale.h:647
virtual int yearStringToInteger(const QString &sNum, int &iLength) const
Short text format, e.g.
Definition: klocale.h:776
virtual int year(const QDate &date) const
Returns the year portion of a given date in the current calendar system.
bool isLeapYear(int year)
bool setDate(int year, int month, int day)
KLocalizedString KI18N_EXPORT ki18nc(const char *context, const char *text)
QStringList languageList() const
Returns the language codes selected by user, ordered by decreasing priority.
Definition: klocale.cpp:376
virtual QString monthName(int month, int year, MonthNameFormat format=LongName) const =0
Gets specific calendar type month name for a given month number If an invalid month is specified...
KLocale provides support for language and country specific stuff.
Definition: klocale.h:75
QString fromLatin1(const char *str, int size)
void getDate(int *year, int *month, int *day) const const
QDate fromJulianDay(qint64 jd)
virtual QString calendarType() const =0
T readEntry(const QString &key, const T &aDefault) const
int daysInMonth(const QDate &date) const
Returns the number of days in the given month.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jul 13 2020 22:58:30 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.