• 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 os.time edition/math?

Athenuz

Owlz!
Joined
Oct 1, 2015
Messages
234
Reaction score
27
Location
México
Code:
function onSay(player, words, param)
    local hours = player:getStorageValue(questStorage) > 0 and math.ceil((player:getStorageValue(questStorage)-os.time())/60/60) or 0
  
    if player:getStorageValue(bonusStorage) > os.time() then
        player:sendTextMessage(TALKTYPE_ORANGE_2, "You have "..hours.." hour(s) of time remainning.")
    else
        player:sendTextMessage(TALKTYPE_ORANGE_2, "You don't have any quest activated.")
    end
  
    return false
end

Result:

You have 4 hour (s) of time remainning.

Hello, i tried to make this look like:

Code:
You have 3 hour (s) and 27 minute (s) of time remainning.

But i don't really know how to.. Can please someone help me?
 
Code:
function onSay(player, words, param)
    local hours = player:getStorageValue(questStorage) > 0 and math.ceil((player:getStorageValue(questStorage)-os.time())/60/60) or 0

    if player:getStorageValue(bonusStorage) > os.time() then
        player:sendTextMessage(TALKTYPE_ORANGE_2, "You have "..hours.." hour(s) of time remainning.")
    else
        player:sendTextMessage(TALKTYPE_ORANGE_2, "You don't have any quest activated.")
    end

    return false
end

Result:

You have 4 hour (s) of time remainning.

Hello, i tried to make this look like:

Code:
You have 3 hour (s) and 27 minute (s) of time remainning.

But i don't really know how to.. Can please someone help me?

Did you try with % operator?

Code:
function onSay(player, words, param)
    local hours = player:getStorageValue(questStorage) > 0 and math.ceil((player:getStorageValue(questStorage)-os.time())/60/60) or 0
    local mins = player:getStorageValue(questStorage) > 0 and ((player:getStorageValue(questStorage)-os.time())/60)%60 or 0
    if player:getStorageValue(bonusStorage) > os.time() then
        player:sendTextMessage(TALKTYPE_ORANGE_2, "You have "..hours.." hour(s) and "..mins.."  min(s) of time remainning.")
    else
        player:sendTextMessage(TALKTYPE_ORANGE_2, "You don't have any quest activated.")
    end

    return false
end
 
Did you try with % operator?

Code:
function onSay(player, words, param)
    local hours = player:getStorageValue(questStorage) > 0 and math.ceil((player:getStorageValue(questStorage)-os.time())/60/60) or 0
    local mins = player:getStorageValue(questStorage) > 0 and ((player:getStorageValue(questStorage)-os.time())/60)%60 or 0
    if player:getStorageValue(bonusStorage) > os.time() then
        player:sendTextMessage(TALKTYPE_ORANGE_2, "You have "..hours.." hour(s) and "..mins.."  min(s) of time remainning.")
    else
        player:sendTextMessage(TALKTYPE_ORANGE_2, "You don't have any quest activated.")
    end

    return false
end

Thanks!
I'm not even a newbie scripter, so it makes a bit harder to me :p

Works good but..

14:02 You have 3 hour(s) and 40.15 min(s) of time remainning.
14:02 You have 3 hour(s) and 40.133333333333 min(s) of time remainning.
14:02 You have 3 hour(s) and 40.116666666667 min(s) of time remainning.
14:02 You have 3 hour(s) and 40.1 min(s) of time remainning.

Is there a way to limit it to minutes and don't show the seconds? :3
 
