File indexing completed on 2024-04-28 11:36:02

0001 /*
0002     Copyright 2009, 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 #include "kcalendarsystemthai_p.h"
0021 #include "kcalendarsystemgregorianprivate_p.h"
0022 
0023 #include "klocale.h"
0024 #include "klocalizedstring.h"
0025 
0026 #include <QDate>
0027 
0028 //Reuse the Gregorian private implementation
0029 class KCalendarSystemThaiPrivate : public KCalendarSystemGregorianPrivate
0030 {
0031 public:
0032     KDELIBS4SUPPORT_DEPRECATED explicit KCalendarSystemThaiPrivate(KCalendarSystemThai *q);
0033     ~KCalendarSystemThaiPrivate() override;
0034 
0035     void loadDefaultEraList() override;
0036     int daysInMonth(int year, int month) const override;
0037     int daysInYear(int year) const override;
0038     bool isLeapYear(int year) const override;
0039     bool hasYearZero() const override;
0040     int earliestValidYear() const override;
0041 };
0042 
0043 //Override only a few of the Gregorian private methods
0044 
0045 KCalendarSystemThaiPrivate::KCalendarSystemThaiPrivate(KCalendarSystemThai *q)
0046     : KCalendarSystemGregorianPrivate(q)
0047 {
0048 }
0049 
0050 KCalendarSystemThaiPrivate::~KCalendarSystemThaiPrivate()
0051 {
0052 }
0053 
0054 void KCalendarSystemThaiPrivate::loadDefaultEraList()
0055 {
0056     QString name, shortName, format;
0057 
0058     name = i18nc("Calendar Era: Thai Buddhist Era, years > 0, LongFormat", "Buddhist Era");
0059     shortName = i18nc("Calendar Era: Thai Buddhist Era, years > 0, ShortFormat", "BE");
0060     format = i18nc("(kdedt-format) Thai, BE, full era year format used for %EY, e.g. 2000 BE", "%Ey %EC");
0061     addEra('+', 1, q->epoch(), 1, q->latestValidDate(), name, shortName, format);
0062 }
0063 
0064 int KCalendarSystemThaiPrivate::daysInMonth(int year, int month) const
0065 {
0066     return KCalendarSystemGregorianPrivate::daysInMonth(year - 543, month);
0067 }
0068 
0069 int KCalendarSystemThaiPrivate::daysInYear(int year) const
0070 {
0071     return KCalendarSystemGregorianPrivate::daysInYear(year - 543);
0072 }
0073 
0074 bool KCalendarSystemThaiPrivate::isLeapYear(int year) const
0075 {
0076     return KCalendarSystemGregorianPrivate::isLeapYear(year - 543);
0077 }
0078 
0079 bool KCalendarSystemThaiPrivate::hasYearZero() const
0080 {
0081     return true;
0082 }
0083 
0084 int KCalendarSystemThaiPrivate::earliestValidYear() const
0085 {
0086     return 0;
0087 }
0088 
0089 KCalendarSystemThai::KCalendarSystemThai(const KSharedConfig::Ptr config, const KLocale *locale)
0090     : KCalendarSystemGregorian(*new KCalendarSystemThaiPrivate(this), config, locale)
0091 {
0092     d_ptr->loadConfig(calendarType());
0093 }
0094 
0095 KCalendarSystemThai::KCalendarSystemThai(KCalendarSystemThaiPrivate &dd,
0096         const KSharedConfig::Ptr config, const KLocale *locale)
0097     : KCalendarSystemGregorian(dd, config, locale)
0098 {
0099     d_ptr->loadConfig(calendarType());
0100 }
0101 
0102 KCalendarSystemThai::~KCalendarSystemThai()
0103 {
0104 }
0105 
0106 QString KCalendarSystemThai::calendarType() const
0107 {
0108     return QLatin1String("thai");
0109 }
0110 
0111 KLocale::CalendarSystem KCalendarSystemThai::calendarSystem() const
0112 {
0113     return KLocale::ThaiCalendar;
0114 }
0115 
0116 QDate KCalendarSystemThai::epoch() const
0117 {
0118     // 0000-01-01 = 0544-01-01 BC Gregorian = 0544-01-07 BC Julian
0119     return QDate::fromJulianDay(1522734);
0120 }
0121 
0122 QDate KCalendarSystemThai::earliestValidDate() const
0123 {
0124     return epoch();
0125 }
0126 
0127 QDate KCalendarSystemThai::latestValidDate() const
0128 {
0129     // Set to last day of year 9999 until confirm date formats & widgets support > 9999
0130     // 9999-12-31 = 9456-12-31 AD Gregorian
0131     return QDate::fromJulianDay(5175158);
0132 }
0133 
0134 bool KCalendarSystemThai::isLunar() const
0135 {
0136     return KCalendarSystemGregorian::isLunar();
0137 }
0138 
0139 bool KCalendarSystemThai::isLunisolar() const
0140 {
0141     return KCalendarSystemGregorian::isLunisolar();
0142 }
0143 
0144 bool KCalendarSystemThai::isSolar() const
0145 {
0146     return KCalendarSystemGregorian::isSolar();
0147 }
0148 
0149 bool KCalendarSystemThai::isProleptic() const
0150 {
0151     return false;
0152 }
0153 
0154 bool KCalendarSystemThai::julianDayToDate(qint64 jd, int &year, int &month, int &day) const
0155 {
0156     bool result = KCalendarSystemGregorian::julianDayToDate(jd, year, month, day);
0157     year = year + 543;
0158     return result;
0159 }
0160 
0161 bool KCalendarSystemThai::dateToJulianDay(int year, int month, int day, qint64 &jd) const
0162 {
0163     return KCalendarSystemGregorian::dateToJulianDay(year - 543, month, day, jd);
0164 }
0165