Mercurial Hosting > luan
annotate core/src/luan/modules/Time.luan @ 615:abc3198c86dd
fix tail recursion bug;
add Hosting.exists();
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 17 Dec 2015 01:53:48 -0700 |
parents | b4f3dbe1c6e3 |
children | ca169567ce07 |
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 |
321
7f7708e8fdd4
remove import statement
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
320
diff
changeset
|
3 java() |
503 | 4 local Luan = require "luan:Luan" |
5 local error = Luan.error | |
610
b4f3dbe1c6e3
add Rpc and change Hosting to use Rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
587
diff
changeset
|
6 local ipairs = Luan.ipairs or error() |
321
7f7708e8fdd4
remove import statement
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
320
diff
changeset
|
7 local Table = require "luan:Table" |
7f7708e8fdd4
remove import statement
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
320
diff
changeset
|
8 local System = require "java:java.lang.System" |
7f7708e8fdd4
remove import statement
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
320
diff
changeset
|
9 local Calendar = require "java:java.util.Calendar" |
7f7708e8fdd4
remove import statement
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
320
diff
changeset
|
10 local Date = require "java:java.util.Date" |
7f7708e8fdd4
remove import statement
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
320
diff
changeset
|
11 local TimeZone = require "java:java.util.TimeZone" |
7f7708e8fdd4
remove import statement
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
320
diff
changeset
|
12 local SimpleDateFormat = require "java:java.text.SimpleDateFormat" |
254
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
13 |
503 | 14 local M = {} |
254
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
15 |
503 | 16 function M.now() |
254
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
17 return System.currentTimeMillis() |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
18 end |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
19 |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
20 -- add more as needed |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
21 local fields = { |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
22 year = Calendar.YEAR; |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
23 month = Calendar.MONTH; |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
24 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
|
25 } |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
26 |
503 | 27 function M.get( time, ... ) |
254
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
28 local cal = Calendar.getInstance() |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
29 cal.setTimeInMillis(time) |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
30 local rtn = {} |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
31 for i, v in ipairs{...} do |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
32 local fld = fields[v.lower()] |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
33 fld or error("invalid field: "+v) |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
34 local n = cal.get(fld) |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
35 if fld == "month" then |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
36 n = n + 1 |
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 rtn[i] = n |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
39 end |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
40 return Table.unpack(rtn) |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
41 end |
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
42 |
503 | 43 function M.format(time,pattern) |
587 | 44 pattern = pattern or "yyyy-MM-dd HH:mm:ss" |
254
e0fb8a49e031
add Time.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
45 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
|
46 end |
256
15122d724ce4
more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
254
diff
changeset
|
47 |
503 | 48 function M.on( year, month, day, hour, minute, second, millis ) |
256
15122d724ce4
more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
254
diff
changeset
|
49 month = month - 1 |
15122d724ce4
more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
254
diff
changeset
|
50 local cal = Calendar.getInstance() |
290
9a0cc6d9b27b
add Time.period();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
283
diff
changeset
|
51 cal.setLenient(false) |
256
15122d724ce4
more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
254
diff
changeset
|
52 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
|
53 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
|
54 return cal.getTimeInMillis() |
15122d724ce4
more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
254
diff
changeset
|
55 end |
15122d724ce4
more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
254
diff
changeset
|
56 |
503 | 57 function M.period( days, hours, minutes, seconds, millis ) |
290
9a0cc6d9b27b
add Time.period();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
283
diff
changeset
|
58 local cal = Calendar.getInstance() |
9a0cc6d9b27b
add Time.period();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
283
diff
changeset
|
59 cal.setTimeZone(TimeZone.getTimeZone("GMT")) |
9a0cc6d9b27b
add Time.period();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
283
diff
changeset
|
60 days = days + 1 |
9a0cc6d9b27b
add Time.period();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
283
diff
changeset
|
61 cal.set( 1970, 0, days, hours or 0, minutes or 0, seconds or 0 ) |
9a0cc6d9b27b
add Time.period();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
283
diff
changeset
|
62 cal.set( Calendar.MILLISECOND, millis or 0 ) |
9a0cc6d9b27b
add Time.period();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
283
diff
changeset
|
63 return cal.getTimeInMillis() |
9a0cc6d9b27b
add Time.period();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
283
diff
changeset
|
64 end |
9a0cc6d9b27b
add Time.period();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
283
diff
changeset
|
65 |
503 | 66 function M.parse( pattern, source ) |
256
15122d724ce4
more Time functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
254
diff
changeset
|
67 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
|
68 end |
503 | 69 |
70 return M |