comparison core/src/luan/modules/Time.luan @ 503:92c3d22745b8

make _ENV optional
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 20 May 2015 23:24:46 -0600
parents 7f7708e8fdd4
children fa281ee942c8
comparison
equal deleted inserted replaced
502:d3183a330ff5 503:92c3d22745b8
1 -- incomplete, will add as needed 1 -- incomplete, will add as needed
2 2
3 java() 3 java()
4 require "luan:String" 4 local Luan = require "luan:Luan"
5 local ipairs = Luan.ipairs
6 local error = Luan.error
5 local Table = require "luan:Table" 7 local Table = require "luan:Table"
6 local System = require "java:java.lang.System" 8 local System = require "java:java.lang.System"
7 local Calendar = require "java:java.util.Calendar" 9 local Calendar = require "java:java.util.Calendar"
8 local Date = require "java:java.util.Date" 10 local Date = require "java:java.util.Date"
9 local TimeZone = require "java:java.util.TimeZone" 11 local TimeZone = require "java:java.util.TimeZone"
10 local SimpleDateFormat = require "java:java.text.SimpleDateFormat" 12 local SimpleDateFormat = require "java:java.text.SimpleDateFormat"
11 13
14 local M = {}
12 15
13 function now() 16 function M.now()
14 return System.currentTimeMillis() 17 return System.currentTimeMillis()
15 end 18 end
16 19
17 -- add more as needed 20 -- add more as needed
18 local fields = { 21 local fields = {
19 year = Calendar.YEAR; 22 year = Calendar.YEAR;
20 month = Calendar.MONTH; 23 month = Calendar.MONTH;
21 day_of_month = Calendar.DAY_OF_MONTH; 24 day_of_month = Calendar.DAY_OF_MONTH;
22 } 25 }
23 26
24 function get( time, ... ) 27 function M.get( time, ... )
25 local cal = Calendar.getInstance() 28 local cal = Calendar.getInstance()
26 cal.setTimeInMillis(time) 29 cal.setTimeInMillis(time)
27 local rtn = {} 30 local rtn = {}
28 for i, v in ipairs{...} do 31 for i, v in ipairs{...} do
29 local fld = fields[v.lower()] 32 local fld = fields[v.lower()]
35 rtn[i] = n 38 rtn[i] = n
36 end 39 end
37 return Table.unpack(rtn) 40 return Table.unpack(rtn)
38 end 41 end
39 42
40 function format(time,pattern) 43 function M.format(time,pattern)
41 pattern = pattern or "yyyy-MM-dd HH:m:ss" 44 pattern = pattern or "yyyy-MM-dd HH:m:ss"
42 return SimpleDateFormat.new(pattern).format(Date.new(time)) 45 return SimpleDateFormat.new(pattern).format(Date.new(time))
43 end 46 end
44 47
45 function on( year, month, day, hour, minute, second, millis ) 48 function M.on( year, month, day, hour, minute, second, millis )
46 month = month - 1 49 month = month - 1
47 local cal = Calendar.getInstance() 50 local cal = Calendar.getInstance()
48 cal.setLenient(false) 51 cal.setLenient(false)
49 cal.set( year, month, day, hour or 0, minute or 0, second or 0 ) 52 cal.set( year, month, day, hour or 0, minute or 0, second or 0 )
50 cal.set( Calendar.MILLISECOND, millis or 0 ) 53 cal.set( Calendar.MILLISECOND, millis or 0 )
51 return cal.getTimeInMillis() 54 return cal.getTimeInMillis()
52 end 55 end
53 56
54 function period( days, hours, minutes, seconds, millis ) 57 function M.period( days, hours, minutes, seconds, millis )
55 local cal = Calendar.getInstance() 58 local cal = Calendar.getInstance()
56 cal.setTimeZone(TimeZone.getTimeZone("GMT")) 59 cal.setTimeZone(TimeZone.getTimeZone("GMT"))
57 days = days + 1 60 days = days + 1
58 cal.set( 1970, 0, days, hours or 0, minutes or 0, seconds or 0 ) 61 cal.set( 1970, 0, days, hours or 0, minutes or 0, seconds or 0 )
59 cal.set( Calendar.MILLISECOND, millis or 0 ) 62 cal.set( Calendar.MILLISECOND, millis or 0 )
60 return cal.getTimeInMillis() 63 return cal.getTimeInMillis()
61 end 64 end
62 65
63 function parse( pattern, source ) 66 function M.parse( pattern, source )
64 return SimpleDateFormat.new(pattern).parse(source).getTime() 67 return SimpleDateFormat.new(pattern).parse(source).getTime()
65 end 68 end
69
70 return M