From b4040aacb04ce38b7eae315d37b2a61a38aea4b1 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Wed, 18 Apr 2018 15:49:26 +0200 Subject: libs: move http.protocol.{date,mime,conditionals} to luci-lib-httpprotoutils Also adjust the dependencies of components depending on these classes and flatten the namespace from luci.http.protocol.* to luci.http.* Signed-off-by: Jo-Philipp Wich --- libs/luci-lib-httpprotoutils/luasrc/http/date.lua | 87 +++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 libs/luci-lib-httpprotoutils/luasrc/http/date.lua (limited to 'libs/luci-lib-httpprotoutils/luasrc/http/date.lua') diff --git a/libs/luci-lib-httpprotoutils/luasrc/http/date.lua b/libs/luci-lib-httpprotoutils/luasrc/http/date.lua new file mode 100644 index 0000000000..72f1bdb577 --- /dev/null +++ b/libs/luci-lib-httpprotoutils/luasrc/http/date.lua @@ -0,0 +1,87 @@ +-- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich +-- Licensed to the public under the Apache License 2.0. + +-- This class contains functions to parse, compare and format http dates. +module("luci.http.date", package.seeall) + +require("luci.sys.zoneinfo") + + +MONTHS = { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", + "Sep", "Oct", "Nov", "Dec" +} + +function tz_offset(tz) + + if type(tz) == "string" then + + -- check for a numeric identifier + local s, v = tz:match("([%+%-])([0-9]+)") + if s == '+' then s = 1 else s = -1 end + if v then v = tonumber(v) end + + if s and v then + return s * 60 * ( math.floor( v / 100 ) * 60 + ( v % 100 ) ) + + -- lookup symbolic tz + elseif luci.sys.zoneinfo.OFFSET[tz:lower()] then + return luci.sys.zoneinfo.OFFSET[tz:lower()] + end + + end + + -- bad luck + return 0 +end + +function to_unix(date) + + 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-Z0-9%+%-]+)" + ) + + if day and mon and yr and hr and min and sec then + -- find month + local month = 1 + for i = 1, 12 do + if MONTHS[i] == mon then + month = i + break + end + end + + -- convert to epoch time + return tz_offset(tz) + os.time( { + year = yr, + month = month, + day = day, + hour = hr, + min = min, + sec = sec + } ) + end + + return 0 +end + +function to_http(time) + return os.date( "%a, %d %b %Y %H:%M:%S GMT", time ) +end + +function compare(d1, d2) + + if d1:match("[^0-9]") then d1 = to_unix(d1) end + if d2:match("[^0-9]") then d2 = to_unix(d2) end + + if d1 == d2 then + return 0 + elseif d1 < d2 then + return -1 + else + return 1 + end +end -- cgit v1.2.3