• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua how convert timestamp to "date" type

silveralol

Advanced OT User
Joined
Mar 16, 2010
Messages
1,480
Solutions
9
Reaction score
211
hello folks, I'm trying to convert a timestamp to show in the item description...
actual behavior with this script
local test = thing:getAttribute(ITEM_ATTRIBUTE_DATE)
description = description .. thing:getDescription(distance) .. '\n' ..tostring(test) .. '.'

this attr I get from the PR of the reward chest system from socket

when I look at reward container appears:
You see a reward container (Vol:32).
This container contains your rewards earned in battles.
1494644666.
I want to convert the red number to something like that
13 may 2017.
its is possible?
 
Lua:
local test = thing:getAttribute(ITEM_ATTRIBUTE_DATE)
print(os.date('%d-%b-%Y', test))
can you test it?
edited:my bad
 
Last edited:
Solution
i've had made a function back in days, just translated from my personal c lib.
Lua:
local function get_date_from_unix(unix_time)
    local day_count, year, days, month = function(yr) return (yr % 4 == 0 and (yr % 100 ~= 0 or yr % 400 == 0)) and 366 or 365 end, 1970, math.ceil(unix_time/86400)

    while days >= day_count(year) do
        days = days - day_count(year) year = year + 1
    end
    local tab_overflow = function(seed, table) for i = 1, #table do if seed - table[i] <= 0 then return i, seed end seed = seed - table[i] end end
    month, days = tab_overflow(days, {31,(day_count(year) == 366 and 29 or 28),31,30,31,30,31,31,30,31,30,31})
    local hours, minutes, seconds = math.floor(unix_time / 3600 % 24), math.floor(unix_time / 60 % 60), math.floor(unix_time % 60)
    local period = hours > 12 and "pm" or "am"
    hours = hours > 12 and hours - 12 or hours == 0 and 12 or hours
    return string.format("%d/%d/%04d %02d:%02d:%02d %s", days, month, year, hours, minutes, seconds, period)
end
usage:
print(get_date_from_unix(1494652614))
let me know if it's working.
a todo that i never did in this function was mend daylight saving times inside of it, if you're using it, you should do it yourself before calling it.
 
Last edited:
If you want to get it as table:

Lua:
local d = os.date("*t", UNIX_TIMESTAMP)

This produces the following table

Lua:
{year = x, month = x, day = x, yday = x, wday = x,
     hour = x, min = x, sec = x, isdst = false}

As a string, do what @StreamSide says
 
Back
Top