001: /*
002: * @(#)date.java 1.2 04/12/06
003: *
004: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.lib;
010:
011: import pnuts.lang.PnutsFunction;
012: import pnuts.lang.Context;
013: import java.util.Calendar;
014: import java.util.TimeZone;
015: import java.util.Date;
016: import java.text.SimpleDateFormat;
017:
018: public class date extends PnutsFunction {
019:
020: final static String TIMEZONE = "pnuts$lib$timezone".intern();
021: final static String CALENDAR = "pnuts$lib$calendar".intern();
022: final static String CALENDAR_POOL = "pnuts$lib$calendar_pool"
023: .intern();
024:
025: public date() {
026: super ("date");
027: }
028:
029: public boolean defined(int nargs) {
030: return (nargs < 4 || nargs == 6);
031: }
032:
033: static TimeZone getTimeZone(Context context) {
034: TimeZone zone = (TimeZone) context.get(TIMEZONE);
035: if (zone == null) {
036: context.set(TIMEZONE, zone = TimeZone.getDefault());
037: }
038: return zone;
039: }
040:
041: static void setTimeZone(Context context, TimeZone zone) {
042: CalendarCache cc = (CalendarCache) context.get(CALENDAR_POOL);
043: if (cc != null) {
044: cc.reset();
045: }
046: context.set(TIMEZONE, zone);
047: context.set(CALENDAR, null);
048: DateTimeFormat.reset(context);
049: }
050:
051: static Calendar getCalendar(Context context) {
052: Calendar c;
053: synchronized (context) {
054: c = (Calendar) context.get(CALENDAR);
055: if (c == null) {
056: c = Calendar.getInstance(getTimeZone(context));
057: context.set(CALENDAR, c);
058: }
059: return c;
060: }
061: }
062:
063: static Calendar getCalendar(Date date, Context context) {
064: CalendarCache cc;
065: synchronized (context) {
066: cc = (CalendarCache) context.get(CALENDAR_POOL);
067: if (cc == null) {
068: context.set(CALENDAR_POOL, cc = new CalendarCache(
069: context));
070: }
071: }
072: return cc.get(date);
073: }
074:
075: protected Object exec(Object[] args, Context context) {
076: int nargs = args.length;
077: switch (nargs) {
078: case 0:
079: return new Date();
080: case 1: {
081: Object arg = args[0];
082: if (arg instanceof Date) {
083: return arg;
084: } else if (arg instanceof Number) {
085: return new Date(((Number) arg).longValue());
086: } else if (arg instanceof String) {
087: return DateTimeFormat.parse((String) arg, context);
088: } else {
089: throw new IllegalArgumentException(String.valueOf(arg));
090: }
091: }
092: case 2: {
093: Object arg0 = args[0];
094: Object arg1 = args[1];
095: if (arg0 instanceof String && arg1 instanceof String) {
096: return DateTimeFormat.parse((String) arg0,
097: (String) arg1, context);
098: } else {
099: throw new IllegalArgumentException(arg0 + ", " + arg1);
100: }
101: }
102: case 3: {
103: int year = ((Number) args[0]).intValue();
104: int month = ((Number) args[1]).intValue();
105: int day = ((Number) args[2]).intValue();
106: Date d = new Date(0L);
107: Calendar c = getCalendar(d, context);
108: c.set(year, month - 1, day, 0, 0, 0);
109: c.set(Calendar.MILLISECOND, 0);
110: d.setTime(c.getTimeInMillis());
111: return d;
112: }
113: case 6: {
114: int year = ((Number) args[0]).intValue();
115: int month = ((Number) args[1]).intValue();
116: int day = ((Number) args[2]).intValue();
117: int hour = ((Number) args[3]).intValue();
118: int minute = ((Number) args[4]).intValue();
119: int second = ((Number) args[5]).intValue();
120: Date d = new Date(0L);
121: Calendar c = getCalendar(d, context);
122: c.set(year, month - 1, day, hour, minute, second);
123: c.set(Calendar.MILLISECOND, 0);
124: d.setTime(c.getTimeInMillis());
125: return d;
126: }
127: default:
128: undefined(args, context);
129: return null;
130: }
131: }
132:
133: public String toString() {
134: return "function date(),(expr),(expr,pattern),(year,month,day),(year,month,day,hour,minute,second)";
135: }
136: }
|