• 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!

TFS 1.X+ How can i get first part string? && Subtract one time from another

roriscrave

Advanced OT User
Joined
Dec 7, 2011
Messages
1,210
Solutions
35
Reaction score
206
exemple how can i get (22) and (10), separate the string?

LUA:
local times = {"22:10", "23:15"}

}
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
   
    return true
end
 
Solution
Ah, you want to turn strings into numbers?

What is your source of strings?


This will take "12:34" and spit out two numbers.
LUA:
function parseHHMM(time)
    local itr = string.gmatch(time, "%d%d")
    local hour, minute = tonumber(itr()), tonumber(itr())

    return hour, minute
end
LUA:
local s = "22:10"
local hour = s:split(":")[1]
print(hour)

and for exemple, check atual time - table time?

LUA:
local times = {"22:10", "23:15"}

}
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local timeNow = os.date("%H:%M")
   print(timeNow < times[1]) -- ok it print correct
   print(timeNow - times[1]) -- it get error
    return true
end
 
If you want local time, git it directly from os.date() ?? It has a formatter builtin


Code:
%% - Percent sign
%a - Abbreviated weekday name (eg. Wed)
%A - Full weekday name (eg. Wednesday)
%b - Abbreviated month name (eg. Sep)
%B - Full month name (eg. September)
%c - Date and time representation appropriate for locale (eg. 23/04/07 10:20:41)
         (Standard date and time string ) - see below for using os.setlocale to get the correct locale.
%d - Day of month as decimal number (01 - 31)
%H - Hour in 24-hour format (00 - 23)
%I - Hour in 12-hour format (01 - 12)
%j - Day of year as decimal number (001 - 366)
%M - Minute as decimal number (00 - 59)
%m - Month as decimal number (01 - 12)
%p - Current locale’s A.M./P.M. indicator for 12-hour clock (eg. AM/PM)
%S - Second as decimal number (00 - 59)
%U - Week of year as decimal number, with Sunday as first day of week 1 (00 - 53)
%W - Week of year as decimal number, with Monday as first day of week 1 (00 - 53)
%w - Weekday as decimal number (0 - 6; Sunday is 0)
%x - Date representation for current locale (Standard date string)
%X - Time representation for current locale (Standard time string)
%Y - Year with century, as decimal number (eg. 2007)
%y - Year without century, as decimal number (00 - 99)  (eg. 07)
%Z - Time-zone name or abbreviation; no characters if time zone is unknown

If you use "*t" you get a table consisting of:


sec (0 to 59)
min (0 to 59)
hour (0 to 23)
day (1 to 31)
month (1 to 12)
year (1900 onwards)
wday (Sunday is 1, Monday is 2 etc.)
yday (January 1st is 1, etc.)
isdst (true if Daylight Savings Time)


Examples:

LUA:
print (os.date ("%x")) --> 25/04/07
print (os.date ("%c")) --> 25/04/07 10:10:05
print (os.date ("%A, %m %B %Y")) --> Wednesday, 04 April 2007

t = os.date ("*t") --> produces a table like this:

  t.sec=18
  t.min=13
  t.hour=10
  t.day=25
  t.month=4
  t.year=2007
  t.wday=4
  t.yday=115
  t.isdst=false

You can directly use in in string.format()

print(string.format("%s:%s", os.date('%I'), os.date('%M')))


The question is are you trying to generate time strings? Or parse ones that already exist?
 
Last edited:
If you want local time, git it directly from os.date() ?? It has a formatter builtin


Code:
%% - Percent sign
%a - Abbreviated weekday name (eg. Wed)
%A - Full weekday name (eg. Wednesday)
%b - Abbreviated month name (eg. Sep)
%B - Full month name (eg. September)
%c - Date and time representation appropriate for locale (eg. 23/04/07 10:20:41)
         (Standard date and time string ) - see below for using os.setlocale to get the correct locale.
%d - Day of month as decimal number (01 - 31)
%H - Hour in 24-hour format (00 - 23)
%I - Hour in 12-hour format (01 - 12)
%j - Day of year as decimal number (001 - 366)
%M - Minute as decimal number (00 - 59)
%m - Month as decimal number (01 - 12)
%p - Current locale’s A.M./P.M. indicator for 12-hour clock (eg. AM/PM)
%S - Second as decimal number (00 - 59)
%U - Week of year as decimal number, with Sunday as first day of week 1 (00 - 53)
%W - Week of year as decimal number, with Monday as first day of week 1 (00 - 53)
%w - Weekday as decimal number (0 - 6; Sunday is 0)
%x - Date representation for current locale (Standard date string)
%X - Time representation for current locale (Standard time string)
%Y - Year with century, as decimal number (eg. 2007)
%y - Year without century, as decimal number (00 - 99)  (eg. 07)
%Z - Time-zone name or abbreviation; no characters if time zone is unknown

If you use "*t" you get a table consisting of:


sec (0 to 59)
min (0 to 59)
hour (0 to 23)
day (1 to 31)
month (1 to 12)
year (1900 onwards)
wday (Sunday is 1, Monday is 2 etc.)
yday (January 1st is 1, etc.)
isdst (true if Daylight Savings Time)


Examples:

