Search code examples
luatimestamptrello

How would I convert the timestamp that Trello uses into unix timestamp?


So I'm trying to take the time that Trello uses in their card due dates and compare it with Unix time, as I find it easier to deal with when I have two Unix timestamps. However, I'm unsure as how to convert it.

Trello timestamps look like this:

2016-08-13T17:27:06.886Z

The first numbers are the date, and after the T is a time. The Z states that it's "Zulu time" which is the same as UTC.

So what I want to do is take that and convert it into a Unix timestamp using Lua.


Solution

  • Nevermind, I found out how to do it by looking closer at the os.time() function.

    local function formatTime(s)
        local y = tonumber(string.sub(s, 1, 4))
        local m = tonumber(string.sub(s, 6, 7))
        local d = tonumber(string.sub(s, 9, 10))
        local h = tonumber(string.sub(s, 12, 13))
        local mi = tonumber(string.sub(s, 15, 16))
        local s = tonumber(string.sub(s, 18, 19))
    
        local tbl = {
            year = y,
            month = m,
            day = d,
            hour = h,
            minute = mi,
            second = s,
            isdst = (m>=3 and m<=10) --this is roughly close to DST, not perfect.
        }
    
        return os.time(tbl)
    end
    

    Using that, if I call the following:

    formatTime("2016-08-13T17:27:06.886Z")
    

    it would return a Unix timestamp corresponding to that time. Hope this helps anyone with the same problem.