1 -- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
4 -- This class contains functions to parse, compare and format http dates.
5 module("luci.http.protocol.date", package.seeall)
7 require("luci.sys.zoneinfo")
11 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
12 "Sep", "Oct", "Nov", "Dec"
15 function tz_offset(tz)
17 if type(tz) == "string" then
19 -- check for a numeric identifier
20 local s, v = tz:match("([%+%-])([0-9]+)")
21 if s == '+' then s = 1 else s = -1 end
22 if v then v = tonumber(v) end
25 return s * 60 * ( math.floor( v / 100 ) * 60 + ( v % 100 ) )
28 elseif luci.sys.zoneinfo.OFFSET[tz:lower()] then
29 return luci.sys.zoneinfo.OFFSET[tz:lower()]
38 function to_unix(date)
40 local wd, day, mon, yr, hr, min, sec, tz = date:match(
41 "([A-Z][a-z][a-z]), ([0-9]+) " ..
42 "([A-Z][a-z][a-z]) ([0-9]+) " ..
43 "([0-9]+):([0-9]+):([0-9]+) " ..
47 if day and mon and yr and hr and min and sec then
51 if MONTHS[i] == mon then
57 -- convert to epoch time
58 return tz_offset(tz) + os.time( {
71 function to_http(time)
72 return os.date( "%a, %d %b %Y %H:%M:%S GMT", time )
75 function compare(d1, d2)
77 if d1:match("[^0-9]") then d1 = to_unix(d1) end
78 if d2:match("[^0-9]") then d2 = to_unix(d2) end