1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 2003-2013, International Business Machines Corporation and *
6 * others. All Rights Reserved. *
7 *******************************************************************************
8 *
9 * File BUDDHCAL.CPP
10 *
11 * Modification History:
12 * 05/13/2003 srl copied from gregocal.cpp
13 *
14 */
15
16 #include "unicode/utypes.h"
17
18 #if !UCONFIG_NO_FORMATTING
19
20 #include "buddhcal.h"
21 #include "gregoimp.h"
22 #include "unicode/gregocal.h"
23 #include "umutex.h"
24 #include <float.h>
25
26 U_NAMESPACE_BEGIN
27
28 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(BuddhistCalendar)
29
30 //static const int32_t kMaxEra = 0; // only 1 era
31
32 static const int32_t kBuddhistEraStart = -543; // 544 BC (Gregorian)
33
34 static const int32_t kGregorianEpoch = 1970; // used as the default value of EXTENDED_YEAR
35
BuddhistCalendar(const Locale & aLocale,UErrorCode & success)36 BuddhistCalendar::BuddhistCalendar(const Locale& aLocale, UErrorCode& success)
37 : GregorianCalendar(aLocale, success)
38 {
39 setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
40 }
41
~BuddhistCalendar()42 BuddhistCalendar::~BuddhistCalendar()
43 {
44 }
45
BuddhistCalendar(const BuddhistCalendar & source)46 BuddhistCalendar::BuddhistCalendar(const BuddhistCalendar& source)
47 : GregorianCalendar(source)
48 {
49 }
50
operator =(const BuddhistCalendar & right)51 BuddhistCalendar& BuddhistCalendar::operator= ( const BuddhistCalendar& right)
52 {
53 GregorianCalendar::operator=(right);
54 return *this;
55 }
56
clone() const57 BuddhistCalendar* BuddhistCalendar::clone() const
58 {
59 return new BuddhistCalendar(*this);
60 }
61
getType() const62 const char *BuddhistCalendar::getType() const
63 {
64 return "buddhist";
65 }
66
handleGetExtendedYear(UErrorCode & status)67 int32_t BuddhistCalendar::handleGetExtendedYear(UErrorCode& status)
68 {
69 if (U_FAILURE(status)) {
70 return 0;
71 }
72 // EXTENDED_YEAR in BuddhistCalendar is a Gregorian year.
73 // The default value of EXTENDED_YEAR is 1970 (Buddhist 2513)
74 if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
75 return internalGet(UCAL_EXTENDED_YEAR, kGregorianEpoch);
76 }
77 // extended year is a gregorian year, where 1 = 1AD, 0 = 1BC, -1 = 2BC, etc
78 int32_t year = internalGet(UCAL_YEAR, kGregorianEpoch - kBuddhistEraStart);
79 if (uprv_add32_overflow(year, kBuddhistEraStart, &year)) {
80 status = U_ILLEGAL_ARGUMENT_ERROR;
81 return 0;
82 }
83 return year;
84 }
85
handleComputeFields(int32_t julianDay,UErrorCode & status)86 void BuddhistCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status)
87 {
88 GregorianCalendar::handleComputeFields(julianDay, status);
89 int32_t y = internalGet(UCAL_EXTENDED_YEAR) - kBuddhistEraStart;
90 internalSet(UCAL_ERA, 0);
91 internalSet(UCAL_YEAR, y);
92 }
93
handleGetLimit(UCalendarDateFields field,ELimitType limitType) const94 int32_t BuddhistCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
95 {
96 if(field == UCAL_ERA) {
97 return BE;
98 }
99 return GregorianCalendar::handleGetLimit(field,limitType);
100 }
101
102 IMPL_SYSTEM_DEFAULT_CENTURY(BuddhistCalendar, "@calendar=buddhist")
103
104 U_NAMESPACE_END
105
106 #endif
107