Verified Commit 73f19554 authored by Fushan Wen's avatar Fushan Wen 💬
Browse files

alternatecalendar: Add ICU calendar system private class

This class will be used in calendar providers that require ICU library.

CCBUG: 429892
parent dc7487d2
......@@ -21,6 +21,15 @@ set_package_properties(ICU
)
if(ICU_FOUND)
set(HAVE_ICU TRUE)
set(alternatecalendar_LIB_SRCS
${alternatecalendar_LIB_SRCS}
provider/icucalendar_p.cpp
)
set(alternatecalendar_LINK_LIBRARIES
${alternatecalendar_LINK_LIBRARIES}
ICU::uc
ICU::i18n
)
endif()
configure_file(config-ICU.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-ICU.h)
......
/*
SPDX-FileCopyrightText: 2021 Gary Wang <wzc782970009@gmail.com>
SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "icucalendar_p.h"
#include <QDate>
#include <QString>
ICUCalendarPrivate::ICUCalendarPrivate()
: m_errorCode(U_ZERO_ERROR)
, m_GregorianCalendar(icu::Calendar::createInstance("en_US@calendar=gregorian", m_errorCode))
{
}
ICUCalendarPrivate::~ICUCalendarPrivate()
{
}
int32_t ICUCalendarPrivate::year() const
{
const int32_t year = m_calendar->get(UCAL_YEAR, m_errorCode);
if (U_FAILURE(m_errorCode)) {
return -1;
}
return year;
}
int32_t ICUCalendarPrivate::month() const
{
const int32_t month = m_calendar->get(UCAL_MONTH, m_errorCode);
if (U_FAILURE(m_errorCode)) {
return -1;
}
return month + 1;
}
int32_t ICUCalendarPrivate::day() const
{
const int32_t day = m_calendar->get(UCAL_DATE, m_errorCode);
if (U_FAILURE(m_errorCode)) {
return -1;
}
return day;
}
QDate ICUCalendarPrivate::date() const
{
return QDate(year(), month(), day());
}
bool ICUCalendarPrivate::setDate(const QDate &date)
{
// icu: Month value is 0-based. e.g., 0 for January.
m_GregorianCalendar->set(date.year(), date.month() - 1, date.day());
const UDate time = m_GregorianCalendar->getTime(m_errorCode);
if (U_FAILURE(m_errorCode)) {
return false;
}
m_calendar->setTime(time, m_errorCode);
return !U_FAILURE(m_errorCode);
}
bool ICUCalendarPrivate::setTime(double time)
{
m_calendar->setTime(time, m_errorCode);
return !U_FAILURE(m_errorCode);
}
/*
SPDX-FileCopyrightText: 2021 Gary Wang <wzc782970009@gmail.com>
SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <unicode/calendar.h>
#include <unicode/smpdtfmt.h>
class QDate;
class QString;
class ICUCalendarPrivate
{
public:
/**
* Initialize the Gregorian Calendar, which will be used in date conversion.
*/
explicit ICUCalendarPrivate();
virtual ~ICUCalendarPrivate();
/**
* Returns the value for a given time field in the alternate calendar.
*/
int32_t year() const;
int32_t month() const;
int32_t day() const;
/**
* Returns the date from the alternate calendar.
*
* @return the date from the alternate calendar
*/
QDate date() const;
/**
* Sets the date in the Gregorian Calendar, and convert the date to
* the alternate calendar.
*
* @param date the Gregorian Calendar's date to be converted
* @return @c true if the date is successfully set, @c false otherwise
*/
bool setDate(const QDate &date);
/**
* Sets the alternate calendar's current time with the given time.
*
* @param time the Gregorian Calendar's time as milliseconds
* @return @c true if the time is successfully set, @c false otherwise
*/
bool setTime(double time);
protected:
/**
* Alternate calendar
*/
std::unique_ptr<icu::Calendar> m_calendar;
/**
* Standard ICU4C error code.
*/
mutable UErrorCode m_errorCode;
private:
/**
* Gregorian Calendar
*/
const std::unique_ptr<icu::Calendar> m_GregorianCalendar;
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment