 # timestring to time

Hi all,

I’m searching for a method to convert a timestring like “1d2m” (1 day 2 months) into time like CurTime() + timefromstring(“1d2m”). How can I do this?

Thanks!
Darrell

What’s wrong with regular numbers?

CurTime() + 86400 * 1 + 2592000 * 2 ( 30 day month. )

RobotBoy is absolutely right. There is really no need for this. Regardless, you can just use string.find to find the position in the string of the identifier for the time (y for years, for example), use string.sub to find the position of the multiplier, and then multiply the time in seconds by the multiplier. Then, you can add that to a defined local variable and return the variable after going through all of the time identifiers. Here’s a messy example:

``````

function GetTimeFromString(str)
local time = 0
local ys, ye = string.find(str, 'y', 1, true)
local ms, me = string.find(str, 'm', 1, true)
local ds, de = string.find(str, 'd', 1, true)
local hs, he = string.find(str, 'd', 1, true)
local ms, me = string.find(str, 'm', 1, true)
local ss, se = string.find(str, 'm', 1, true)
if ys then
time = time + (tonumber(string.sub(str, ys-1, ye-1)) * 31536000)
end
if ms then
time = time + (tonumber(string.sub(str, ms-1, me-1)) * 2419200)
end
if ds then
time = time + (tonumber(string.sub(str, ds-1, de-1)) * 86400)
end
if hs then
time = time + (tonumber(string.sub(str, hs-1, he-1)) * 3600)
end
if ms then
time = time + (tonumber(string.sub(str, ms-1, me-1)) * 60)
end
if ss then
time = time + (tonumber(string.sub(str, ss-1, se-1)) * 1)
end
return time
end

``````

``````

local things={
s = 1,
mi = 1*60,
h = 1*60*60,
d = 1*60*60*24,
mo= 1*60*60*24*30, -- fix this, not always correct unless you want 30 day long months
y = 1*60*60*24*30*12,
}
local t = CurTime()
for amt, which in str:gmatch(("([0-9]*)([a-zA-Z]*)") do
t = t + amt * things[which];
end
return t;

``````

Will error if you don’t have any modifier after a number or modifier is invalid. Fixed version:

``````

local things={
s = 1,
mi = 1*60,
h = 1*60*60,
d = 1*60*60*24,
mo= 1*60*60*24*30, -- fix this, not always correct unless you want 30 day long months
y = 1*60*60*24*30*12,
}
local t = CurTime()
for amt, which in str:gmatch(("([0-9]+)([a-zA-Z]*)") do
t = t + tonumber(amt) * (things[(which or "s"):lower()] or 1);
end
return t;

``````

Because if you do a command like /ban <person> 173200 you have no idea how long that is, this makes it easier to understand.

[editline]23rd April 2015[/editline]

Thanks, that helped me alot!!