LUA:
print (os.date ("%x")) --> 25/04/07
print (os.date ("%c")) --> 25/04/07 10:10:05
print (os.date ("%A, %m %B %Y")) --> Wednesday, 04 April 2007

t = os.date ("*t") --> produces a table like this:

  t.sec=18
  t.min=13
  t.hour=10
  t.day=25
  t.month=4
  t.year=2007
  t.wday=4
  t.yday=115
  t.isdst=false

You can directly use in in string.format()

print(string.format("%s:%s", os.date('%I'), os.date('%M')))


The question is are you trying to generate time strings? Or parse ones that already exist?
LUA:
local times = {"22:10", "23:15"}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    local timeNow = string.format("%s:%s", os.date('%I'), os.date('%M'))
   print(timeNow < times[1]) -- ok it print correct

   print(timeNow - times[1]) -- it get error

    return true
end

it get error because cant subtract string, I need to know how to make this... timeNow - times[1]
exemple:
time now in my pc is 23:40h, and times[1] is a table with time 22:10h.
I need return 23:40 - 22:10 = 01:30h
 
Ah, you want to turn strings into numbers?

What is your source of strings?


This will take "12:34" and spit out two numbers.
LUA:
function parseHHMM(time)
    local itr = string.gmatch(time, "%d%d")
    local hour, minute = tonumber(itr()), tonumber(itr())

    return hour, minute
end
 
Last edited:
Solution
Ah, you want to turn strings into numbers?

What is your source of strings?
exemple:
time now in my pc is 23:40h, and times[1] is a table with time 22:10h.
I need return 23:40 - 22:10 = 01:30h
LUA:
local times = {"22:10", "23:15"}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    local timeNow = MY_PC_CURRENTY_TIME  Exemple if my pc currenty time is 23:40
    print(timeNow - times[1])
    print(23:40 - 22:10)
   print(01:30)


    return true
end
 
exemple:
time now in my pc is 23:40h, and times[1] is a table with time 22:10h.
I need return 23:40 - 22:10 = 01:30h

537421342115627010.png

This is the point where I load a Lua library or make Lua bindings to a C library man. Lua is not the place to be doing this shit.

Your only hope is to get your source format into something sane, like UTC epochs on both sides, because Time Zones suck, even in languages with stellar string manipulation (which Lua does not qualify)

What is your source of timestamps? Do you have control over it? Can you choose the format you're dealing with?

LuaDate v2 (http://tieske.github.io/date/) <------ Call in reinforcements bro

Maybe you should say what you are actually trying to accomplish? Are you just trying to make something run every hour? Run at a certain time everyday? Because if you want time diffrences between two timestamps, you need the full time stamp, and not just the HH:MM. Otherwise we have to make a lot of assumptions, and making assumptions is very bad.
 
Last edited:
537421342115627010.png

This is the point where I load a Lua library or make Lua bindings to a C library man. Lua is not the place to be doing this shit.

Your only hope is to get your source format into something sane, like UTC epochs on both sides, because Time Zones suck, even in languages with stellar string manipulation (which Lua does not qualify)

What is your source of timestamps? Do you have control over it? Can you choose the format you're dealing with?

LuaDate v2 (http://tieske.github.io/date/) <------ Call in reinforcements bro

Maybe you should say what you are actually trying to accomplish? Are you just trying to make something run every hour? Run at a certain time everyday? Because if you want time diffrences between two timestamps, you need the full time stamp, and not just the HH:MM. Otherwise we have to make a lot of assumptions, and making assumptions is very bad.
I've explained it several times, I'm sad that you didn't understand.
I just need to do what I said, nothing more.
I need to take an current time Hour: Minute [IT'S OK] and subtract from another Hour: Minute. (only it).
1587366706888.png

but when i try to use currentTime - times_subtract, i get erros in tfs.
( attempt to perform arithmetic on local 'currentTime' (a string value))
LUA:
local times_subtract = "03:10"

function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    local currentTime = os.date("%H:%M")

    I NEED currentTime - times_subtract
    04:11 - 03:10 = 1:01
    return true
end

local currentTime = os.date("%H:%M")
it returns 04:11 [ITS OK]
but i cant use "currentTime - times_subtract", i want to know what is the correct form to subtract it!
 
Last edited:
Nah, I understand that you're saying. You're not understanding everything you are not saying:

From just your computer and your computer alone? Not the other players? That does make things simpler.

But!
Depending on which computer is which timezome, the math could end up backwards.
17 hours ahead instead of 7 hours behind, do you get it now?

If this really that simple and there is only one source of input, then why are you comparing timestamps? Won't your server always be the same distance of time away from your computer? Why not just substract that value from the server time stamp and call it a day?
 
Nah, I understand that you're saying. You're not understanding everything you are not saying:

From just your computer and your computer alone? Not the other players? That does make things simpler.

But!
Depending on which computer is which timezome, the math could end up backwards.
17 hours ahead instead of 7 hours behind, do you get it now?

If this really that simple and there is only one source of input, then why are you comparing timestamps? Won't your server always be the same distance of time away from your computer? Why not just substract that value from the server time stamp and call it a day?
os.time () takes the current value from the "host machine" and not the players' computer.
so the value will be the same for all players
 
Back
Top