File indexing completed on 2024-05-12 16:59:38

0001 /*
0002     SPDX-FileCopyrightText: 2021 Gary Wang <wzc782970009@gmail.com>
0003     SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "icucalendar_p.h"
0009 
0010 #include <QDate>
0011 #include <QString>
0012 
0013 ICUCalendarPrivate::ICUCalendarPrivate()
0014     : m_errorCode(U_ZERO_ERROR)
0015     , m_GregorianCalendar(icu::Calendar::createInstance("en_US@calendar=gregorian", m_errorCode))
0016 {
0017 }
0018 
0019 ICUCalendarPrivate::~ICUCalendarPrivate()
0020 {
0021 }
0022 
0023 int32_t ICUCalendarPrivate::year() const
0024 {
0025     const int32_t year = m_calendar->get(UCAL_YEAR, m_errorCode);
0026 
0027     if (U_FAILURE(m_errorCode)) {
0028         return -1;
0029     }
0030 
0031     return year;
0032 }
0033 
0034 int32_t ICUCalendarPrivate::month() const
0035 {
0036     const int32_t month = m_calendar->get(UCAL_MONTH, m_errorCode);
0037 
0038     if (U_FAILURE(m_errorCode)) {
0039         return -1;
0040     }
0041 
0042     return month + 1;
0043 }
0044 
0045 int32_t ICUCalendarPrivate::day() const
0046 {
0047     const int32_t day = m_calendar->get(UCAL_DATE, m_errorCode);
0048 
0049     if (U_FAILURE(m_errorCode)) {
0050         return -1;
0051     }
0052 
0053     return day;
0054 }
0055 
0056 QCalendar::YearMonthDay ICUCalendarPrivate::date() const
0057 {
0058     return QCalendar::YearMonthDay(year(), month(), day());
0059 }
0060 
0061 bool ICUCalendarPrivate::setDate(const QDate &date)
0062 {
0063     // icu: Month value is 0-based. e.g., 0 for January.
0064     m_GregorianCalendar->set(date.year(), date.month() - 1, date.day());
0065 
0066     const UDate time = m_GregorianCalendar->getTime(m_errorCode);
0067 
0068     if (U_FAILURE(m_errorCode)) {
0069         return false;
0070     }
0071 
0072     m_calendar->setTime(time, m_errorCode);
0073 
0074     return !U_FAILURE(m_errorCode);
0075 }
0076 
0077 bool ICUCalendarPrivate::setTime(double time)
0078 {
0079     m_calendar->setTime(time, m_errorCode);
0080 
0081     return !U_FAILURE(m_errorCode);
0082 }