So basically I'm confused on how I'd make it so that I can convert DD:HH:MM:SS to only seconds while taking into account the amount of numbers there are. (Sorry if I make 0 sense, you should definitely know what I mean by the example below.)
print("05:00":FormatToSeconds()) -- 5 minutes and 0 seconds
-- 300
print("10:30:15":FormatToSeconds()) -- 10 hours, 30 minutes and 15 seconds
-- 37815
print("1:00:00:00":FormatToSeconds()) -- 1 day
-- 86400
print("10:00:00:30":FormatToSeconds()) -- 10 days, 30 seconds
-- 864030
So on and so forth. I think that maybe using gmatch would work but still idk. Help would be greatly appreciated.
Edit:
So I've tried doing it with gmatch, but I don't know if this is the most fastest way of doing this (which it probably isn't), so any help would still be appreciated.
(My code)
function ConvertTimeToSeconds(Time)
local Thingy = {}
local TimeInSeconds = 0
for v in string.gmatch(Time, "%d+") do
if tonumber(string.sub(v, 1, 1)) == 0 then
table.insert(Thingy, tonumber(string.sub(v, 2, 2)))
else
table.insert(Thingy, tonumber(v))
end
end
if #Thingy == 1 then
TimeInSeconds = TimeInSeconds + Thingy[1]
elseif #Thingy == 2 then
TimeInSeconds = TimeInSeconds + (Thingy[1] * 60) + Thingy[2]
elseif #Thingy == 3 then
TimeInSeconds = TimeInSeconds + (Thingy[1] * 60 * 60) + (Thingy[2] * 60) + Thingy[3]
elseif #Thingy == 4 then
TimeInSeconds = TimeInSeconds + (Thingy[1] * 24 * 60 * 60) + (Thingy[2] * 60 * 60) + (Thingy[3] * 60) + Thingy[4]
end
return TimeInSeconds
end
print(ConvertTimeToSeconds("1:00:00:00"))
Don't worry about execution speed before doing any actual measurements unless you're designing a time-critical program. In any extreme situation you'd probably want to offload risky parts to a C module.
Your approach is just fine. There are parts you can clean up: you can just return the results of calculations as TimeInSeconds doesn't actually act as accumulator in your case; tonumber handles '00' just fine and it can ensure decimal integers with an argument (since 5.3).
I'd go the other way and describe factors in a table:
local Factors = {1, 60, 60 * 60, 60 * 60 * 24}
local
function ConvertTimeToSeconds(Time)
local Components = {}
for v in string.gmatch(Time, "%d+") do
table.insert(Components, 1, tonumber(v, 10))
end
if #Components > #Factors then
error("unexpected time component")
end
local TimeInSeconds = 0
for i, v in ipairs(Components) do
TimeInSeconds = TimeInSeconds + v * Factors[i]
end
return TimeInSeconds
end
Of course, both implementations have problem with pattern being naïve as it would match e.g., '00 what 10 ever 10'. To fix that, you could go another route of using string.match with e.g., '(%d+):(%d+):(%d+):(%d+)' and enforcing strict format, or matching each possible variant.
Otherwise you can go all in and use LPeg to parse the duration.
Another way would be to not use strings internally, but instead convert them into a table like {secs=10, mins=1, hours=10, days=1} and then use these tables instead - getting seconds from that representation would be straight-forward.
I'm looking for a way to run a Jenkins build every 2 hours, Monday through Friday only from 7 AM to 10PM. I got 7AM to 10PM and every 2 hours part down but how would I go about adding Monday through Friday to it?
Here is the schedule right now: H 07-22/2 * * *
If I add 1-5 to the above (1 for Monday and 5 for Friday), it gives me an error
" Invalid input: "H 07-22/2 * * * 1-5": line 1:16: expecting EOF,
found ' ' "
Please help me out.
Thanks.
You have too much element in your cron expression.
0 07-22/2 * * 1-5 instead of 0 07-22/2 * * * 1-5
Is it possible to create a CronExpression with: "fire every 5 min but not run at 00:05 and 00:10"?
org.quartz.CronScheduleBuilder.cronSchedule("0 0/5 * * * ?")
You will have to use two expressions:
0 15/5 0 * * ?
0 0/5 1-23 * * ?
The first expression is specific to 12 AM, the second is for the rest of the time.
Is there any internal function in Perl to convert binary number with fractional part into decimal? I know how to convert integer numbers but what about numbers with a decimal point?
e.g. 1010.1012 = 10.62510
No, there's no builtin that does this.
However, it's easy to calculate. 1010.1012 is simply 10101012 / 23, so
Remove the ".".
Convert the resulting integer.
Divide by 2**$decimal_places.
Or you could do the conversion yourself. 1010.1012 is
1 * 2**3
0 * 2**2
1 * 2**1
0 * 2**0
.
1 * 2**(-1)
0 * 2**(-2)
+ 1 * 2**(-3)
--------------
10.625
Is it possible to create a CronExpression with:
"fire every day every 20min but not on Saturday between 10:00 and 14:00"?
Something like "0 0/20 * ? * MON-SAT" is clear, but it is not the same...
You will have to use two expressions; One for saturdays, one for all other days:
0 0/20 * * * SUN-FRI command
0 0/20 0-9,14-23 * * SAT command