changeset 1098:bae624e455e2

add Time.time_zone
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 29 Mar 2017 19:54:45 -0600
parents b2e4a22ffa5d
children a5406f076726
files src/luan/modules/Time.luan
diffstat 1 files changed, 17 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/Time.luan	Tue Mar 28 20:58:28 2017 -0600
+++ b/src/luan/modules/Time.luan	Wed Mar 29 19:54:45 2017 -0600
@@ -43,10 +43,23 @@
 	return Table.unpack(rtn)
 end
 
+-- Time.time_zone      set for other than default
+
+function Time.get_time_zone()
+	local tz = Time.time_zone and TimeZone.getTimeZone(Time.time_zone) or TimeZone.getDefault()
+	return tz.getID().." - "..tz.getDisplayName()
+end
+
+local function set_time_zone(fmt)
+	Time.time_zone and fmt.setTimeZone( TimeZone.getTimeZone(Time.time_zone) )
+end
+
 function Time.format(time,pattern)
 	time = assert_long(time)
 	pattern = pattern or "yyyy-MM-dd HH:mm:ss"
-	return SimpleDateFormat.new(pattern).format(Date.new(time))
+	local fmt = SimpleDateFormat.new(pattern)
+	set_time_zone(fmt)
+	return fmt.format(Date.new(time))
 end
 
 function Time.on( year, month, day, hour, minute, second, millis )
@@ -69,7 +82,9 @@
 end
 
 function Time.parse( pattern, source )
-	return SimpleDateFormat.new(pattern).parse(source).getTime()
+	local fmt = SimpleDateFormat.new(pattern)
+	set_time_zone(fmt)
+	return fmt.parse(source).getTime()
 end