* libs/http: implement caching in http.protocol.date
This commit is contained in:
parent
378ef2da56
commit
96e11423b3
1 changed files with 35 additions and 25 deletions
|
@ -15,6 +15,10 @@ $Id$
|
||||||
|
|
||||||
module("luci.http.protocol.date", package.seeall)
|
module("luci.http.protocol.date", package.seeall)
|
||||||
|
|
||||||
|
local ucache = { }
|
||||||
|
local hcache = { }
|
||||||
|
|
||||||
|
|
||||||
MONTHS = {
|
MONTHS = {
|
||||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
|
||||||
"Sep", "Oct", "Nov", "Dec"
|
"Sep", "Oct", "Nov", "Dec"
|
||||||
|
@ -151,40 +155,46 @@ end
|
||||||
-- Convert a HTTP date to unixtime
|
-- Convert a HTTP date to unixtime
|
||||||
function to_unix(date)
|
function to_unix(date)
|
||||||
|
|
||||||
local wd, day, mon, yr, hr, min, sec, tz = date:match(
|
if not ucache[date] then
|
||||||
"([A-Z][a-z][a-z]), ([0-9]+) " ..
|
local wd, day, mon, yr, hr, min, sec, tz = date:match(
|
||||||
"([A-Z][a-z][a-z]) ([0-9]+) " ..
|
"([A-Z][a-z][a-z]), ([0-9]+) " ..
|
||||||
"([0-9]+):([0-9]+):([0-9]+) " ..
|
"([A-Z][a-z][a-z]) ([0-9]+) " ..
|
||||||
"([A-Z0-9%+%-]+)"
|
"([0-9]+):([0-9]+):([0-9]+) " ..
|
||||||
)
|
"([A-Z0-9%+%-]+)"
|
||||||
|
)
|
||||||
|
|
||||||
if day and mon and yr and hr and min and sec then
|
if day and mon and yr and hr and min and sec then
|
||||||
-- find month
|
-- find month
|
||||||
local month = 1
|
local month = 1
|
||||||
for i = 1, 12 do
|
for i = 1, 12 do
|
||||||
if MONTHS[i] == mon then
|
if MONTHS[i] == mon then
|
||||||
month = i
|
month = i
|
||||||
break
|
break
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
-- convert to epoch time
|
-- convert to epoch time
|
||||||
return tz_offset(tz) + os.time( {
|
ucache[date] = tz_offset(tz) + os.time( {
|
||||||
year = yr,
|
year = yr,
|
||||||
month = month,
|
month = month,
|
||||||
day = day,
|
day = day,
|
||||||
hour = hr,
|
hour = hr,
|
||||||
min = min,
|
min = min,
|
||||||
sec = sec
|
sec = sec
|
||||||
} )
|
} )
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return 0
|
return ucache[date] or 0
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Convert a unixtime to HTTP date
|
-- Convert a unixtime to HTTP date
|
||||||
function to_http(time)
|
function to_http(time)
|
||||||
return os.date( "%a, %d %b %Y %H:%M:%S GMT", time )
|
if not hcache[time] then
|
||||||
|
hcache[time] = os.date( "%a, %d %b %Y %H:%M:%S GMT", time )
|
||||||
|
end
|
||||||
|
|
||||||
|
return hcache[time]
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Compare two dates
|
-- Compare two dates
|
||||||
|
|
Loading…
Reference in a new issue