comparison src/luan/modules/Time.luan @ 1098:bae624e455e2

add Time.time_zone
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 29 Mar 2017 19:54:45 -0600
parents b2e4a22ffa5d
children 1f9d34a6f308
comparison
equal deleted inserted replaced
1097:b2e4a22ffa5d 1098:bae624e455e2
41 rtn[i] = n 41 rtn[i] = n
42 end 42 end
43 return Table.unpack(rtn) 43 return Table.unpack(rtn)
44 end 44 end
45 45
46 -- Time.time_zone set for other than default
47
48 function Time.get_time_zone()
49 local tz = Time.time_zone and TimeZone.getTimeZone(Time.time_zone) or TimeZone.getDefault()
50 return tz.getID().." - "..tz.getDisplayName()
51 end
52
53 local function set_time_zone(fmt)
54 Time.time_zone and fmt.setTimeZone( TimeZone.getTimeZone(Time.time_zone) )
55 end
56
46 function Time.format(time,pattern) 57 function Time.format(time,pattern)
47 time = assert_long(time) 58 time = assert_long(time)
48 pattern = pattern or "yyyy-MM-dd HH:mm:ss" 59 pattern = pattern or "yyyy-MM-dd HH:mm:ss"
49 return SimpleDateFormat.new(pattern).format(Date.new(time)) 60 local fmt = SimpleDateFormat.new(pattern)
61 set_time_zone(fmt)
62 return fmt.format(Date.new(time))
50 end 63 end
51 64
52 function Time.on( year, month, day, hour, minute, second, millis ) 65 function Time.on( year, month, day, hour, minute, second, millis )
53 month = month - 1 66 month = month - 1
54 local cal = Calendar.getInstance() 67 local cal = Calendar.getInstance()
67 cal.set( Calendar.MILLISECOND, t.millis or 0 ) 80 cal.set( Calendar.MILLISECOND, t.millis or 0 )
68 return cal.getTimeInMillis() 81 return cal.getTimeInMillis()
69 end 82 end
70 83
71 function Time.parse( pattern, source ) 84 function Time.parse( pattern, source )
72 return SimpleDateFormat.new(pattern).parse(source).getTime() 85 local fmt = SimpleDateFormat.new(pattern)
86 set_time_zone(fmt)
87 return fmt.parse(source).getTime()
73 end 88 end
74 89
75 90
76 local count_times = { 91 local count_times = {
77 days = Time.period{days=1} 92 days = Time.period{days=1}