changeset 1546:2b4471d310b3

better time zone handling
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 22 Sep 2020 21:41:22 -0600
parents f46b81048a80
children f24a9ba7551e
files src/luan/modules/Time.luan src/luan/modules/http/Http.luan
diffstat 2 files changed, 14 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
diff -r f46b81048a80 -r 2b4471d310b3 src/luan/modules/Time.luan
--- a/src/luan/modules/Time.luan	Sun Sep 20 21:09:19 2020 -0600
+++ b/src/luan/modules/Time.luan	Tue Sep 22 21:41:22 2020 -0600
@@ -44,22 +44,20 @@
 	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()
+function Time.get_time_zone(name)
+	local tz = name and TimeZone.getTimeZone(name) 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) )
+local function set_time_zone(fmt,time_zone)
+	time_zone and fmt.setTimeZone( TimeZone.getTimeZone(time_zone) )
 end
 
-function Time.format(time,pattern)
+function Time.format( time, pattern, time_zone )
 	time = long(time)
 	pattern = pattern or "yyyy-MM-dd HH:mm:ss"
 	local fmt = SimpleDateFormat.new(pattern)
-	set_time_zone(fmt)
+	set_time_zone(fmt,time_zone)
 	return fmt.format(Date.new(time))
 end
 
@@ -82,9 +80,9 @@
 	return cal.getTimeInMillis()
 end
 
-function Time.parse( pattern, source )
+function Time.parse( pattern, source, time_zone )
 	local fmt = SimpleDateFormat.new(pattern)
-	set_time_zone(fmt)
+	set_time_zone(fmt,time_zone)
 	return fmt.parse(source).getTime()
 end
 
diff -r f46b81048a80 -r 2b4471d310b3 src/luan/modules/http/Http.luan
--- a/src/luan/modules/http/Http.luan	Sun Sep 20 21:09:19 2020 -0600
+++ b/src/luan/modules/http/Http.luan	Tue Sep 22 21:41:22 2020 -0600
@@ -14,6 +14,8 @@
 local lower = String.lower or error()
 local matches = String.matches or error()
 local trim = String.trim or error()
+local Time = require "luan:Time.luan"
+local time_format = Time.format or error()
 local Boot = require "luan:Boot.luan"
 local LuanJava = require "java:luan.Luan"
 local Request = require "java:goodjava.webserver.Request"
@@ -180,4 +182,8 @@
 
 Http.is_serving = false
 
+function Http.format_date(date)
+	return time_format(date,"EEE, dd MMM yyyy HH:mm:ss 'GMT'","GMT")
+end
+
 return Http