001 
002 /*
003  *  JScripter Simulation 1.0 - For Java To Script
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package org.jscripter.sim;
021 
022 import java.text.DateFormat;
023 import java.text.ParseException;
024 import java.util.Calendar;
025 import java.util.Date;
026 import java.util.GregorianCalendar;
027 
028 import org.jscripter.sim.core.Core;
029 
030 
031 import js.DateLike;
032 import js.core.JsDate;
033 import js.core.JsGlobal;
034 
035 final class SimDateLike implements DateLike
036 {
037     private final Calendar cal;
038 
039     public SimDateLike() {
040         cal = new GregorianCalendar();
041         cal.setTime(new Date());
042     }
043     public SimDateLike(Number n) {
044         cal = new GregorianCalendar();
045         cal.setTime(new Date(n.longValue()));
046     }
047     public SimDateLike(String s) {
048         cal = new GregorianCalendar();
049         try {
050             cal.setTime(DateFormat.getInstance().parse(s));
051         } catch (ParseException e) {
052             e.printStackTrace();
053         }
054     }
055 
056     //@Override
057     public final Number getDate() {
058         return cal.get(Calendar.DATE);
059     }
060 
061     //@Override
062     public final Number getDay() {
063         return cal.get(Calendar.DAY_OF_WEEK);
064     }
065 
066     //@Override
067     public final Number getFullYear() {
068         return cal.get(Calendar.YEAR);
069     }
070 
071     //@Override
072     public final Number getHours() {
073         return cal.get(Calendar.HOUR_OF_DAY);
074     }
075 
076     //@Override
077     public final Number getMilliseconds() {
078         return cal.get(Calendar.MILLISECOND);
079     }
080 
081     //@Override
082     public final Number getMinutes() {
083         return cal.get(Calendar.MINUTE);
084     }
085 
086     //@Override
087     public final Number getMonth() {
088         return cal.get(Calendar.MONTH);
089     }
090 
091     //@Override
092     public final Number getSeconds() {
093         return cal.get(Calendar.SECOND);
094     }
095 
096     //@Override
097     public final Number getTime() {
098         return cal.getTimeInMillis();
099     }
100 
101     //@Override
102     public final Number getTimezoneOffset() {
103         return cal.get(Calendar.ZONE_OFFSET);
104     }
105 
106     //@Override
107     public final Number getUTCDate() {
108         return null;
109     }
110 
111     //@Override
112     public final Number getUTCDay() {
113         // TODO Auto-generated method stub
114         return null;
115     }
116 
117     //@Override
118     public final Number getUTCFullYear() {
119         // TODO Auto-generated method stub
120         return null;
121     }
122 
123     //@Override
124     public final Number getUTCHours() {
125         // TODO Auto-generated method stub
126         return null;
127     }
128 
129     //@Override
130     public final Number getUTCMilliseconds() {
131         // TODO Auto-generated method stub
132         return null;
133     }
134 
135     //@Override
136     public final Number getUTCMinutes() {
137         // TODO Auto-generated method stub
138         return null;
139     }
140 
141     //@Override
142     public final Number getUTCMonth() {
143         // TODO Auto-generated method stub
144         return null;
145     }
146 
147     //@Override
148     public final Number getUTCSeconds() {
149         // TODO Auto-generated method stub
150         return null;
151     }
152 
153     //@Override
154     public final Number setDate(Object day) {
155         cal.set(Calendar.DATE, SimUtil.getNumber(day).intValue());
156         return getTime();
157     }
158 
159     //@Override
160     public final Number setFullYear(Object yr) {
161         cal.set(Calendar.YEAR, SimUtil.getNumber(yr).intValue());
162         return getTime();
163     }
164 
165     //@Override
166     public final Number setFullYear(Object yr, Object mo) {
167         cal.set(Calendar.YEAR , SimUtil.getNumber(yr).intValue());
168         cal.set(Calendar.MONTH, SimUtil.getNumber(mo).intValue());
169         return getTime();
170     }
171 
172     //@Override
173     public final Number setFullYear(Object yr, Object mo, Object day) {
174         cal.set(
175                 SimUtil.getNumber(yr ).intValue(),
176                 SimUtil.getNumber(mo ).intValue(),
177                 SimUtil.getNumber(day).intValue()
178         );
179         return getTime();
180     }
181 
182     //@Override
183     public final Number setHours(Object hr) {
184         cal.set(Calendar.HOUR, SimUtil.getNumber(hr).intValue());
185         return getTime();
186     }
187 
188     //@Override
189     public final Number setHours(Object hr, Object min) {
190         cal.set(Calendar.HOUR  , SimUtil.getNumber(hr ).intValue());
191         cal.set(Calendar.MINUTE, SimUtil.getNumber(min).intValue());
192         return getTime();
193     }
194 
195     //@Override
196     public final Number setHours(Object hr, Object min, Object sec) {
197         cal.set(Calendar.HOUR  , SimUtil.getNumber(hr ).intValue());
198         cal.set(Calendar.MINUTE, SimUtil.getNumber(min).intValue());
199         cal.set(Calendar.SECOND, SimUtil.getNumber(sec).intValue());
200         return getTime();
201     }
202 
203     //@Override
204     public final Number setHours(Object hr, Object min, Object sec, Object ms) {
205         cal.set(Calendar.HOUR       , SimUtil.getNumber(hr ).intValue());
206         cal.set(Calendar.MINUTE     , SimUtil.getNumber(min).intValue());
207         cal.set(Calendar.SECOND     , SimUtil.getNumber(sec).intValue());
208         cal.set(Calendar.MILLISECOND, SimUtil.getNumber(ms).intValue());
209         return getTime();
210     }
211 
212     //@Override
213     public final Number setMilliseconds(Object ms) {
214         cal.set(Calendar.MILLISECOND, SimUtil.getNumber(ms).intValue());
215         return getTime();
216     }
217 
218     //@Override
219     public final Number setMinutes(Object min) {
220         cal.set(Calendar.MINUTE, SimUtil.getNumber(min).intValue());
221         return getTime();
222     }
223 
224     //@Override
225     public final Number setMinutes(Object min, Object sec) {
226         cal.set(Calendar.MINUTE, SimUtil.getNumber(min).intValue());
227         cal.set(Calendar.SECOND, SimUtil.getNumber(sec).intValue());
228         return getTime();
229     }
230 
231     //@Override
232     public final Number setMinutes(Object min, Object sec, Object ms) {
233         cal.set(Calendar.MINUTE     , SimUtil.getNumber(min).intValue());
234         cal.set(Calendar.SECOND     , SimUtil.getNumber(sec).intValue());
235         cal.set(Calendar.MILLISECOND, SimUtil.getNumber(ms).intValue());
236         return getTime();
237     }
238 
239     //@Override
240     public final Number setMonth(Object mo) {
241         cal.set(Calendar.MONTH, SimUtil.getNumber(mo).intValue());
242         return getTime();
243     }
244 
245     //@Override
246     public final Number setMonth(Object mo, Object day) {
247         cal.set(Calendar.MONTH, SimUtil.getNumber(mo ).intValue());
248         cal.set(Calendar.DATE , SimUtil.getNumber(day).intValue());
249         return getTime();
250     }
251 
252     //@Override
253     public final Number setSeconds(Object sec) {
254         cal.set(Calendar.SECOND     , SimUtil.getNumber(sec).intValue());
255         return getTime();
256     }
257 
258     //@Override
259     public final Number setSeconds(Object sec, Object ms) {
260         cal.set(Calendar.SECOND     , SimUtil.getNumber(sec).intValue());
261         cal.set(Calendar.MILLISECOND, SimUtil.getNumber(ms).intValue());
262         return getTime();
263     }
264 
265     //@Override
266     public final Number setTime(Object time) {
267         cal.setTimeInMillis(SimUtil.getNumber(time).longValue());
268         return getTime();
269     }
270 
271     //@Override
272     public final Number setUTCDate(Object day) {
273         // TODO Auto-generated method stub
274         return null;
275     }
276 
277     //@Override
278     public final Number setUTCFullYear(Object yr) {
279         // TODO Auto-generated method stub
280         return null;
281     }
282 
283     //@Override
284     public final Number setUTCFullYear(Object yr, Object mo) {
285         // TODO Auto-generated method stub
286         return null;
287     }
288 
289     //@Override
290     public final Number setUTCFullYear(Object yr, Object mo, Object day) {
291         // TODO Auto-generated method stub
292         return null;
293     }
294 
295     //@Override
296     public final Number setUTCHours(Object hr) {
297         // TODO Auto-generated method stub
298         return null;
299     }
300 
301     //@Override
302     public final Number setUTCHours(Object hr, Object min) {
303         // TODO Auto-generated method stub
304         return null;
305     }
306 
307     //@Override
308     public final Number setUTCHours(Object hr, Object min, Object sec) {
309         // TODO Auto-generated method stub
310         return null;
311     }
312 
313     //@Override
314     public final Number setUTCHours(Object hr, Object min, Object sec, Object ms) {
315         // TODO Auto-generated method stub
316         return null;
317     }
318 
319     //@Override
320     public final Number setUTCMilliseconds(Object ms) {
321         // TODO Auto-generated method stub
322         return null;
323     }
324 
325     //@Override
326     public final Number setUTCMinutes(Object min) {
327         // TODO Auto-generated method stub
328         return null;
329     }
330 
331     //@Override
332     public final Number setUTCMinutes(Object min, Object sec) {
333         // TODO Auto-generated method stub
334         return null;
335     }
336 
337     //@Override
338     public final Number setUTCMinutes(Object min, Object sec, Object ms) {
339         // TODO Auto-generated method stub
340         return null;
341     }
342 
343     //@Override
344     public final Number setUTCMonth(Object mo) {
345         // TODO Auto-generated method stub
346         return null;
347     }
348 
349     //@Override
350     public final Number setUTCMonth(Object mo, Object day) {
351         // TODO Auto-generated method stub
352         return null;
353     }
354 
355     //@Override
356     public final Number setUTCSeconds(Object sec) {
357         // TODO Auto-generated method stub
358         return null;
359     }
360 
361     //@Override
362     public final Number setUTCSeconds(Object sec, Object ms) {
363         // TODO Auto-generated method stub
364         return null;
365     }
366 
367     //@Override
368     public final String toDateString() {
369         return cal.getTime().toString();
370     }
371 
372     //@Override
373     public final String toLocaleDateString() {
374         return cal.getTime().toString();
375     }
376 
377     //@Override
378     public final String toLocaleString() {
379         return cal.getTime().toString();
380     }
381 
382     //@Override
383     public final String toLocaleTimeString() {
384         return cal.getTime().toString();
385     }
386 
387     //@Override
388     public final String toTimeString() {
389         return cal.getTime().toString();
390     }
391 
392     //@Override
393     public final String toString() {
394         return cal.getTime().toString();
395     }
396 
397     //@Override
398     public final String toUTCString() {
399         return cal.getTime().toString();
400     }
401 
402     //@Override
403     public final Number valueOf() {
404         return cal.getTime().getTime();
405     }
406 
407     //@Override
408     public final JsDate var() {
409         if (Core.isRunning()) {
410             return JsGlobal.Date.with().create(this);
411         } else {
412             return JsGlobal.Date.with().create(getTime());
413         }
414     }
415 }