| 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 | |
| 10 | #include "unicode/utypes.h" |
| 11 | |
| 12 | #if !UCONFIG_NO_FORMATTING |
| 13 | |
| 14 | #include "gregoimp.h" |
| 15 | #include "umutex.h" |
| 16 | #include "coptccal.h" |
| 17 | #include "cecal.h" |
| 18 | #include <float.h> |
| 19 | |
| 20 | U_NAMESPACE_BEGIN |
| 21 | |
| 22 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CopticCalendar) |
| 23 | |
| 24 | static const int32_t COPTIC_JD_EPOCH_OFFSET = 1824665; |
| 25 | |
| 26 | //------------------------------------------------------------------------- |
| 27 | // Constructors... |
| 28 | //------------------------------------------------------------------------- |
| 29 | |
| 30 | CopticCalendar::CopticCalendar(const Locale& aLocale, UErrorCode& success) |
| 31 | : CECalendar(aLocale, success) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | CopticCalendar::CopticCalendar (const CopticCalendar& other) |
| 36 | : CECalendar(other) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | CopticCalendar::~CopticCalendar() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | CopticCalendar* |
| 45 | CopticCalendar::clone() const |
| 46 | { |
| 47 | return new CopticCalendar(*this); |
| 48 | } |
| 49 | |
| 50 | const char* |
| 51 | CopticCalendar::getType() const |
| 52 | { |
| 53 | return "coptic"; |
| 54 | } |
| 55 | |
| 56 | //------------------------------------------------------------------------- |
| 57 | // Calendar framework |
| 58 | //------------------------------------------------------------------------- |
| 59 | |
| 60 | int32_t |
| 61 | CopticCalendar::handleGetExtendedYear(UErrorCode& status) |
| 62 | { |
| 63 | if (U_FAILURE(status)) { |
| 64 | return 0; |
| 65 | } |
| 66 | if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) { |
| 67 | return internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1 |
| 68 | } |
| 69 | // The year defaults to the epoch start, the era to CE |
| 70 | int32_t era = internalGet(UCAL_ERA, CE); |
| 71 | if (era == BCE) { |
| 72 | return 1 - internalGet(UCAL_YEAR, 1); // Convert to extended year |
| 73 | } |
| 74 | if (era == CE){ |
| 75 | return internalGet(UCAL_YEAR, 1); // Default to year 1 |
| 76 | } |
| 77 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | CopticCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status) |
| 83 | { |
| 84 | int32_t eyear, month, day, era, year; |
| 85 | jdToCE(julianDay, getJDEpochOffset(), eyear, month, day, status); |
| 86 | if (U_FAILURE(status)) return; |
| 87 | |
| 88 | if (eyear <= 0) { |
| 89 | era = BCE; |
| 90 | year = 1 - eyear; |
| 91 | } else { |
| 92 | era = CE; |
| 93 | year = eyear; |
| 94 | } |
| 95 | |
| 96 | internalSet(UCAL_EXTENDED_YEAR, eyear); |
| 97 | internalSet(UCAL_ERA, era); |
| 98 | internalSet(UCAL_YEAR, year); |
| 99 | internalSet(UCAL_MONTH, month); |
| 100 | internalSet(UCAL_ORDINAL_MONTH, month); |
| 101 | internalSet(UCAL_DATE, day); |
| 102 | internalSet(UCAL_DAY_OF_YEAR, (30 * month) + day); |
| 103 | } |
| 104 | |
| 105 | constexpr uint32_t kCopticRelatedYearDiff = 284; |
| 106 | |
| 107 | int32_t CopticCalendar::getRelatedYear(UErrorCode &status) const |
| 108 | { |
| 109 | int32_t year = get(UCAL_EXTENDED_YEAR, status); |
| 110 | if (U_FAILURE(status)) { |
| 111 | return 0; |
| 112 | } |
| 113 | return year + kCopticRelatedYearDiff; |
| 114 | } |
| 115 | |
| 116 | void CopticCalendar::setRelatedYear(int32_t year) |
| 117 | { |
| 118 | // set extended year |
| 119 | set(UCAL_EXTENDED_YEAR, year - kCopticRelatedYearDiff); |
| 120 | } |
| 121 | |
| 122 | IMPL_SYSTEM_DEFAULT_CENTURY(CopticCalendar, "@calendar=coptic") |
| 123 | |
| 124 | int32_t |
| 125 | CopticCalendar::getJDEpochOffset() const |
| 126 | { |
| 127 | return COPTIC_JD_EPOCH_OFFSET; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | U_NAMESPACE_END |
| 132 | |
| 133 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
| 134 | //eof |