User:GreenC/software/base62.lua

Given a WebCite ID taken from a URL ( http://www.webcitation.org/6KmF9nYxg ) return dates in multiple formats.

Example:

mintbox:[/home/adminuser] ./base62.lua 6KmF9nYxg
October 31, 2013|31 October 2013|2013-10-31|2013 October 31

Base62.lua

#!/usr/bin/lua

-- Given a Webcite ID on arg[1], return dates in mdy|dmy|iso|ymd format
--   example ID: 6H8pdR68H

-- http://convertxy.com/index.php/numberbases
-- http://www.onlineconversion.com/unix_time.htm


--[[--------------------------< base62 >-----------------------

     Convert base-62 to base-10
     Credit: https://de.wikipedia.org/wiki/Modul:Expr 

  ]]

local function base62( value )

    local r = 1

    if value:match( "^%w+$" ) then
        local n = #value
        local k = 1
        local c
        r = 0
        for i = n, 1, -1 do
            c = value:byte( i, i )
            if c >= 48  and  c <= 57 then
                c = c - 48
            elseif c >= 65  and  c <= 90 then
                c = c - 55
            elseif c >= 97  and  c <= 122 then
                c = c - 61
            else    -- How comes?
                r = 1
                break    -- for i
            end
            r = r + c * k
            k = k * 62
        end -- for i
    end
    return r
end 

local function main()

  -- "!" in os.date means use GMT 

  zday = os.date("!%d", string.sub(string.format("%d", base62(arg[1])),1,10) )
  day = zday:match("0*(%d+)")                                                             -- remove leading zero
  zmonth = os.date("!%m", string.sub(string.format("%d", base62(arg[1])),1,10) )
  month = zmonth:match("0*(%d+)")
  nmonth = os.date("!%B", string.sub(string.format("%d", base62(arg[1])),1,10) ) 
  year = os.date("!%Y", string.sub(string.format("%d", base62(arg[1])),1,10) )

  mdy = nmonth .. " " .. day .. ", " .. year
  dmy = day .. " " .. nmonth .. " " .. year
  iso = year .. "-" .. zmonth .. "-" .. zday
  ymd = year .. " " .. nmonth .. " " .. day  

  year = tonumber(year)
  month = tonumber(month)
  day = tonumber(day)

  if year < 1970 or year > 2060 then
    print "error"
  elseif day < 1 or day > 31 then
    print "error"
  elseif month < 1 or month > 12 then
    print "error"
  else
    print(mdy .. "|" .. dmy .. "|" .. iso .. "|" .. ymd)
  end 

end

main()

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.