blob: 3fa4a4e564730026079d009b1c2e9a3c374b3254 [file] [log] [blame]
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LIBTEXTCLASSIFIER_UTILS_CALENDAR_CALENDAR_ICU_H_
#define LIBTEXTCLASSIFIER_UTILS_CALENDAR_CALENDAR_ICU_H_
#include <memory>
#include <string>
#include "annotator/types.h"
#include "utils/base/integral_types.h"
#include "utils/base/logging.h"
#include "utils/calendar/calendar-common.h"
#include "unicode/gregocal.h"
#include "unicode/timezone.h"
#include "unicode/ucal.h"
namespace libtextclassifier3 {
class Calendar {
public:
bool Initialize(const std::string& time_zone, const std::string& locale,
int64 time_ms_utc);
bool AddDayOfMonth(int value) const;
bool AddYear(int value) const;
bool AddMonth(int value) const;
bool GetDayOfWeek(int* value) const;
bool GetFirstDayOfWeek(int* value) const;
bool GetTimeInMillis(int64* value) const;
bool SetZoneOffset(int value) const;
bool SetDstOffset(int value) const;
bool SetYear(int value) const;
bool SetMonth(int value) const;
bool SetDayOfYear(int value) const;
bool SetDayOfMonth(int value) const;
bool SetDayOfWeek(int value) const;
bool SetHourOfDay(int value) const;
bool SetMinute(int value) const;
bool SetSecond(int value) const;
bool SetMillisecond(int value) const;
private:
// We don't use a unique_ptr here because icu::Calendar has an implicit
// destructor - meaning that we couldn't use a forward declaration and would
// have to put the ICU includes in the header.
std::unique_ptr<icu::Calendar> calendar_;
};
class CalendarLib {
public:
// Interprets parse_data as milliseconds since_epoch. Relative times are
// resolved against the current time (reference_time_ms_utc). Returns true if
// the interpratation was successful, false otherwise.
bool InterpretParseData(const DateParseData& parse_data,
int64 reference_time_ms_utc,
const std::string& reference_timezone,
const std::string& reference_locale,
DatetimeGranularity granularity,
int64* interpreted_time_ms_utc) const {
Calendar calendar;
calendar::CalendarLibTempl<Calendar> impl_;
if (!impl_.InterpretParseData(parse_data, reference_time_ms_utc,
reference_timezone, reference_locale,
granularity, &calendar)) {
return false;
}
return calendar.GetTimeInMillis(interpreted_time_ms_utc);
}
};
} // namespace libtextclassifier3
#endif // LIBTEXTCLASSIFIER_UTILS_CALENDAR_CALENDAR_ICU_H_