6a1035b151baa002f2d8e233ed49f07f1fc7db70
[jalviewjs.git] / site / swingjs / j2s / jssun / util / calendar / AbstractCalendar.js
1 Clazz.declarePackage ("jssun.util.calendar");
2 Clazz.load (["jssun.util.calendar.CalendarSystem"], "jssun.util.calendar.AbstractCalendar", ["java.lang.IllegalArgumentException", "jssun.util.calendar.CalendarUtils", "$.ZoneInfo"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.eras = null;
5 Clazz.instantialize (this, arguments);
6 }, jssun.util.calendar, "AbstractCalendar", jssun.util.calendar.CalendarSystem);
7 Clazz.makeConstructor (c$, 
8 function () {
9 Clazz.superConstructor (this, jssun.util.calendar.AbstractCalendar, []);
10 });
11 Clazz.overrideMethod (c$, "getEra", 
12 function (eraName) {
13 if (this.eras != null) {
14 for (var i = 0; i < this.eras.length; i++) {
15 if (this.eras[i].equals (eraName)) {
16 return this.eras[i];
17 }}
18 }return null;
19 }, "~S");
20 Clazz.overrideMethod (c$, "getEras", 
21 function () {
22 var e = null;
23 if (this.eras != null) {
24 e =  new Array (this.eras.length);
25 System.arraycopy (this.eras, 0, e, 0, this.eras.length);
26 }return e;
27 });
28 Clazz.overrideMethod (c$, "setEra", 
29 function (date, eraName) {
30 if (this.eras == null) {
31 return;
32 }for (var i = 0; i < this.eras.length; i++) {
33 var e = this.eras[i];
34 if (e != null && e.getName ().equals (eraName)) {
35 date.setEra (e);
36 return;
37 }}
38 throw  new IllegalArgumentException ("unknown era name: " + eraName);
39 }, "jssun.util.calendar.CalendarDate,~S");
40 Clazz.defineMethod (c$, "setEras", 
41 function (eras) {
42 this.eras = eras;
43 }, "~A");
44 Clazz.defineMethod (c$, "getCalendarDate", 
45 function () {
46 return this.getCalendarDate (System.currentTimeMillis (), this.newCalendarDate ());
47 });
48 Clazz.defineMethod (c$, "getCalendarDate", 
49 function (millis) {
50 return this.getCalendarDate (millis, this.newCalendarDate ());
51 }, "~N");
52 Clazz.defineMethod (c$, "getCalendarDate", 
53 function (millis, zone) {
54 var date = this.newCalendarDate (zone);
55 return this.getCalendarDate (millis, date);
56 }, "~N,java.util.TimeZone");
57 Clazz.defineMethod (c$, "getCalendarDate", 
58 function (millis, date) {
59 var ms = 0;
60 var zoneOffset = 0;
61 var saving = 0;
62 var days = 0;
63 var zi = date.getZone ();
64 if (zi != null) {
65 var offsets =  Clazz.newIntArray (2, 0);
66 if (Clazz.instanceOf (zi, jssun.util.calendar.ZoneInfo)) {
67 zoneOffset = (zi).getOffsets (millis, offsets);
68 } else {
69 zoneOffset = zi.getOffset (millis);
70 offsets[0] = zi.getRawOffset ();
71 offsets[1] = zoneOffset - offsets[0];
72 }days = Clazz.doubleToInt (zoneOffset / 86400000);
73 ms = zoneOffset % 86400000;
74 saving = offsets[1];
75 }date.setZoneOffset (zoneOffset);
76 date.setDaylightSaving (saving);
77 days += Clazz.doubleToInt (millis / 86400000);
78 ms += (millis % 86400000);
79 if (ms >= 86400000) {
80 ms -= 86400000;
81 ++days;
82 } else {
83 while (ms < 0) {
84 ms += 86400000;
85 --days;
86 }
87 }days += 719163;
88 this.getCalendarDateFromFixedDate (date, days);
89 this.setTimeOfDay (date, ms);
90 date.setLeapYear (this.isLeapYear (date));
91 date.setNormalized (true);
92 return date;
93 }, "~N,jssun.util.calendar.CalendarDate");
94 Clazz.overrideMethod (c$, "getTime", 
95 function (date) {
96 var gd = this.getFixedDate (date);
97 var ms = (gd - 719163) * 86400000 + this.getTimeOfDay (date);
98 var zoneOffset = 0;
99 var zi = date.getZone ();
100 if (zi != null) {
101 if (date.isNormalized ()) {
102 return ms - date.getZoneOffset ();
103 }var offsets =  Clazz.newIntArray (2, 0);
104 if (date.isStandardTime ()) {
105 if (Clazz.instanceOf (zi, jssun.util.calendar.ZoneInfo)) {
106 (zi).getOffsetsByStandard (ms, offsets);
107 zoneOffset = offsets[0];
108 } else {
109 zoneOffset = zi.getOffset (ms - zi.getRawOffset ());
110 }} else {
111 if (Clazz.instanceOf (zi, jssun.util.calendar.ZoneInfo)) {
112 zoneOffset = (zi).getOffsetsByWall (ms, offsets);
113 } else {
114 zoneOffset = zi.getOffset (ms - zi.getRawOffset ());
115 }}}ms -= zoneOffset;
116 this.getCalendarDate (ms, date);
117 return ms;
118 }, "jssun.util.calendar.CalendarDate");
119 Clazz.defineMethod (c$, "getTimeOfDay", 
120 function (date) {
121 var fraction = date.getTimeOfDay ();
122 if (fraction != -9223372036854775808) {
123 return fraction;
124 }fraction = this.getTimeOfDayValue (date);
125 date.setTimeOfDay (fraction);
126 return fraction;
127 }, "jssun.util.calendar.CalendarDate");
128 Clazz.defineMethod (c$, "getTimeOfDayValue", 
129 function (date) {
130 var fraction = date.getHours ();
131 fraction *= 60;
132 fraction += date.getMinutes ();
133 fraction *= 60;
134 fraction += date.getSeconds ();
135 fraction *= 1000;
136 fraction += date.getMillis ();
137 return fraction;
138 }, "jssun.util.calendar.CalendarDate");
139 Clazz.overrideMethod (c$, "setTimeOfDay", 
140 function (cdate, fraction) {
141 if (fraction < 0) {
142 throw  new IllegalArgumentException ();
143 }var normalizedState = cdate.isNormalized ();
144 var time = fraction;
145 var hours = Clazz.doubleToInt (time / 3600000);
146 time %= 3600000;
147 var minutes = Clazz.doubleToInt (time / 60000);
148 time %= 60000;
149 var seconds = Clazz.doubleToInt (time / 1000);
150 time %= 1000;
151 cdate.setHours (hours);
152 cdate.setMinutes (minutes);
153 cdate.setSeconds (seconds);
154 cdate.setMillis (time);
155 cdate.setTimeOfDay (fraction);
156 if (hours < 24 && normalizedState) {
157 cdate.setNormalized (normalizedState);
158 }return cdate;
159 }, "jssun.util.calendar.CalendarDate,~N");
160 Clazz.overrideMethod (c$, "getWeekLength", 
161 function () {
162 return 7;
163 });
164 Clazz.overrideMethod (c$, "getNthDayOfWeek", 
165 function (nth, dayOfWeek, date) {
166 var ndate = date.clone ();
167 this.normalize (ndate);
168 var fd = this.getFixedDate (ndate);
169 var nfd;
170 if (nth > 0) {
171 nfd = 7 * nth + jssun.util.calendar.AbstractCalendar.getDayOfWeekDateBefore (fd, dayOfWeek);
172 } else {
173 nfd = 7 * nth + jssun.util.calendar.AbstractCalendar.getDayOfWeekDateAfter (fd, dayOfWeek);
174 }this.getCalendarDateFromFixedDate (ndate, nfd);
175 return ndate;
176 }, "~N,~N,jssun.util.calendar.CalendarDate");
177 c$.getDayOfWeekDateBefore = Clazz.defineMethod (c$, "getDayOfWeekDateBefore", 
178 function (fixedDate, dayOfWeek) {
179 return jssun.util.calendar.AbstractCalendar.getDayOfWeekDateOnOrBefore (fixedDate - 1, dayOfWeek);
180 }, "~N,~N");
181 c$.getDayOfWeekDateAfter = Clazz.defineMethod (c$, "getDayOfWeekDateAfter", 
182 function (fixedDate, dayOfWeek) {
183 return jssun.util.calendar.AbstractCalendar.getDayOfWeekDateOnOrBefore (fixedDate + 7, dayOfWeek);
184 }, "~N,~N");
185 c$.getDayOfWeekDateOnOrBefore = Clazz.defineMethod (c$, "getDayOfWeekDateOnOrBefore", 
186 function (fixedDate, dayOfWeek) {
187 var fd = fixedDate - (dayOfWeek - 1);
188 if (fd >= 0) {
189 return fixedDate - (fd % 7);
190 }return fixedDate - jssun.util.calendar.CalendarUtils.mod (fd, 7);
191 }, "~N,~N");
192 Clazz.defineMethod (c$, "validateTime", 
193 function (date) {
194 var t = date.getHours ();
195 if (t < 0 || t >= 24) {
196 return false;
197 }t = date.getMinutes ();
198 if (t < 0 || t >= 60) {
199 return false;
200 }t = date.getSeconds ();
201 if (t < 0 || t >= 60) {
202 return false;
203 }t = date.getMillis ();
204 if (t < 0 || t >= 1000) {
205 return false;
206 }return true;
207 }, "jssun.util.calendar.CalendarDate");
208 Clazz.defineMethod (c$, "normalizeTime", 
209 function (date) {
210 var fraction = this.getTimeOfDay (date);
211 var days = 0;
212 if (fraction >= 86400000) {
213 days = Clazz.doubleToInt (fraction / 86400000);
214 fraction %= 86400000;
215 } else if (fraction < 0) {
216 days = jssun.util.calendar.CalendarUtils.floorDivide (fraction, 86400000);
217 if (days != 0) {
218 fraction -= 86400000 * days;
219 }}if (days != 0) {
220 date.setTimeOfDay (fraction);
221 }date.setMillis ((fraction % 1000));
222 fraction = Clazz.doubleToInt (fraction / 1000);
223 date.setSeconds ((fraction % 60));
224 fraction = Clazz.doubleToInt (fraction / 60);
225 date.setMinutes ((fraction % 60));
226 date.setHours ((Clazz.doubleToInt (fraction / 60)));
227 return days;
228 }, "jssun.util.calendar.CalendarDate");
229 Clazz.defineStatics (c$,
230 "SECOND_IN_MILLIS", 1000,
231 "MINUTE_IN_MILLIS", 60000,
232 "HOUR_IN_MILLIS", 3600000,
233 "DAY_IN_MILLIS", 86400000,
234 "EPOCH_OFFSET", 719163);
235 });