83d11e2c25c1a6bdba6e5dd54f0f0adb8ce1876a
[project/luci.git] / libs / web / luasrc / http / protocol / date.lua
1 --[[
2
3 HTTP protocol implementation for LuCI - date handling
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
5
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
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 ]]--
15
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)
19
20 require("luci.sys.zoneinfo")
21
22
23 MONTHS = {
24 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
25 "Sep", "Oct", "Nov", "Dec"
26 }
27
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)
32
33 if type(tz) == "string" then
34
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
39
40 if s and v then
41 return s * 60 * ( math.floor( v / 100 ) * 60 + ( v % 100 ) )
42
43 -- lookup symbolic tz
44 elseif luci.sys.zoneinfo.OFFSET[tz:lower()] then
45 return luci.sys.zoneinfo.OFFSET[tz:lower()]
46 end
47
48 end
49
50 -- bad luck
51 return 0
52 end
53
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)
58
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]+) " ..
63 "([A-Z0-9%+%-]+)"
64 )
65
66 if day and mon and yr and hr and min and sec then
67 -- find month
68 local month = 1
69 for i = 1, 12 do
70 if MONTHS[i] == mon then
71 month = i
72 break
73 end
74 end
75
76 -- convert to epoch time
77 return tz_offset(tz) + os.time( {
78 year = yr,
79 month = month,
80 day = day,
81 hour = hr,
82 min = min,
83 sec = sec
84 } )
85 end
86
87 return 0
88 end
89
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 )
95 end
96
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)
104
105 if d1:match("[^0-9]") then d1 = to_unix(d1) end
106 if d2:match("[^0-9]") then d2 = to_unix(d2) end
107
108 if d1 == d2 then
109 return 0
110 elseif d1 < d2 then
111 return -1
112 else
113 return 1
114 end
115 end