3 HTTP protocol implementation for LuCI - date handling
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
16 --- LuCI http protocol implementation - date helper class.
17 -- This class contains functions to parse, compare and format http dates.
18 module("luci.http.protocol.date", package.seeall)
20 require("luci.sys.zoneinfo")
24 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
25 "Sep", "Oct", "Nov", "Dec"
28 --- Return the time offset in seconds between the UTC and given time zone.
29 -- @param tz Symbolic or numeric timezone specifier
30 -- @return Time offset to UTC in seconds
31 function tz_offset(tz)
33 if type(tz) == "string" then
35 -- check for a numeric identifier
36 local s, v = tz:match("([%+%-])([0-9]+)")
37 if s == '+' then s = 1 else s = -1 end
38 if v then v = tonumber(v) end
41 return s * 60 * ( math.floor( v / 100 ) * 60 + ( v % 100 ) )
44 elseif luci.sys.zoneinfo.OFFSET[tz:lower()] then
45 return luci.sys.zoneinfo.OFFSET[tz:lower()]
54 --- Parse given HTTP date string and convert it to unix epoch time.
55 -- @param data String containing the date
56 -- @return Unix epoch time
57 function to_unix(date)
59 local wd, day, mon, yr, hr, min, sec, tz = date:match(
60 "([A-Z][a-z][a-z]), ([0-9]+) " ..
61 "([A-Z][a-z][a-z]) ([0-9]+) " ..
62 "([0-9]+):([0-9]+):([0-9]+) " ..
66 if day and mon and yr and hr and min and sec then
70 if MONTHS[i] == mon then
76 -- convert to epoch time
77 return tz_offset(tz) + os.time( {
90 --- Convert the given unix epoch time to valid HTTP date string.
91 -- @param time Unix epoch time
92 -- @return String containing the formatted date
93 function to_http(time)
94 return os.date( "%a, %d %b %Y %H:%M:%S GMT", time )
97 --- Compare two dates which can either be unix epoch times or HTTP date strings.
98 -- @param d1 The first date or epoch time to compare
99 -- @param d2 The first date or epoch time to compare
100 -- @return -1 - if d1 is lower then d2
101 -- @return 0 - if both dates are equal
102 -- @return 1 - if d1 is higher then d2
103 function compare(d1, d2)
105 if d1:match("[^0-9]") then d1 = to_unix(d1) end
106 if d2:match("[^0-9]") then d2 = to_unix(d2) end