Last edited:
I'm not going to try edit your code.. but this is what I use to show hunger in 0.3.7
I remember researching lua date/time codes for like 2 hours to get it this way. xD
Code:
Your current hunger timer shows ".. os.date("!%M:%S",getPlayerFood(cid)) ..".
Although this might be more useful.
http://www.lua.org/pil/22.1.html
Code:
%a abbreviated weekday name (e.g., Wed)
%A full weekday name (e.g., Wednesday)
%b abbreviated month name (e.g., Sep)
%B full month name (e.g., September)
%c date and time (e.g., 09/16/98 23:48:10)
%d day of the month (16) [01-31]
%H hour, using a 24-hour clock (23) [00-23]
%I hour, using a 12-hour clock (11) [01-12]
%M minute (48) [00-59]
%m month (09) [01-12]
%p either "am" or "pm" (pm)
%S second (10) [00-61]
%w weekday (3) [0-6 = Sunday-Saturday]
%x date (e.g., 09/16/98)
%X time (e.g., 23:48:10)
%Y full year (1998)
%y two-digit year (98) [00-99]
%% the character `%´

Just thought you could also add "math.ceil" to the seconds.. and that should round it up as well.. like your already doing with the hours.
 
Last edited:
I'm not going to try edit your code.. but this is what I use to show hunger in 0.3.7
I remember researching lua date/time codes for like 2 hours to get it this way. xD
Code:
Your current hunger timer shows ".. os.date("!%M:%S",getPlayerFood(cid)) ..".
Although this might be more useful.
http://www.lua.org/pil/22.1.html
Code:
%a abbreviated weekday name (e.g., Wed)
%A full weekday name (e.g., Wednesday)
%b abbreviated month name (e.g., Sep)
%B full month name (e.g., September)
%c date and time (e.g., 09/16/98 23:48:10)
%d day of the month (16) [01-31]
%H hour, using a 24-hour clock (23) [00-23]
%I hour, using a 12-hour clock (11) [01-12]
%M minute (48) [00-59]
%m month (09) [01-12]
%p either "am" or "pm" (pm)
%S second (10) [00-61]
%w weekday (3) [0-6 = Sunday-Saturday]
%x date (e.g., 09/16/98)
%X time (e.g., 23:48:10)
%Y full year (1998)
%y two-digit year (98) [00-99]
%% the character `%´

Just thought you could also add "math.ceil" to the seconds.. and that should round it up as well.. like your already doing with the hours.

I'll try! Thanks Xikini!
 
Code:
function string.diff(self)
    local format = {
        {'day', self / 60 / 60 / 24},
        {'hour', self / 60 / 60 % 24},
        {'minute', self / 60 % 60},
        {'second', self % 60}
    }

    local out = {}
    for k, t in ipairs(format) do
        local v = math.floor(t[2])
        if(v > 0) then
            table.insert(out, (k < #format and (#out > 0 and ', ' or '') or ' and ') .. v .. ' ' .. t[1] .. (v ~= 1 and 's' or ''))
        end
    end
    local ret = table.concat(out)
    if ret:len() < 16 and ret:find('second') then
        local a, b = ret:find(' and ')
        ret = ret:sub(b+1)
    end
    return ret
end

This can help you.
 
Or this, you can use to test :p
Code:
function gt()
    return os.date("*t", os.time())
end

local t = {year = gt().year, month = gt().month, day = gt().day, min = gt().min, sec = gt().sec}
local x = os.time(t)
print(os.date("*t", x).year)
 
The gt function in the example returns the current time as a table you can then store this data in a table like i did with t table, you can add time more easier now that its whole values rather than dealing with milliseconds, and then pass it back to os.time to store as milliseconds again.

To retrieve the actual time to compare it to the current time you would pass the storage value to os.date just as we did with os.time inside the gt function, it might seem like extra work but it isn't, by comparing milliseconds to milliseconds your getting this inaccurate time where as comparing data retrieved from os.date your getting exact numbers.

To show an example of how you can set time using os.time & os.date
I created 2 functions
Code:
function gt()
    return os.date("*t", os.time())
end
function setTime(y, m, d, h, mi, s)
    local t = {
        year = gt().year + (y or 0),
        month = gt().month + (m or 0),
        day = gt().day + (d or 0),
        hour = gt().hour + (h or 0),
        min = gt().min + (mi or 0),
        sec = gt().sec + (s or 0)
    }
    return t
end
-- this will set the year to 2016, rather than 2015
local x = os.time(setTime(1)) -- this will store the modified time in x

If we were to run a for loop over os.date we would get this
Code:
for i, n in pairs(os.date("*t", x)) do
    print(i, n)
end
-- output
hour    0 -- its 12 am here :p
min    14
wday    4
day    26
month    10
year    2016
sec    56
yday    300
isdst    true

Of course you can add the rest of the properties to setTime function but only if you really need those additional values.

I strongly recommend if your building a server, learn the core language (lua) this will make writing scripts so much more easier :)
 
Last edited:
The gt function in the example returns the current time as a table you can then store this data in a table like i did with t table, you can add time more easier now that its whole values rather than dealing with milliseconds, and then pass it back to os.time to store as milliseconds again.

To retrieve the actual time to compare it to the current time you would pass the storage value to os.date just as we did with os.time inside the gt function, it might seem like extra work but it isn't, by comparing milliseconds to milliseconds your getting this inaccurate time where as comparing data retrieved from os.date your getting exact numbers.

To show an example of how you can set time using os.time & os.date
I created 2 functions
Code:
function gt()
    return os.date("*t", os.time())
end
function setTime(y, m, d, h, mi, s)
    local t = {
        year = gt().year + (y or 0),
        month = gt().month + (m or 0),
        day = gt().day + (d or 0),
        hour = gt().hour + (h or 0),
        min = gt().min + (mi or 0),
        sec = gt().sec + (s or 0)
    }
    return t
end
-- this will set the year to 2016, rather than 2015
local x = os.time(setTime(1)) -- this will store the modified time in x

If we were to run a for loop over os.date we would get this
Code:
for i, n in pairs(os.date("*t", x)) do
    print(i, n)
end
-- output
hour    0 -- its 12 am here :p
min    14
wday    4
day    26
month    10
year    2016
sec    56
yday    300
isdst    true

Of course you can add the rest of the properties to setTime function but only if you really need those additional values.

I strongly recommend if your building a server, learn the core language (lua) this will make writing scripts so much more easier :)

