|
| 1 | +package com.annimon.ownlang.lib.modules; |
| 2 | + |
| 3 | +import com.annimon.ownlang.exceptions.TypeException; |
| 4 | +import com.annimon.ownlang.lib.*; |
| 5 | +import java.text.DateFormat; |
| 6 | +import java.text.ParseException; |
| 7 | +import java.text.SimpleDateFormat; |
| 8 | +import java.util.Calendar; |
| 9 | +import java.util.Date; |
| 10 | +import java.util.Locale; |
| 11 | + |
| 12 | +/** |
| 13 | + * |
| 14 | + * @author aNNiMON |
| 15 | + */ |
| 16 | +public final class date implements Module { |
| 17 | + |
| 18 | + private static final int DATE_DATE_FORMAT = 9829; |
| 19 | + |
| 20 | + private static final StringValue |
| 21 | + YEAR = new StringValue("year"), |
| 22 | + MONTH = new StringValue("month"), |
| 23 | + DAY = new StringValue("day"), |
| 24 | + HOUR = new StringValue("hour"), |
| 25 | + MINUTE = new StringValue("minute"), |
| 26 | + SECOND = new StringValue("second"), |
| 27 | + MILLISECOND = new StringValue("millisecond"); |
| 28 | + |
| 29 | + @Override |
| 30 | + public void init() { |
| 31 | + Variables.set("STYLE_FULL", NumberValue.of(DateFormat.FULL)); |
| 32 | + Variables.set("STYLE_LONG", NumberValue.of(DateFormat.LONG)); |
| 33 | + Variables.set("STYLE_MEDIUM", NumberValue.of(DateFormat.MEDIUM)); |
| 34 | + Variables.set("STYLE_SHORT", NumberValue.of(DateFormat.SHORT)); |
| 35 | + |
| 36 | + Functions.set("newDate", new date_newDate()); |
| 37 | + Functions.set("newFormat", new date_newFormat()); |
| 38 | + Functions.set("formatDate", new date_format()); |
| 39 | + Functions.set("parseDate", new date_parse()); |
| 40 | + Functions.set("toTimestamp", new date_toTimestamp()); |
| 41 | + } |
| 42 | + |
| 43 | + //<editor-fold defaultstate="collapsed" desc="Values"> |
| 44 | + private static class DateValue extends MapValue { |
| 45 | + |
| 46 | + private static DateValue from(Date date) { |
| 47 | + final Calendar calendar = Calendar.getInstance(); |
| 48 | + calendar.setTime(date); |
| 49 | + return from(calendar); |
| 50 | + } |
| 51 | + |
| 52 | + private static DateValue from(Calendar calendar) { |
| 53 | + final DateValue value = new DateValue(calendar); |
| 54 | + value.set(YEAR, NumberValue.of(calendar.get(Calendar.YEAR))); |
| 55 | + value.set(MONTH, NumberValue.of(calendar.get(Calendar.MONTH))); |
| 56 | + value.set(DAY, NumberValue.of(calendar.get(Calendar.DAY_OF_MONTH))); |
| 57 | + value.set(HOUR, NumberValue.of(calendar.get(Calendar.HOUR))); |
| 58 | + value.set(MINUTE, NumberValue.of(calendar.get(Calendar.MINUTE))); |
| 59 | + value.set(SECOND, NumberValue.of(calendar.get(Calendar.SECOND))); |
| 60 | + value.set(MILLISECOND, NumberValue.of(calendar.get(Calendar.MILLISECOND))); |
| 61 | + return value; |
| 62 | + } |
| 63 | + |
| 64 | + private final Calendar value; |
| 65 | + |
| 66 | + private DateValue(Calendar value) { |
| 67 | + super(8); |
| 68 | + this.value = value; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Object raw() { |
| 73 | + return value; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public int asInt() { |
| 78 | + throw new TypeException("Cannot cast Date to integer"); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public double asNumber() { |
| 83 | + throw new TypeException("Cannot cast Date to number"); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public String asString() { |
| 88 | + return String.format("%04d-%02d-%02d %02d:%02d:%02d.%03d", |
| 89 | + get(YEAR).asInt(), get(MONTH).asInt(), get(DAY).asInt(), |
| 90 | + get(HOUR).asInt(), get(MINUTE).asInt(), get(SECOND).asInt(), |
| 91 | + get(MILLISECOND).asInt()); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public int compareTo(Value o) { |
| 96 | + if (o.type() == Types.MAP && (o.raw() instanceof Calendar)) { |
| 97 | + return value.compareTo((Calendar) o.raw()); |
| 98 | + } |
| 99 | + return asString().compareTo(o.asString()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private static class DateFormatValue implements Value { |
| 104 | + |
| 105 | + private final DateFormat value; |
| 106 | + |
| 107 | + public DateFormatValue(DateFormat value) { |
| 108 | + this.value = value; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public Object raw() { |
| 113 | + return value; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public int asInt() { |
| 118 | + throw new TypeException("Cannot cast DateFormat to integer"); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public double asNumber() { |
| 123 | + throw new TypeException("Cannot cast DateFormat to number"); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public String asString() { |
| 128 | + return value.toString(); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public int type() { |
| 133 | + return DATE_DATE_FORMAT; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public int compareTo(Value o) { |
| 138 | + return 0; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public String toString() { |
| 143 | + return "DateFormat " + value.toString(); |
| 144 | + } |
| 145 | + } |
| 146 | +//</editor-fold> |
| 147 | + |
| 148 | + private static class date_newDate implements Function { |
| 149 | + |
| 150 | + @Override |
| 151 | + public Value execute(Value... args) { |
| 152 | + final Calendar calendar = Calendar.getInstance(); |
| 153 | + calendar.clear(); |
| 154 | + switch (args.length) { |
| 155 | + case 0: |
| 156 | + // date() |
| 157 | + calendar.setTimeInMillis(System.currentTimeMillis()); |
| 158 | + break; |
| 159 | + case 1: |
| 160 | + // date(timestamp) |
| 161 | + // date("date") |
| 162 | + date(calendar, args[0]); |
| 163 | + break; |
| 164 | + case 2: |
| 165 | + // date("pattern", "date") |
| 166 | + date(calendar, args[0], args[1]); |
| 167 | + break; |
| 168 | + case 3: |
| 169 | + case 4: |
| 170 | + // date(year, month, day) |
| 171 | + calendar.set(args[0].asInt(), args[1].asInt(), args[2].asInt()); |
| 172 | + break; |
| 173 | + case 5: |
| 174 | + // date(year, month, day, hour, minute) |
| 175 | + calendar.set(args[0].asInt(), args[1].asInt(), args[2].asInt(), |
| 176 | + args[3].asInt(), args[4].asInt()); |
| 177 | + break; |
| 178 | + case 6: |
| 179 | + default: |
| 180 | + // date(year, month, day, hour, minute, second) |
| 181 | + calendar.set(args[0].asInt(), args[1].asInt(), args[2].asInt(), |
| 182 | + args[3].asInt(), args[4].asInt(), args[5].asInt()); |
| 183 | + break; |
| 184 | + } |
| 185 | + return DateValue.from(calendar); |
| 186 | + } |
| 187 | + |
| 188 | + private static void date(Calendar calendar, Value arg1) { |
| 189 | + if (arg1.type() == Types.NUMBER) { |
| 190 | + calendar.setTimeInMillis(((NumberValue) arg1).asLong()); |
| 191 | + return; |
| 192 | + } |
| 193 | + try { |
| 194 | + calendar.setTime(DateFormat.getDateTimeInstance().parse(arg1.asString())); |
| 195 | + } catch (ParseException ex) { } |
| 196 | + } |
| 197 | + |
| 198 | + private static void date(Calendar calendar, Value arg1, Value arg2) { |
| 199 | + if (arg1.type() == Types.NUMBER) { |
| 200 | + date(calendar, arg1); |
| 201 | + return; |
| 202 | + } |
| 203 | + try { |
| 204 | + calendar.setTime(new SimpleDateFormat(arg1.asString()).parse(arg2.asString())); |
| 205 | + } catch (ParseException ex) { } |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + private static class date_newFormat implements Function { |
| 210 | + |
| 211 | + @Override |
| 212 | + public Value execute(Value... args) { |
| 213 | + if (args.length == 0) { |
| 214 | + return new DateFormatValue(new SimpleDateFormat()); |
| 215 | + } |
| 216 | + if (args.length == 1) { |
| 217 | + if (args[0].type() == Types.STRING) { |
| 218 | + return new DateFormatValue(new SimpleDateFormat(args[0].asString())); |
| 219 | + } |
| 220 | + switch (args[0].asInt()) { |
| 221 | + case 0: |
| 222 | + return new DateFormatValue(DateFormat.getInstance()); |
| 223 | + case 1: |
| 224 | + return new DateFormatValue(DateFormat.getDateInstance()); |
| 225 | + case 2: |
| 226 | + return new DateFormatValue(DateFormat.getTimeInstance()); |
| 227 | + case 3: |
| 228 | + default: |
| 229 | + return new DateFormatValue(DateFormat.getDateTimeInstance()); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + if (args[0].type() == Types.STRING) { |
| 234 | + return new DateFormatValue(new SimpleDateFormat(args[0].asString(), new Locale(args[1].asString()))); |
| 235 | + } |
| 236 | + switch (args[0].asInt()) { |
| 237 | + case 0: |
| 238 | + return new DateFormatValue(DateFormat.getInstance()); |
| 239 | + case 1: |
| 240 | + return new DateFormatValue(DateFormat.getDateInstance(args[1].asInt())); |
| 241 | + case 2: |
| 242 | + return new DateFormatValue(DateFormat.getTimeInstance(args[1].asInt())); |
| 243 | + case 3: |
| 244 | + default: |
| 245 | + int dateStyle = args[1].asInt(); |
| 246 | + int timeStyle = (args.length > 2) ? args[2].asInt() : dateStyle; |
| 247 | + return new DateFormatValue(DateFormat.getDateTimeInstance(dateStyle, timeStyle)); |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + private static class date_parse implements Function { |
| 253 | + |
| 254 | + @Override |
| 255 | + public Value execute(Value... args) { |
| 256 | + Arguments.checkOrOr(1, 2, args.length); |
| 257 | + |
| 258 | + final DateFormat format; |
| 259 | + if (args.length == 1) { |
| 260 | + format = new SimpleDateFormat(); |
| 261 | + } else { |
| 262 | + if (args[1].type() != DATE_DATE_FORMAT) { |
| 263 | + throw new TypeException("DateFormat expected, found " + Types.typeToString(args[1].type())); |
| 264 | + } |
| 265 | + format = (DateFormat) args[1].raw(); |
| 266 | + } |
| 267 | + |
| 268 | + try { |
| 269 | + return DateValue.from(format.parse(args[0].asString())); |
| 270 | + } catch (ParseException ex) { |
| 271 | + throw new RuntimeException(ex); |
| 272 | + } |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + private static class date_format implements Function { |
| 277 | + |
| 278 | + @Override |
| 279 | + public Value execute(Value... args) { |
| 280 | + Arguments.checkOrOr(1, 2, args.length); |
| 281 | + |
| 282 | + final DateFormat format; |
| 283 | + if (args.length == 1) { |
| 284 | + format = new SimpleDateFormat(); |
| 285 | + } else { |
| 286 | + if (args[1].type() != DATE_DATE_FORMAT) { |
| 287 | + throw new TypeException("DateFormat expected, found " + Types.typeToString(args[1].type())); |
| 288 | + } |
| 289 | + format = (DateFormat) args[1].raw(); |
| 290 | + } |
| 291 | + |
| 292 | + return new StringValue(format.format( ((Calendar) args[0].raw()).getTime() )); |
| 293 | + } |
| 294 | + } |
| 295 | + |
| 296 | + private static class date_toTimestamp implements Function { |
| 297 | + |
| 298 | + @Override |
| 299 | + public Value execute(Value... args) { |
| 300 | + Arguments.check(1, args.length); |
| 301 | + return NumberValue.of(((Calendar) args[0].raw()).getTimeInMillis() ); |
| 302 | + } |
| 303 | + } |
| 304 | +} |
0 commit comments