Skip to content

Commit 401d426

Browse files
committed
Добавлен модуль date
1 parent 059b4c4 commit 401d426

File tree

4 files changed

+355
-0
lines changed

4 files changed

+355
-0
lines changed

program.own

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,13 @@ println 1 :: 2 :: 3
243243
println "\u042a"
244244

245245
include "visitor.own"
246+
247+
248+
use "date"
249+
d = newDate();
250+
println d
251+
println formatDate(d)
252+
println formatDate(d, newFormat("yyyy-MM-dd HH:mm:ss, EEEE"))
253+
println parseDate("2016/05/10", newFormat("yyyy/MM/dd"))
254+
println toTimestamp(d)
255+
println newDate(toTimestamp(d) - 100000)
Lines changed: 304 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
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+
}

src/com/annimon/ownlang/lib/modules/package-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@Modules(modules = {
22
canvas.class,
33
canvasfx.class,
4+
date.class,
45
files.class,
56
functional.class,
67
http.class,

tests.own

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use "ounit"
22
use "functional"
3+
use "date"
34

45
def testAdditionOnNumbers() {
56
assertEquals(6, 0 + 1 + 2 + 3)
@@ -67,4 +68,43 @@ def testFunctionalChain() {
6768
assertEquals([8,6,4,2], result)
6869
}
6970

71+
def testDateParse() {
72+
d = parseDate("2016/05/10", newFormat("yyyy/MM/dd"))
73+
assertEquals(2016, d.year)
74+
assertEquals(4, d.month)
75+
assertEquals(10, d.day)
76+
assertEquals(0, d.hour)
77+
}
78+
79+
// --- Date
80+
def testNewDate() {
81+
d = newDate(2016, 04, 10)
82+
assertEquals(2016, d.year)
83+
assertEquals(4, d.month)
84+
assertEquals(10, d.day)
85+
assertEquals(0, d.hour)
86+
assertEquals(0, d.minute)
87+
assertEquals(0, d.second)
88+
assertEquals(0, d.millisecond)
89+
}
90+
91+
def testCompareDates() {
92+
assertTrue(newDate(2016, 04, 10) > newDate(2015, 03, 11))
93+
assertTrue(newDate(2012, 04, 10) < newDate(2015, 03, 11))
94+
assertTrue(newDate(2015, 03, 11, 0, 0, 0) == newDate(2015, 03, 11))
95+
}
96+
97+
def testDateFormat() {
98+
d = formatDate(newDate(2016, 04, 10), newFormat("yyyy/MM/dd HH:mm:ss"))
99+
assertEquals("2016/05/10 00:00:00", d)
100+
}
101+
102+
def testDateParse() {
103+
d = parseDate("2016/05/10", newFormat("yyyy/MM/dd"))
104+
assertEquals(2016, d.year)
105+
assertEquals(4, d.month)
106+
assertEquals(10, d.day)
107+
assertEquals(0, d.hour)
108+
}
109+
70110
println runTests()

0 commit comments

Comments
 (0)