I know, i'm going to learn asap xD

Meanwhile i still don't know how to implement that to get something like this:
Code:
You have 3 hour (s) and 27 minute (s) of time remainning.

I'll dig a little and try xD


Thanks :p
 
You can test your script with this, (not tested).. dam I need a new mouse :(
Code:
    local questStorage = 12345

    function getTime(time_)
        return os.date("*t", time_ or os.time())
    end
    function setTime(y, m, d, h, mi, s)
        local t = {
            year = getTime().year + (y or 0),
            month = getTime().month + (m or 0),
            day = getTime().day + (d or 0),
            hour = getTime().hour + (h or 0),
            min = getTime().min + (mi or 0),
            sec = getTime().sec + (s or 0)
        }
        return t
    end

    function setQuestStorage(player, storage, value)
        if player:isPlayer() then
            player:setStorageValue( storage, os.time( setTime(nil, nil, nil, value, nil, nil) ) )
        end
    end

    local avoid = {'year', 'month', 'wday', 'yday', 'day', 'isdst' }

    function getQuestData(player, storage)
        if not player:isPlayer() then
            return false
        end
        local questTime = {}
        local temp = getTime( player:getStorageValue(storage) )
        for prop, value in pairs( temp ) do
            if not isInArray(avoid, prop) then
                questTime[prop] = value
            end
        end
        return questTime, os.time(temp)
    end

    function onSay(player, words, param)
        local data, time_ = getQuestData(player, questStorage)
        if data then
            local msg = ""..(data.hour > 0 and data.hour .." hour(s)," or '').." "..(data.min > 0 and data.min.." minutes(s)," or '').." "..(data.sec > 0 and data.sec.." second(s)," or '')
            if time_ > os.time() then
                player:sendTextMessage(TALKTYPE_ORANGE_2, "You have ".. msg .. " of time remainning.")
            else
                player:sendTextMessage(TALKTYPE_ORANGE_2, "You don't have any quest activated.")
            end
        end
        return false
    end
 
Last edited:
Back
Top