comparison src/luan/modules/Time.luan @ 1546:2b4471d310b3

better time zone handling
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 22 Sep 2020 21:41:22 -0600
parents 8d95711f6615
children
comparison
equal deleted inserted replaced
1545:f46b81048a80 1546:2b4471d310b3
42 rtn[i] = n 42 rtn[i] = n
43 end 43 end
44 return Table.unpack(rtn) 44 return Table.unpack(rtn)
45 end 45 end
46 46
47 -- Time.time_zone set for other than default 47 function Time.get_time_zone(name)
48 48 local tz = name and TimeZone.getTimeZone(name) or TimeZone.getDefault()
49 function Time.get_time_zone()
50 local tz = Time.time_zone and TimeZone.getTimeZone(Time.time_zone) or TimeZone.getDefault()
51 return tz.getID().." - "..tz.getDisplayName() 49 return tz.getID().." - "..tz.getDisplayName()
52 end 50 end
53 51
54 local function set_time_zone(fmt) 52 local function set_time_zone(fmt,time_zone)
55 Time.time_zone and fmt.setTimeZone( TimeZone.getTimeZone(Time.time_zone) ) 53 time_zone and fmt.setTimeZone( TimeZone.getTimeZone(time_zone) )
56 end 54 end
57 55
58 function Time.format(time,pattern) 56 function Time.format( time, pattern, time_zone )
59 time = long(time) 57 time = long(time)
60 pattern = pattern or "yyyy-MM-dd HH:mm:ss" 58 pattern = pattern or "yyyy-MM-dd HH:mm:ss"
61 local fmt = SimpleDateFormat.new(pattern) 59 local fmt = SimpleDateFormat.new(pattern)
62 set_time_zone(fmt) 60 set_time_zone(fmt,time_zone)
63 return fmt.format(Date.new(time)) 61 return fmt.format(Date.new(time))
64 end 62 end
65 63
66 function Time.on( year, month, day, hour, minute, second, millis ) 64 function Time.on( year, month, day, hour, minute, second, millis )
67 month = month - 1 65 month = month - 1
80 cal.set( 1970, 0, days, t.hours or 0, t.minutes or 0, t.seconds or 0 ) 78 cal.set( 1970, 0, days, t.hours or 0, t.minutes or 0, t.seconds or 0 )
81 cal.set( Calendar.MILLISECOND, t.millis or 0 ) 79 cal.set( Calendar.MILLISECOND, t.millis or 0 )
82 return cal.getTimeInMillis() 80 return cal.getTimeInMillis()
83 end 81 end
84 82
85 function Time.parse( pattern, source ) 83 function Time.parse( pattern, source, time_zone )
86 local fmt = SimpleDateFormat.new(pattern) 84 local fmt = SimpleDateFormat.new(pattern)
87 set_time_zone(fmt) 85 set_time_zone(fmt,time_zone)
88 return fmt.parse(source).getTime() 86 return fmt.parse(source).getTime()
89 end 87 end
90 88
91 89
92 local count_times = { 90 local count_times = {