KDELibs4Support

kcalendarsystemindiannational.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 #include "kcalendarsystemindiannational_p.h"
21 #include "kcalendarsystemprivate_p.h"
22 
23 #include "klocale.h"
24 #include "klocalizedstring.h"
25 
26 #include <QDate>
27 #include <QCharRef>
28 
29 class KCalendarSystemIndianNationalPrivate : public KCalendarSystemPrivate
30 {
31 public:
32  KDELIBS4SUPPORT_DEPRECATED explicit KCalendarSystemIndianNationalPrivate(KCalendarSystemIndianNational *q);
33 
34  virtual ~KCalendarSystemIndianNationalPrivate();
35 
36  // Virtual methods each calendar system must re-implement
37  void loadDefaultEraList() override;
38  int monthsInYear(int year) const override;
39  int daysInMonth(int year, int month) const override;
40  int daysInYear(int year) const override;
41  bool isLeapYear(int year) const override;
42  bool hasLeapMonths() const override;
43  bool hasYearZero() const override;
44  int maxMonthsInYear() const override;
45  int earliestValidYear() const override;
46  int latestValidYear() const override;
47  QString monthName(int month, int year, KLocale::DateTimeComponentFormat format, bool possessive) const override;
48  QString weekDayName(int weekDay, KLocale::DateTimeComponentFormat format) const override;
49 };
50 
51 // Shared d pointer base class definitions
52 
53 KCalendarSystemIndianNationalPrivate::KCalendarSystemIndianNationalPrivate(KCalendarSystemIndianNational *q)
54  : KCalendarSystemPrivate(q)
55 {
56 }
57 
58 KCalendarSystemIndianNationalPrivate::~KCalendarSystemIndianNationalPrivate()
59 {
60 }
61 
62 void KCalendarSystemIndianNationalPrivate::loadDefaultEraList()
63 {
64  QString name, shortName, format;
65  // Saka Era
66  name = i18nc("Calendar Era: Indian National Saka Era, years > 0, LongFormat", "Saka Era");
67  shortName = i18nc("Calendar Era: Indian National Saka Era, years > 0, ShortFormat", "SE");
68  format = i18nc("(kdedt-format) Indian National, SE, full era year format used for %EY, e.g. 2000 SE", "%Ey %EC");
69  addEra('+', 1, q->epoch(), 1, q->latestValidDate(), name, shortName, format);
70 }
71 
72 int KCalendarSystemIndianNationalPrivate::monthsInYear(int year) const
73 {
74  Q_UNUSED(year)
75  return 12;
76 }
77 
78 int KCalendarSystemIndianNationalPrivate::daysInMonth(int year, int month) const
79 {
80  if (month == 1) {
81  if (isLeapYear(year)) {
82  return 31;
83  } else {
84  return 30;
85  }
86  }
87 
88  if (month >= 2 && month <= 6) {
89  return 31;
90  }
91 
92  return 30;
93 }
94 
95 int KCalendarSystemIndianNationalPrivate::daysInYear(int year) const
96 {
97  if (isLeapYear(year)) {
98  return 366;
99  } else {
100  return 365;
101  }
102 }
103 
104 bool KCalendarSystemIndianNationalPrivate::isLeapYear(int year) const
105 {
106  //Uses same rule as Gregorian, and is explicitly synchronized to Gregorian
107  //so add 78 years to get Gregorian year and apply Gregorian calculation
108  year = year + 78;
109  if (!hasYearZero() && year < 1) {
110  year = year + 1;
111  }
112 
113  if (year % 4 == 0) {
114  if (year % 100 != 0) {
115  return true;
116  } else if (year % 400 == 0) {
117  return true;
118  }
119  }
120 
121  return false;
122 }
123 
124 bool KCalendarSystemIndianNationalPrivate::hasLeapMonths() const
125 {
126  return false;
127 }
128 
129 bool KCalendarSystemIndianNationalPrivate::hasYearZero() const
130 {
131  return true;
132 }
133 
134 int KCalendarSystemIndianNationalPrivate::maxMonthsInYear() const
135 {
136  return 12;
137 }
138 
139 int KCalendarSystemIndianNationalPrivate::earliestValidYear() const
140 {
141  return 0;
142 }
143 
144 int KCalendarSystemIndianNationalPrivate::latestValidYear() const
145 {
146  return 9999;
147 }
148 
149 QString KCalendarSystemIndianNationalPrivate::monthName(int month, int year, KLocale::DateTimeComponentFormat format, bool possessive) const
150 {
151  Q_UNUSED(year);
152 
153  QStringList languages = locale()->languageList();
154 
155  if (format == KLocale::NarrowName) {
156  switch (month) {
157  case 1:
158  return ki18nc("Indian National month 1 - KLocale::NarrowName", "C").toString(languages);
159  case 2:
160  return ki18nc("Indian National month 2 - KLocale::NarrowName", "V").toString(languages);
161  case 3:
162  return ki18nc("Indian National month 3 - KLocale::NarrowName", "J").toString(languages);
163  case 4:
164  return ki18nc("Indian National month 4 - KLocale::NarrowName", "Ā").toString(languages);
165  case 5:
166  return ki18nc("Indian National month 5 - KLocale::NarrowName", "S").toString(languages);
167  case 6:
168  return ki18nc("Indian National month 6 - KLocale::NarrowName", "B").toString(languages);
169  case 7:
170  return ki18nc("Indian National month 7 - KLocale::NarrowName", "Ā").toString(languages);
171  case 8:
172  return ki18nc("Indian National month 8 - KLocale::NarrowName", "K").toString(languages);
173  case 9:
174  return ki18nc("Indian National month 9 - KLocale::NarrowName", "A").toString(languages);
175  case 10:
176  return ki18nc("Indian National month 10 - KLocale::NarrowName", "P").toString(languages);
177  case 11:
178  return ki18nc("Indian National month 11 - KLocale::NarrowName", "M").toString(languages);
179  case 12:
180  return ki18nc("Indian National month 12 - KLocale::NarrowName", "P").toString(languages);
181  default:
182  return QString();
183  }
184  }
185 
186  if (format == KLocale::ShortName && possessive) {
187  switch (month) {
188  case 1:
189  return ki18nc("Indian National month 1 - KLocale::ShortName Possessive", "of Cha").toString(languages);
190  case 2:
191  return ki18nc("Indian National month 2 - KLocale::ShortName Possessive", "of Vai").toString(languages);
192  case 3:
193  return ki18nc("Indian National month 3 - KLocale::ShortName Possessive", "of Jya").toString(languages);
194  case 4:
195  return ki18nc("Indian National month 4 - KLocale::ShortName Possessive", "of Āsh").toString(languages);
196  case 5:
197  return ki18nc("Indian National month 5 - KLocale::ShortName Possessive", "of Shr").toString(languages);
198  case 6:
199  return ki18nc("Indian National month 6 - KLocale::ShortName Possessive", "of Bhā").toString(languages);
200  case 7:
201  return ki18nc("Indian National month 7 - KLocale::ShortName Possessive", "of Āsw").toString(languages);
202  case 8:
203  return ki18nc("Indian National month 8 - KLocale::ShortName Possessive", "of Kār").toString(languages);
204  case 9:
205  return ki18nc("Indian National month 9 - KLocale::ShortName Possessive", "of Agr").toString(languages);
206  case 10:
207  return ki18nc("Indian National month 10 - KLocale::ShortName Possessive", "of Pau").toString(languages);
208  case 11:
209  return ki18nc("Indian National month 11 - KLocale::ShortName Possessive", "of Māg").toString(languages);
210  case 12:
211  return ki18nc("Indian National month 12 - KLocale::ShortName Possessive", "of Phā").toString(languages);
212  default:
213  return QString();
214  }
215  }
216 
217  if (format == KLocale::ShortName && !possessive) {
218  switch (month) {
219  case 1:
220  return ki18nc("Indian National month 1 - KLocale::ShortName", "Cha").toString(languages);
221  case 2:
222  return ki18nc("Indian National month 2 - KLocale::ShortName", "Vai").toString(languages);
223  case 3:
224  return ki18nc("Indian National month 3 - KLocale::ShortName", "Jya").toString(languages);
225  case 4:
226  return ki18nc("Indian National month 4 - KLocale::ShortName", "Āsh").toString(languages);
227  case 5:
228  return ki18nc("Indian National month 5 - KLocale::ShortName", "Shr").toString(languages);
229  case 6:
230  return ki18nc("Indian National month 6 - KLocale::ShortName", "Bhā").toString(languages);
231  case 7:
232  return ki18nc("Indian National month 7 - KLocale::ShortName", "Āsw").toString(languages);
233  case 8:
234  return ki18nc("Indian National month 8 - KLocale::ShortName", "Kār").toString(languages);
235  case 9:
236  return ki18nc("Indian National month 9 - KLocale::ShortName", "Agr").toString(languages);
237  case 10:
238  return ki18nc("Indian National month 10 - KLocale::ShortName", "Pau").toString(languages);
239  case 11:
240  return ki18nc("Indian National month 11 - KLocale::ShortName", "Māg").toString(languages);
241  case 12:
242  return ki18nc("Indian National month 12 - KLocale::ShortName", "Phā").toString(languages);
243  default:
244  return QString();
245  }
246  }
247 
248  if (format == KLocale::LongName && possessive) {
249  switch (month) {
250  case 1:
251  return ki18nc("Indian National month 1 - KLocale::LongName Possessive", "of Chaitra").toString(languages);
252  case 2:
253  return ki18nc("Indian National month 2 - KLocale::LongName Possessive", "of Vaishākh").toString(languages);
254  case 3:
255  return ki18nc("Indian National month 3 - KLocale::LongName Possessive", "of Jyaishtha").toString(languages);
256  case 4:
257  return ki18nc("Indian National month 4 - KLocale::LongName Possessive", "of Āshādha").toString(languages);
258  case 5:
259  return ki18nc("Indian National month 5 - KLocale::LongName Possessive", "of Shrāvana").toString(languages);
260  case 6:
261  return ki18nc("Indian National month 6 - KLocale::LongName Possessive", "of Bhādrapad").toString(languages);
262  case 7:
263  return ki18nc("Indian National month 7 - KLocale::LongName Possessive", "of Āshwin").toString(languages);
264  case 8:
265  return ki18nc("Indian National month 8 - KLocale::LongName Possessive", "of Kārtik").toString(languages);
266  case 9:
267  return ki18nc("Indian National month 9 - KLocale::LongName Possessive", "of Agrahayana").toString(languages);
268  case 10:
269  return ki18nc("Indian National month 10 - KLocale::LongName Possessive", "of Paush").toString(languages);
270  case 11:
271  return ki18nc("Indian National month 11 - KLocale::LongName Possessive", "of Māgh").toString(languages);
272  case 12:
273  return ki18nc("Indian National month 12 - KLocale::LongName Possessive", "of Phālgun").toString(languages);
274  default:
275  return QString();
276  }
277  }
278 
279  // Default to LongName
280  switch (month) {
281  case 1:
282  return ki18nc("Indian National month 1 - KLocale::LongName", "Chaitra").toString(languages);
283  case 2:
284  return ki18nc("Indian National month 2 - KLocale::LongName", "Vaishākh").toString(languages);
285  case 3:
286  return ki18nc("Indian National month 3 - KLocale::LongName", "Jyaishtha").toString(languages);
287  case 4:
288  return ki18nc("Indian National month 4 - KLocale::LongName", "Āshādha").toString(languages);
289  case 5:
290  return ki18nc("Indian National month 5 - KLocale::LongName", "Shrāvana").toString(languages);
291  case 6:
292  return ki18nc("Indian National month 6 - KLocale::LongName", "Bhādrapad").toString(languages);
293  case 7:
294  return ki18nc("Indian National month 7 - KLocale::LongName", "Āshwin").toString(languages);
295  case 8:
296  return ki18nc("Indian National month 8 - KLocale::LongName", "Kārtik").toString(languages);
297  case 9:
298  return ki18nc("Indian National month 9 - KLocale::LongName", "Agrahayana").toString(languages);
299  case 10:
300  return ki18nc("Indian National month 10 - KLocale::LongName", "Paush").toString(languages);
301  case 11:
302  return ki18nc("Indian National month 11 - KLocale::LongName", "Māgh").toString(languages);
303  case 12:
304  return ki18nc("Indian National month 12 - KLocale::LongName", "Phālgun").toString(languages);
305  default:
306  return QString();
307  }
308 }
309 
310 QString KCalendarSystemIndianNationalPrivate::weekDayName(int weekDay, KLocale::DateTimeComponentFormat format) const
311 {
312  QStringList languages = locale()->languageList();
313 
314  if (format == KLocale::NarrowName) {
315  switch (weekDay) {
316  case 1:
317  return ki18nc("Indian National weekday 1 - KLocale::NarrowName ", "S").toString(languages);
318  case 2:
319  return ki18nc("Indian National weekday 2 - KLocale::NarrowName ", "M").toString(languages);
320  case 3:
321  return ki18nc("Indian National weekday 3 - KLocale::NarrowName ", "B").toString(languages);
322  case 4:
323  return ki18nc("Indian National weekday 4 - KLocale::NarrowName ", "G").toString(languages);
324  case 5:
325  return ki18nc("Indian National weekday 5 - KLocale::NarrowName ", "S").toString(languages);
326  case 6:
327  return ki18nc("Indian National weekday 6 - KLocale::NarrowName ", "S").toString(languages);
328  case 7:
329  return ki18nc("Indian National weekday 7 - KLocale::NarrowName ", "R").toString(languages);
330  default:
331  return QString();
332  }
333  }
334 
335  if (format == KLocale::ShortName || format == KLocale:: ShortNumber) {
336  switch (weekDay) {
337  case 1:
338  return ki18nc("Indian National weekday 1 - KLocale::ShortName", "Som").toString(languages);
339  case 2:
340  return ki18nc("Indian National weekday 2 - KLocale::ShortName", "Mañ").toString(languages);
341  case 3:
342  return ki18nc("Indian National weekday 3 - KLocale::ShortName", "Bud").toString(languages);
343  case 4:
344  return ki18nc("Indian National weekday 4 - KLocale::ShortName", "Gur").toString(languages);
345  case 5:
346  return ki18nc("Indian National weekday 5 - KLocale::ShortName", "Suk").toString(languages);
347  case 6:
348  return ki18nc("Indian National weekday 6 - KLocale::ShortName", "San").toString(languages);
349  case 7:
350  return ki18nc("Indian National weekday 7 - KLocale::ShortName", "Rav").toString(languages);
351  default: return QString();
352  }
353  }
354 
355  switch (weekDay) {
356  case 1:
357  return ki18nc("Indian National weekday 1 - KLocale::LongName", "Somavãra").toString(languages);
358  case 2:
359  return ki18nc("Indian National weekday 2 - KLocale::LongName", "Mañgalvã").toString(languages);
360  case 3:
361  return ki18nc("Indian National weekday 3 - KLocale::LongName", "Budhavãra").toString(languages);
362  case 4:
363  return ki18nc("Indian National weekday 4 - KLocale::LongName", "Guruvãra").toString(languages);
364  case 5:
365  return ki18nc("Indian National weekday 5 - KLocale::LongName", "Sukravãra").toString(languages);
366  case 6:
367  return ki18nc("Indian National weekday 6 - KLocale::LongName", "Sanivãra").toString(languages);
368  case 7:
369  return ki18nc("Indian National weekday 7 - KLocale::LongName", "Raviãra").toString(languages);
370  default:
371  return QString();
372  }
373 }
374 
375 KCalendarSystemIndianNational::KCalendarSystemIndianNational(const KSharedConfig::Ptr config, const KLocale *locale)
376  : KCalendarSystem(*new KCalendarSystemIndianNationalPrivate(this), config, locale)
377 {
378  d_ptr->loadConfig(calendarType());
379 }
380 
381 KCalendarSystemIndianNational::KCalendarSystemIndianNational(KCalendarSystemIndianNationalPrivate &dd,
382  const KSharedConfig::Ptr config,
383  const KLocale *locale)
384  : KCalendarSystem(dd, config, locale)
385 {
386  d_ptr->loadConfig(calendarType());
387 }
388 
389 KCalendarSystemIndianNational::~KCalendarSystemIndianNational()
390 {
391 }
392 
393 QString KCalendarSystemIndianNational::calendarType() const
394 {
395  return QLatin1String("indian-national");
396 }
397 
398 KLocale::CalendarSystem KCalendarSystemIndianNational::calendarSystem() const
399 {
401 }
402 
403 QDate KCalendarSystemIndianNational::epoch() const
404 {
405  //0000-01-01, has Year 0.
406  //0078-03-22 AD Gregorian / 0078-03-24 AD Julian
407  return QDate::fromJulianDay(1749994);
408 }
409 
410 QDate KCalendarSystemIndianNational::earliestValidDate() const
411 {
412  //0000-01-01, has Year 0.
413  //0078-03-22 AD Gregorian / 0078-03-24 AD Julian
414  //Don't do proleptic yet, need to check
415  return QDate::fromJulianDay(1749630);
416 }
417 
418 QDate KCalendarSystemIndianNational::latestValidDate() const
419 {
420  // Set to last day of year 9999 until confirm date formats & widgets support > 9999
421  //9999-12-30
422  //10078-03-21 AD Gregorian
423  return QDate::fromJulianDay(5402054);
424 }
425 
426 QString KCalendarSystemIndianNational::monthName(int month, int year, MonthNameFormat format) const
427 {
428  return KCalendarSystem::monthName(month, year, format);
429 }
430 
431 QString KCalendarSystemIndianNational::monthName(const QDate &date, MonthNameFormat format) const
432 {
433  return KCalendarSystem::monthName(date, format);
434 }
435 
436 QString KCalendarSystemIndianNational::weekDayName(int weekDay, WeekDayNameFormat format) const
437 {
438  return KCalendarSystem::weekDayName(weekDay, format);
439 }
440 
441 QString KCalendarSystemIndianNational::weekDayName(const QDate &date, WeekDayNameFormat format) const
442 {
443  return KCalendarSystem::weekDayName(date, format);
444 }
445 
446 bool KCalendarSystemIndianNational::isLunar() const
447 {
448  return false;
449 }
450 
451 bool KCalendarSystemIndianNational::isLunisolar() const
452 {
453  return true;
454 }
455 
456 bool KCalendarSystemIndianNational::isSolar() const
457 {
458  return false;
459 }
460 
461 bool KCalendarSystemIndianNational::isProleptic() const
462 {
463  return false;
464 }
465 
466 bool KCalendarSystemIndianNational::julianDayToDate(qint64 jd, int &year, int &month, int &day) const
467 {
468  int L, N, I, J, D, M, Y;
469 
470  // "Explanatory Supplement to the Astronomical Almanac" 2006 section 12.94 pp 605-606
471  // Originally from "Report of the Calendar Reform Committee" 1955
472  L = jd + 68518;
473  N = (4 * L) / 146097;
474  L = L - (146097 * N + 3) / 4;
475  I = (4000 * (L + 1)) / 1461001;
476  L = L - (1461 * I) / 4 + 1;
477  J = ((L - 1) / 31) * (1 - L / 185) + (L / 185) * ((L - 156) / 30 + 5) - L / 366;
478  D = L - 31 * J + ((J + 2) / 8) * (J - 5);
479  L = J / 11;
480  M = J + 2 - 12 * L;
481  Y = 100 * (N - 49) + L + I - 78;
482 
483  day = D;
484  month = M;
485  year = Y;
486 
487  return true;
488 }
489 
490 bool KCalendarSystemIndianNational::dateToJulianDay(int year, int month, int day, qint64 &jd) const
491 {
492  int Y = year;
493  int M = month;
494  int D = day;
495 
496  // "Explanatory Supplement to the Astronomical Almanac" 2006 section 12.94 pp 605-606
497  // Originally from "Report of the Calendar Reform Committee" 1955
498  jd = 365 * Y
499  + (Y + 78 - 1 / M) / 4
500  + 31 * M
501  - (M + 9) / 11
502  - (M / 7) * (M - 7)
503  - (3 * ((Y + 78 - 1 / M) / 100 + 1)) / 4
504  + D
505  + 1749579;
506 
507  return true;
508 }
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.
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
Indian National Calendar, not the Lunar Calendar.
Definition: klocale.h:659
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...)
Long text format, e.g.
Definition: klocale.h:777
virtual int month(const QDate &date) const
Returns the month portion of a given date in the current calendar system.
const KLocale * locale() const
Returns the locale used for translations and formats for this calendar system instance.
WeekDayNameFormat
Format for returned month / day name.
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.
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
QDate fromJulianDay(qint64 jd)
virtual QString calendarType() const =0
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 Fri Aug 14 2020 22:58:19 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.