DaikiKaminari

lib/datetime

Aug 9th, 2020 (edited)
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.76 KB | None | 0 0
  1. -- [V1.2]
  2. -- table : days of march and october when we add/remove an hour for daylight saving
  3. local daylightHours = {
  4.     [2020] = {["summerBegin"] = 28, ["summerEnd"] = 24},
  5.     [2021] = {["summerBegin"] = 27, ["summerEnd"] = 30},
  6.     [2022] = {["summerBegin"] = 26, ["summerEnd"] = 29},
  7.     [2023] = {["summerBegin"] = 25, ["summerEnd"] = 28},
  8.     [2024] = {["summerBegin"] = 30, ["summerEnd"] = 26},
  9.     [2025] = {["summerBegin"] = 29, ["summerEnd"] = 25}
  10. }
  11.  
  12. --- INIT ---
  13. function init()
  14.     if not fs.exists("lib/json") then
  15.         error("[lib/json] file not found.")
  16.     end
  17.     if not fs.exists("lib/objectJSON") then
  18.         error("[lib/objectJSON] file not found.")
  19.     end
  20.     os.loadAPI("lib/objectJSON")
  21.     objectJSON.init()
  22. end
  23.  
  24.  
  25. --- UTILS ----
  26. local function split(inputstr, sep)
  27.     if sep == nil then sep = "%s" end
  28.     local t={}
  29.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  30.             table.insert(t, str)
  31.     end
  32.     return t
  33. end
  34.  
  35. function getDaylightHours(day, month, year)
  36.     if month == 3 and day >= daylightHours[year]["summerBegin"] or
  37.     month == 10 and day <= daylightHours[year]["summerEnd"] or
  38.     (month > 3 and month < 10) then
  39.         return 1 -- summer
  40.     else
  41.         return 0 -- winter
  42.     end
  43. end
  44.  
  45. --- FUNCTIONS ---
  46. function getDatetime()
  47.     local jsonDatetime = objectJSON.decodeHTTP("http://worldtimeapi.org/api/timezone/Europe/Paris.json")
  48.     if jsonDatetime == nil then
  49.         return nil
  50.     end
  51.     local datetime = jsonDatetime.utc_datetime
  52.     local offset = jsonDatetime.utc_offset
  53.  
  54.     local dayOfWeek = jsonDatetime.day_of_week
  55.     local days = {
  56.         [0] = "Dimanche",
  57.         [1] = "Lundi",
  58.         [2] = "Mardi",
  59.         [3] = "Mercredi",
  60.         [4] = "Jeudi",
  61.         [5] = "Vendredi",
  62.         [6] = "Samedi"
  63.     }
  64.  
  65.     local infos = {}
  66.     local date = split(split(datetime, "T")[1], "-")
  67.     local year = date[1]
  68.     local month = date[2]
  69.     local day = date[3]
  70.     local daylight = getDaylightHours(tonumber(day), tonumber(month), tonumber(year))
  71.     local time = split(split(datetime, "T")[2], ":")
  72.     local offset = split(split(offset, ":")[1], "+")[1]
  73.  
  74.     infos["dayOfWeek"] = days[dayOfWeek]
  75.     infos["year"] = year
  76.     infos["month"] = month
  77.     infos["day"] = day
  78.     infos["hour"] = (tonumber(time[1]) + tonumber(offset) + daylight + 2) % 24
  79.     infos["min"] = time[2]
  80.     infos["sec"] = split(time[3], ".")[1]
  81.  
  82.     return infos -- table with keys : dayOfWeek, year, month, day, hour, min, sec
  83. end
  84.  
  85. function getDatetime2()
  86.     local jsonDatetime = objectJSON.decodeHTTP("http://worldclockapi.com/api/json/utc/now")
  87.     if jsonDatetime == nil then
  88.         return nil
  89.     end
  90.     local datetime = jsonDatetime.currentDateTime       -- 2020-08-09T16:14Z
  91.  
  92.     local infos = {}            
  93.     local date = split(split(datetime, "T")[1], "-")    -- 2020-08-09
  94.     local year = date[1]
  95.     local month = date[2]
  96.     local day = date[3]
  97.  
  98.     local dayOfWeek = jsonDatetime.dayOfTheWeek
  99.     local days = {
  100.         ["Sunday"] = "Dimanche",
  101.         ["Monday"] = "Lundi",
  102.         ["Tuesday"] = "Mardi",
  103.         ["Wednesday"] = "Mercredi",
  104.         ["Thursday"] = "Jeudi",
  105.         ["Friday"] = "Vendredi",
  106.         ["Saturday"] = "Samedi"
  107.     }
  108.    
  109.     local daylight = getDaylightHours(tonumber(day), tonumber(month), tonumber(year))
  110.     local time = split(split(datetime, "T")[2], ":")    -- 16:14Z
  111.     local hour = tostring((tonumber(time[1]) + daylight + 1) % 24)
  112.     local min = split(time[2], "Z")[1]
  113.    
  114.     infos["dayOfWeek"] = days[dayOfWeek]
  115.     infos["year"] = year
  116.     infos["month"] = month
  117.     infos["day"] = day
  118.     infos["hour"] = hour
  119.     infos["min"] = min
  120.     return infos -- table with keys : dayOfWeek, year, month, day, hour, min
  121. end
Add Comment
Please, Sign In to add comment