1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho ******************************************************************************
385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Copyright (C) 2003-2008, International Business Machines Corporation
4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * and others. All Rights Reserved.
5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ******************************************************************************
6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * File PERSNCAL.CPP
8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Modification History:
10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *   Date        Name        Description
12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *   9/23/2003 mehran        posted to icu-design
13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *****************************************************************************
14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
1585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "persncal.h"
17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if !UCONFIG_NO_FORMATTING
19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
2085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#include "umutex.h"
2185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#include <float.h>
2285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
2385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hostatic const int8_t monthDays[] = { 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 };
24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic int32_t
26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querujalali_to_julian(int year, int month, int day)
27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t daysNo=0;
29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int i;
30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    year = year -475+2820;
32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    month -= 1;
33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    daysNo=(year/2820)*1029983;
35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    year=year % 2820;
36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    daysNo+=(year/128)* 46751;
38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if((year/128)>21)
39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        daysNo-=46751;
41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year=(year%128)+128;
42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    else
44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year=year%128;
45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(year>=29)
47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year-=29;
49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        daysNo+=10592;
50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(year>=66)
53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year-=66;
55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        daysNo+=24106;
56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    else if( year>=33)
58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        daysNo+=(year/33)* 12053;
60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year=year%33;
61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (year >= 5)
64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        daysNo += 1826;
66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year -=5;
67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    else if (year == 4)
69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        daysNo += 1460;
71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year -=4;
72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    daysNo += 1461 * (year/4);
75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    year %= 4;
76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    daysNo += 365 * year;
77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for (i = 0; i < month; i++) {
79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        daysNo += monthDays[i];
80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    daysNo += day;
83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return daysNo-856493;
85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic void julian_to_jalali (int32_t daysNo, int *h_y, int *h_m, int *h_d)
88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int year=0, month=0, day=0,scalarDays=0;
90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int i;
91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    daysNo+=856493;
93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    scalarDays=daysNo;
94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    year=(daysNo/1029983)*2820;
95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    daysNo=daysNo%1029983;
96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if((daysNo/46751)<=21)
98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year+=(daysNo/46751)* 128;
100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        daysNo=daysNo%46751;
101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    else
103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year+=(daysNo/46751)* 128;
105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        daysNo=daysNo%46751;
106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year-=128;
107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        daysNo+=46751;
108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (daysNo >= 10592)
111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year+= 29;
113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        daysNo -= 10592;
114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(daysNo>=24106)
117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        daysNo-=24106;
119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year+=66;
120ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
121ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
122ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(daysNo>=12053)
123ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
124ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        daysNo-=12053;
125ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year+=33;
126ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
127ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
128ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
129ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (daysNo >= 1826)
130ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
131ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year+= 5;
132ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        daysNo -= 1826;
133ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
134ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    else if (daysNo > 1095)
135ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
136ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year+= 3;
137ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        daysNo -= 1095;
138ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
139ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
140ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
141ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    year +=(4 * (daysNo/1461));
142ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    daysNo %= 1461;
143ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
144ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (daysNo == 0)
145ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
146ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year -= 1;
147ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        daysNo = 366;
148ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
149ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    else
150ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {
151ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year += daysNo/365;
152ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        daysNo = daysNo % 365;
153ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (daysNo == 0)
154ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        {
155ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            year -= 1;
156ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            daysNo = 365;
157ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
158ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
159ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
160ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
161ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    for (i = 0; i < 11 && daysNo > monthDays[i]; ++i) {
162ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        daysNo -= monthDays[i];
163ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
164ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
165ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    month = i + 1;
166ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
167ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    day = daysNo;
168ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
169ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    *h_d = day;
170ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    *h_m = month;
171ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    *h_y = year-2345;
172ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
173ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
174ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_BEGIN
175ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
176ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Implementation of the PersianCalendar class
177ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
178ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//-------------------------------------------------------------------------
179ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Constructors...
180ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//-------------------------------------------------------------------------
181ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
182ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruconst char *PersianCalendar::getType() const {
183ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return "persian";
184ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
185ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
186ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruCalendar* PersianCalendar::clone() const {
187ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return new PersianCalendar(*this);
188ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
189ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
190ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruPersianCalendar::PersianCalendar(const Locale& aLocale, UErrorCode& success)
191ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru  :   Calendar(TimeZone::createDefault(), aLocale, success)
192ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
193ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
194ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
195ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
196ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruPersianCalendar::PersianCalendar(const PersianCalendar& other) : Calendar(other) {
197ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
198ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
199ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruPersianCalendar::~PersianCalendar()
200ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
201ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
202ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
203ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//-------------------------------------------------------------------------
204ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Minimum / Maximum access functions
205ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//-------------------------------------------------------------------------
206ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
207ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const int32_t LIMITS[UCAL_FIELD_COUNT][4] = {
20885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Minimum  Greatest     Least   Maximum
20985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    //           Minimum   Maximum
21085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {        0,        0,        0,        0}, // ERA
21185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    { -5000000, -5000000,  5000000,  5000000}, // YEAR
21285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {        0,        0,       11,       11}, // MONTH
21385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {        1,        1,       52,       53}, // WEEK_OF_YEAR
21485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH
21585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {        1,       1,        29,       31}, // DAY_OF_MONTH
21685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {        1,       1,       365,      366}, // DAY_OF_YEAR
21785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK
21885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {        1,       1,         5,        5}, // DAY_OF_WEEK_IN_MONTH
21985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM
22085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR
22185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY
22285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE
22385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND
22485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND
22585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET
22685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET
22785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    { -5000000, -5000000,  5000000,  5000000}, // YEAR_WOY
22885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL
22985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    { -5000000, -5000000,  5000000,  5000000}, // EXTENDED_YEAR
23085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY
23185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY
23285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH
233ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru};
234ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const int32_t MONTH_COUNT[12][4]  = {
235ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    //len len2   st  st2
236ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {  31,  31,   0,   0 }, // Farvardin
237ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {  31,  31,  31,  31 }, // Ordibehesht
238ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {  31,  31,  62,  62 }, // Khordad
239ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {  31,  31,  93,  93 }, // Tir
240ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {  31,  31, 124, 124 }, // Mordad
241ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {  31,  31, 155, 155 }, // Shahrivar
242ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {  30,  30, 186, 186 }, // Mehr
243ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {  30,  30, 216, 216 }, // Aban
244ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {  30,  30, 246, 246 }, // Azar
245ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {  30,  30, 276, 276 }, // Dey
246ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {  30,  30, 306, 306 }, // Bahman
247ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    {  29,  30, 336, 336 }  // Esfand
248ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // len  length of month
249ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // len2 length of month in a leap year
250ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // st   days in year before start of month
251ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // st2  days in year before month in leap year
252ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru};
253ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
254ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t PersianCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const {
255ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return LIMITS[field][limitType];
256ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
257ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
258ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//-------------------------------------------------------------------------
259ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Assorted calculation utilities
260ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//
261ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
262ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/**
263ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Determine whether a year is a leap year in the Persian calendar
264ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
265ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool PersianCalendar::isLeapYear(int32_t year)
266ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
267ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return jalali_to_julian(year+1,1,1)-jalali_to_julian(year,1,1) == 366;
268ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
269ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
270ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/**
271ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Return the day # on which the given year starts.  Days are counted
272ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * from the Hijri epoch, origin 0.
273ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
274ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t PersianCalendar::yearStart(int32_t year) {
275ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return handleComputeMonthStart(year,1,FALSE);
276ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
277ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
278ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/**
279ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Return the day # on which the given month starts.  Days are counted
280ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * from the Hijri epoch, origin 0.
281ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
282ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param year  The hijri shamsi year
283ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param year  The hijri shamsi month, 0-based
284ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
285ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t PersianCalendar::monthStart(int32_t year, int32_t month) const {
286ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return handleComputeMonthStart(year,month,FALSE);
287ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
288ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
289ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//----------------------------------------------------------------------
290ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Calendar framework
291ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//----------------------------------------------------------------------
292ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
293ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/**
294ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Return the length (in days) of the given month.
295ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
296ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param year  The hijri shamsi year
297ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param year  The hijri shamsi month, 0-based
298ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
299ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t PersianCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const {
300ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return MONTH_COUNT[month][PersianCalendar::isLeapYear(extendedYear)?1:0];
301ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
302ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
303ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/**
304ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Return the number of days in the given Persian year
305ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
306ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t PersianCalendar::handleGetYearLength(int32_t extendedYear) const {
307ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return 365 + (PersianCalendar::isLeapYear(extendedYear) ? 1 : 0);
308ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
309ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
310ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//-------------------------------------------------------------------------
311ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Functions for converting from field values to milliseconds....
312ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//-------------------------------------------------------------------------
313ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
314ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Return JD of start of given month/year
315ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t PersianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool useMonth) const {
316ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // If the month is out of range, adjust it into range, and
317ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // modify the extended year value accordingly.
318ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (month < 0 || month > 11) {
319ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        eyear += month / 12;
320ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        month = month % 12;
321ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
322ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return jalali_to_julian(eyear,(useMonth?month+1:1),1)-1+1947955;
323ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
324ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
325ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//-------------------------------------------------------------------------
326ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Functions for converting from milliseconds to field values
327ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//-------------------------------------------------------------------------
328ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
329ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t PersianCalendar::handleGetExtendedYear() {
330ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t year;
331ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
332ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
333ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
334ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        year = internalGet(UCAL_YEAR, 1); // Default to year 1
335ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
336ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return year;
337ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
338ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
339ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/**
340ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Override Calendar to compute several fields specific to the Persian
341ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * calendar system.  These are:
342ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
343ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * <ul><li>ERA
344ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * <li>YEAR
345ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * <li>MONTH
346ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * <li>DAY_OF_MONTH
347ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * <li>DAY_OF_YEAR
348ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * <li>EXTENDED_YEAR</ul>
349ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
350ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * The DAY_OF_WEEK and DOW_LOCAL fields are already set when this
351ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * method is called. The getGregorianXxx() methods return Gregorian
352ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * calendar equivalents for the given Julian day.
353ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
354ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid PersianCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/) {
355ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int jy,jm,jd;
356ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    julian_to_jalali(julianDay-1947955,&jy,&jm,&jd);
357ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    internalSet(UCAL_ERA, 0);
358ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    internalSet(UCAL_YEAR, jy);
359ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    internalSet(UCAL_EXTENDED_YEAR, jy);
360ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    internalSet(UCAL_MONTH, jm-1);
361ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    internalSet(UCAL_DAY_OF_MONTH, jd);
362ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    internalSet(UCAL_DAY_OF_YEAR, jd + MONTH_COUNT[jm-1][2]);
363ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
364ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
365ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool
366ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruPersianCalendar::inDaylightTime(UErrorCode& status) const
367ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
368ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // copied from GregorianCalendar
369ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (U_FAILURE(status) || !getTimeZone().useDaylightTime())
370ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return FALSE;
371ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
372ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // Force an update of the state of the Calendar.
373ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    ((PersianCalendar*)this)->complete(status); // cast away const
374ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
375ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE);
376ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
377ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
37885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// default century
37985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoconst UDate     PersianCalendar::fgSystemDefaultCentury        = DBL_MIN;
38085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoconst int32_t   PersianCalendar::fgSystemDefaultCenturyYear    = -1;
38185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
38285bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoUDate           PersianCalendar::fgSystemDefaultCenturyStart       = DBL_MIN;
38385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoint32_t         PersianCalendar::fgSystemDefaultCenturyStartYear   = -1;
38485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
385ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool PersianCalendar::haveDefaultCentury() const
386ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
38785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return TRUE;
388ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
389ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
390ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUDate PersianCalendar::defaultCenturyStart() const
391ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
39285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return internalGetDefaultCenturyStart();
393ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
394ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
395ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t PersianCalendar::defaultCenturyStartYear() const
396ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
39785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return internalGetDefaultCenturyStartYear();
39885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
39985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
40085bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoUDate
40185bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoPersianCalendar::internalGetDefaultCenturyStart() const
40285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho{
40385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // lazy-evaluate systemDefaultCenturyStart
40485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UBool needsUpdate;
40585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UMTX_CHECK(NULL, (fgSystemDefaultCenturyStart == fgSystemDefaultCentury), needsUpdate);
40685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
40785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (needsUpdate) {
40885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        initializeSystemDefaultCentury();
40985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
41085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
41185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // use defaultCenturyStart unless it's the flag value;
41285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // then use systemDefaultCenturyStart
41385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
41485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return fgSystemDefaultCenturyStart;
41585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
41685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
41785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoint32_t
41885bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoPersianCalendar::internalGetDefaultCenturyStartYear() const
41985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho{
42085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // lazy-evaluate systemDefaultCenturyStartYear
42185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UBool needsUpdate;
42285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UMTX_CHECK(NULL, (fgSystemDefaultCenturyStart == fgSystemDefaultCentury), needsUpdate);
42385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
42485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (needsUpdate) {
42585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        initializeSystemDefaultCentury();
42685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
42785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
42885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // use defaultCenturyStart unless it's the flag value;
42985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // then use systemDefaultCenturyStartYear
43085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
43185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return    fgSystemDefaultCenturyStartYear;
43285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
43385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
43485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hovoid
43585bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoPersianCalendar::initializeSystemDefaultCentury()
43685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho{
43785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // initialize systemDefaultCentury and systemDefaultCenturyYear based
43885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // on the current time.  They'll be set to 80 years before
43985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // the current time.
44085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UErrorCode status = U_ZERO_ERROR;
44185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    PersianCalendar calendar(Locale("@calendar=persian"),status);
44285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (U_SUCCESS(status))
44385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {
44485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        calendar.setTime(Calendar::getNow(), status);
44585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        calendar.add(UCAL_YEAR, -80, status);
44685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        UDate    newStart =  calendar.getTime(status);
44785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        int32_t  newYear  =  calendar.get(UCAL_YEAR, status);
44885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        umtx_lock(NULL);
44985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury)
45085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        {
45185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            fgSystemDefaultCenturyStartYear = newYear;
45285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            fgSystemDefaultCenturyStart = newStart;
45385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        }
45485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        umtx_unlock(NULL);
45585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
45685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // We have no recourse upon failure unless we want to propagate the failure
45785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // out.
458ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
459ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
460ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUOBJECT_DEFINE_RTTI_IMPLEMENTATION(PersianCalendar)
461ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
462ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_END
463ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
464ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif
465ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
466