2
0

date.lua 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. -- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. -- This class contains functions to parse, compare and format http dates.
  4. module("luci.http.date", package.seeall)
  5. require("luci.sys.zoneinfo")
  6. MONTHS = {
  7. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
  8. "Sep", "Oct", "Nov", "Dec"
  9. }
  10. function tz_offset(tz)
  11. if type(tz) == "string" then
  12. -- check for a numeric identifier
  13. local s, v = tz:match("([%+%-])([0-9]+)")
  14. if s == '+' then s = 1 else s = -1 end
  15. if v then v = tonumber(v) end
  16. if s and v then
  17. return s * 60 * ( math.floor( v / 100 ) * 60 + ( v % 100 ) )
  18. -- lookup symbolic tz
  19. elseif luci.sys.zoneinfo.OFFSET[tz:lower()] then
  20. return luci.sys.zoneinfo.OFFSET[tz:lower()]
  21. end
  22. end
  23. -- bad luck
  24. return 0
  25. end
  26. function to_unix(date)
  27. local wd, day, mon, yr, hr, min, sec, tz = date:match(
  28. "([A-Z][a-z][a-z]), ([0-9]+) " ..
  29. "([A-Z][a-z][a-z]) ([0-9]+) " ..
  30. "([0-9]+):([0-9]+):([0-9]+) " ..
  31. "([A-Z0-9%+%-]+)"
  32. )
  33. if day and mon and yr and hr and min and sec then
  34. -- find month
  35. local month = 1
  36. for i = 1, 12 do
  37. if MONTHS[i] == mon then
  38. month = i
  39. break
  40. end
  41. end
  42. -- convert to epoch time
  43. return tz_offset(tz) + os.time( {
  44. year = yr,
  45. month = month,
  46. day = day,
  47. hour = hr,
  48. min = min,
  49. sec = sec
  50. } )
  51. end
  52. return 0
  53. end
  54. function to_http(time)
  55. return os.date( "%a, %d %b %Y %H:%M:%S GMT", time )
  56. end
  57. function compare(d1, d2)
  58. if d1:match("[^0-9]") then d1 = to_unix(d1) end
  59. if d2:match("[^0-9]") then d2 = to_unix(d2) end
  60. if d1 == d2 then
  61. return 0
  62. elseif d1 < d2 then
  63. return -1
  64. else
  65. return 1
  66. end
  67. end