annotate core/src/luan/modules/Time.luan @ 283:b669cdaf54b7

add URL post; add Http.request.query_string; add web_run.form(); git-svn-id: https://luan-java.googlecode.com/svn/trunk@284 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 02 Dec 2014 03:34:04 +0000
parents 073044e3ac03
children 9a0cc6d9b27b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
254
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
1 -- incomplete, will add as needed
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
2
264
9e0d4452e649 implement URL style module names
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 256
diff changeset
3 import "luan:String"
9e0d4452e649 implement URL style module names
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 256
diff changeset
4 import "luan:Table"
9e0d4452e649 implement URL style module names
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 256
diff changeset
5 import "luan:Java"
272
eb27e765affb rename Io.protocols to Io.schemes and "class:" to "java:"
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 264
diff changeset
6 import "java:java.lang.System"
eb27e765affb rename Io.protocols to Io.schemes and "class:" to "java:"
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 264
diff changeset
7 import "java:java.util.Calendar"
eb27e765affb rename Io.protocols to Io.schemes and "class:" to "java:"
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 264
diff changeset
8 import "java:java.util.Date"
eb27e765affb rename Io.protocols to Io.schemes and "class:" to "java:"
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 264
diff changeset
9 import "java:java.text.SimpleDateFormat"
254
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
10
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
11
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
12 function now()
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
13 return System.currentTimeMillis()
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
14 end
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
15
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
16 -- add more as needed
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
17 local fields = {
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
18 year = Calendar.YEAR;
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
19 month = Calendar.MONTH;
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
20 day_of_month = Calendar.DAY_OF_MONTH;
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
21 }
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
22
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
23 function get( time, ... )
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
24 local cal = Calendar.getInstance()
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
25 cal.setTimeInMillis(time)
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
26 local rtn = {}
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
27 for i, v in ipairs{...} do
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
28 local fld = fields[v.lower()]
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
29 fld or error("invalid field: "+v)
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
30 local n = cal.get(fld)
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
31 if fld == "month" then
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
32 n = n + 1
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
33 end
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
34 rtn[i] = n
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
35 end
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
36 return Table.unpack(rtn)
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
37 end
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
38
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
39 function format(time,pattern)
273
073044e3ac03 fix mmake.sh and multipart mail
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 272
diff changeset
40 pattern = pattern or "yyyy-MM-dd HH:m:ss"
254
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
41 return SimpleDateFormat.new(pattern).format(Date.new(time))
e0fb8a49e031 add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
42 end
256
15122d724ce4 more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 254
diff changeset
43
15122d724ce4 more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 254
diff changeset
44 function on( year, month, day, hour, minute, second, millis )
15122d724ce4 more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 254
diff changeset
45 month = month - 1
15122d724ce4 more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 254
diff changeset
46 local cal = Calendar.getInstance()
15122d724ce4 more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 254
diff changeset
47 cal.set( year, month, day, hour or 0, minute or 0, second or 0 )
15122d724ce4 more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 254
diff changeset
48 cal.set( Calendar.MILLISECOND, millis or 0 )
15122d724ce4 more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 254
diff changeset
49 return cal.getTimeInMillis()
15122d724ce4 more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 254
diff changeset
50 end
15122d724ce4 more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 254
diff changeset
51
15122d724ce4 more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 254
diff changeset
52 function parse( pattern, source )
15122d724ce4 more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 254
diff changeset
53 return SimpleDateFormat.new(pattern).parse(source).getTime()
15122d724ce4 more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 254
diff changeset
54 end