What time is it?

Posted at roughly Zk46Ee:

So 2024-04-06-12-38Z:

local t = dt.fromb60("Zk46Ee")
print(t.b60(), t.iso(), t.rss())

Or:

local t = dt.fromdate({ year = 2024, month = 4, day = 6, hour = 12, min = 38})
print(t.b60(), t.iso(), t.rss())

Current time:

local t = dt.fromdate()
print(t.b60(), t.iso(), t.rss())

local alphabet = "01234567ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

local map = {}
local i = 0
for c in alphabet:gmatch(".") do
    map[c] = i
    i = i + 1
end

local function char(i)
    i = i + 1
    return alphabet:sub(i, i)
end

local function fromint(i)
    if not i then return "_" end
    local res = "";
    while i >= #alphabet do
        res = char(i % #alphabet) .. res
        i = i // #alphabet
    end
    return char(i % #alphabet) .. res
end

local function toint(s)
    local res = 0
    for c in s:gmatch(".") do
        res = (res * #alphabet) + (map[c] or 0)
    end
    return res
end

local month = {
    "Jan", "Feb", "Mar", "Apr" , "May", "Jun",
    "Jul", "Aug" , "Sep", "Oct", "Nov", "Dec"
}

local function fromdate(t)
  t = t or os.date("!*t")
  return {
    b60 = function()
      return fromint(t.year)
        .. fromint(t.month)
        .. fromint(t.day)
        .. fromint(t.hour)
        .. fromint(t.min)
    end,
    iso = function()
      if t.hour then
        return string.format(
          "%04d-%02d-%02dT%02d:%02dZ",
          t.year, t.month, t.day, t.hour, t.min
        )
      else
        return string.format(
          "%04d-%02d-%02d",
          t.year, t.month, t.day
        )
      end
    end,
    rss = function()
      if t.hour then
        return string.format(
          "%02d %s %04d %02d:%02d GMT",
          t.day, month[t.month], t.year, t.hour, t.min
        )
      else
        return
          string.format(
            "%02d %s %04d",
            t.day, month[t.month], t.year
          )
      end
  end
  }
end

function fromb60(s)
  local hour, min
  if s:sub(-2, -1) == "__" then hour, min = nil, nil
  else
    hour = toint(s:sub(-2, -2))
    min = toint(s:sub(-1, -1))
  end
  return fromdate({
    year = toint(s:sub(1, -5)),
    month = toint(s:sub(-4, -4)),
    day = toint(s:sub(-3, -3)),
    hour = hour,
    min = hour
  })
end

dt = {
    fromb60 = fromb60,
    fromdate = fromdate,
    dbg = dbg
}