1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html#License
3/*
4 *******************************************************************************
5 * Copyright (C) 2005-2016, International Business Machines Corporation and    *
6 * others. All Rights Reserved.                                                *
7 *******************************************************************************
8 */
9
10package com.ibm.icu.util;
11
12import java.util.Date;
13import java.util.Locale;
14
15/**
16 * Implement the Coptic calendar system.
17 * <p>
18 * CopticCalendar usually should be instantiated using
19 * {@link com.ibm.icu.util.Calendar#getInstance(ULocale)} passing in a <code>ULocale</code>
20 * with the tag <code>"@calendar=coptic"</code>.</p>
21 *
22 * @see com.ibm.icu.util.Calendar
23 * @stable ICU 3.4
24 */
25public final class CopticCalendar extends CECalendar
26{
27    // jdk1.4.2 serialver
28    private static final long serialVersionUID = 5903818751846742911L;
29
30    /**
31     * Constant for ωογτ / تﻮﺗ,
32     * the 1st month of the Coptic year.
33     * @stable ICU 3.4
34     */
35    public static final int TOUT = 0;
36
37    /**
38     * Constant for Παοπι / ﻪﺑﺎﺑ,
39     * the 2nd month of the Coptic year.
40     * @stable ICU 3.4
41     */
42    public static final int BABA = 1;
43
44    /**
45     * Constant for Αθορ / رﻮﺗﺎﻫ,
46     * the 3rd month of the Coptic year.
47     * @stable ICU 3.4
48     */
49    public static final int HATOR = 2;
50
51    /**
52     * Constant for Χοιακ / ﻚﻬﻴﻛ;,
53     * the 4th month of the Coptic year.
54     * @stable ICU 3.4
55     */
56    public static final int KIAHK = 3;
57
58    /**
59     * Constant for Τωβι / طﻮﺒﻫ,
60     * the 5th month of the Coptic year.
61     * @stable ICU 3.4
62     */
63    public static final int TOBA = 4;
64
65    /**
66     * Constant for Μεϣιρ / ﺮﻴﺸﻣأ,
67     * the 6th month of the Coptic year.
68     * @stable ICU 3.4
69     */
70    public static final int AMSHIR = 5;
71
72    /**
73     * Constant for Παρεμϩατ / تﺎﻬﻣﺮﺑ,
74     * the 7th month of the Coptic year.
75     * @stable ICU 3.4
76     */
77    public static final int BARAMHAT = 6;
78
79    /**
80     * Constant for Φαρμοθι / هدﻮﻣﺮﺑ,
81     * the 8th month of the Coptic year.
82     * @stable ICU 3.4
83     */
84    public static final int BARAMOUDA = 7;
85
86    /**
87     * Constant for Παϣαν / ﺲﻨﺸﺑ;,
88     * the 9th month of the Coptic year.
89     * @stable ICU 3.4
90     */
91    public static final int BASHANS = 8;
92
93    /**
94     * Constant for Παωνι / ﻪﻧؤﻮﺑ,
95     * the 10th month of the Coptic year.
96     * @stable ICU 3.4
97     */
98    public static final int PAONA = 9;
99
100    /**
101     * Constant for Επηπ / ﺐﻴﺑأ,
102     * the 11th month of the Coptic year.
103     * @stable ICU 3.4
104     */
105    public static final int EPEP = 10;
106
107    /**
108     * Constant for Μεϲωρη / ىﺮﺴﻣ,
109     * the 12th month of the Coptic year.
110     * @stable ICU 3.4
111     */
112    public static final int MESRA = 11;
113
114    /**
115     * Constant for Πικογϫι μαβοτ / ﺮﻴﻐﺼﻟاﺮﻬﺸﻟا,
116     * the 13th month of the Coptic year.
117     * @stable ICU 3.4
118     */
119    public static final int NASIE = 12;
120
121    private static final int JD_EPOCH_OFFSET  = 1824665;
122
123    // Eras
124    private static final int BCE = 0;
125    private static final int CE = 1;
126
127    /**
128     * Constructs a default <code>CopticCalendar</code> using the current time
129     * in the default time zone with the default locale.
130     * @stable ICU 3.4
131     */
132    public CopticCalendar() {
133        super();
134    }
135
136    /**
137     * Constructs a <code>CopticCalendar</code> based on the current time
138     * in the given time zone with the default locale.
139     *
140     * @param zone The time zone for the new calendar.
141     * @stable ICU 3.4
142     */
143    public CopticCalendar(TimeZone zone) {
144        super(zone);
145    }
146
147    /**
148     * Constructs a <code>CopticCalendar</code> based on the current time
149     * in the default time zone with the given locale.
150     *
151     * @param aLocale The locale for the new calendar.
152     * @stable ICU 3.4
153     */
154    public CopticCalendar(Locale aLocale) {
155        super(aLocale);
156    }
157
158    /**
159     * Constructs a <code>CopticCalendar</code> based on the current time
160     * in the default time zone with the given locale.
161     *
162     * @param locale The icu locale for the new calendar.
163     * @stable ICU 3.4
164     */
165    public CopticCalendar(ULocale locale) {
166        super(locale);
167    }
168
169    /**
170     * Constructs a <code>CopticCalendar</code> based on the current time
171     * in the given time zone with the given locale.
172     *
173     * @param zone The time zone for the new calendar.
174     * @param aLocale The locale for the new calendar.
175     * @stable ICU 3.4
176     */
177    public CopticCalendar(TimeZone zone, Locale aLocale) {
178        super(zone, aLocale);
179    }
180
181    /**
182     * Constructs a <code>CopticCalendar</code> based on the current time
183     * in the given time zone with the given locale.
184     *
185     * @param zone The time zone for the new calendar.
186     * @param locale The icu locale for the new calendar.
187     * @stable ICU 3.4
188     */
189    public CopticCalendar(TimeZone zone, ULocale locale) {
190        super(zone, locale);
191    }
192
193    /**
194     * Constructs a <code>CopticCalendar</code> with the given date set
195     * in the default time zone with the default locale.
196     *
197     * @param year      The value used to set the calendar's {@link #YEAR YEAR} time field.
198     * @param month     The value used to set the calendar's {@link #MONTH MONTH} time field.
199     *                  The value is 0-based. e.g., 0 for Tout.
200     * @param date      The value used to set the calendar's {@link #DATE DATE} time field.
201     * @stable ICU 3.4
202     */
203    public CopticCalendar(int year, int month, int date) {
204        super(year, month, date);
205    }
206
207    /**
208     * Constructs a <code>CopticCalendar</code> with the given date set
209     * in the default time zone with the default locale.
210     *
211     * @param date      The date to which the new calendar is set.
212     * @stable ICU 3.4
213     */
214    public CopticCalendar(Date date) {
215        super(date);
216    }
217
218    /**
219     * Constructs a <code>CopticCalendar</code> with the given date
220     * and time set for the default time zone with the default locale.
221     *
222     * @param year      The value used to set the calendar's {@link #YEAR YEAR} time field.
223     * @param month     The value used to set the calendar's {@link #MONTH MONTH} time field.
224     *                  The value is 0-based. e.g., 0 for Tout.
225     * @param date      The value used to set the calendar's {@link #DATE DATE} time field.
226     * @param hour      The value used to set the calendar's {@link #HOUR_OF_DAY HOUR_OF_DAY} time field.
227     * @param minute    The value used to set the calendar's {@link #MINUTE MINUTE} time field.
228     * @param second    The value used to set the calendar's {@link #SECOND SECOND} time field.
229     * @stable ICU 3.4
230     */
231    public CopticCalendar(int year, int month, int date, int hour,
232                          int minute, int second) {
233        super(year, month, date, hour, minute, second);
234    }
235
236    /**
237     * {@inheritDoc}
238     * @stable ICU 3.8
239     */
240    public String getType() {
241        return "coptic";
242    }
243
244    /**
245     * {@inheritDoc}
246     * @internal
247     * @deprecated This API is ICU internal only.
248     */
249    @Deprecated
250    protected int handleGetExtendedYear() {
251        int eyear;
252        if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR) {
253            eyear = internalGet(EXTENDED_YEAR, 1); // Default to year 1
254        } else {
255            // The year defaults to the epoch start, the era to AD
256            int era = internalGet(ERA, CE);
257            if (era == BCE) {
258                eyear = 1 - internalGet(YEAR, 1); // Convert to extended year
259            } else {
260                eyear = internalGet(YEAR, 1); // Default to year 1
261            }
262        }
263        return eyear;
264    }
265
266    /**
267     * {@inheritDoc}
268     * @internal
269     * @deprecated This API is ICU internal only.
270     */
271    @Deprecated
272    protected void handleComputeFields(int julianDay) {
273        int era, year;
274        int[] fields = new int[3];
275        jdToCE(julianDay, getJDEpochOffset(), fields);
276
277        // fields[0] eyear
278        // fields[1] month
279        // fields[2] day
280
281        if (fields[0] <= 0) {
282            era = BCE;
283            year = 1 - fields[0];
284        } else {
285            era = CE;
286            year = fields[0];
287        }
288
289        internalSet(EXTENDED_YEAR, fields[0]);
290        internalSet(ERA, era);
291        internalSet(YEAR, year);
292        internalSet(MONTH, fields[1]);
293        internalSet(DAY_OF_MONTH, fields[2]);
294        internalSet(DAY_OF_YEAR, (30 * fields[1]) + fields[2]);
295    }
296
297    /**
298     * {@inheritDoc}
299     * @internal
300     * @deprecated This API is ICU internal only.
301     */
302    @Deprecated
303    protected int getJDEpochOffset() {
304        return JD_EPOCH_OFFSET;
305    }
306
307    /**
308     * Convert an Coptic year, month, and day to a Julian day.
309     *
310     * @param year the year
311     * @param month the month
312     * @param date the day
313     * @draft ICU 3.4 (retain)
314     * @provisional This API might change or be removed in a future release.
315     */
316    // The equivalent operation can be done by public Calendar API.
317    // This API was accidentally marked as @draft, but we have no good
318    // reason to keep this.  For now, we leave it as is, but may be
319    // removed in future.  2008-03-21 yoshito
320    public static int copticToJD(long year, int month, int date) {
321        return ceToJD(year, month, date, JD_EPOCH_OFFSET);
322    }
323}
324
325