Cron help

Hi guys,

I have created a cron task inside server.lua that sends out a chatMessage at a specified time which works… However, it seems to be sending the chatMessage at the host time and not the server time. For instance, my server is based in Germany and my local machine is UK. Germany is 1 hour ahead so if I set the cron task to to send the message at 20:00 I would expect to receive that message at 19:00 UK time but I dont, I get it at 20:00 UK time.

Can anyone help with this or suggest a way to get it to send at server time and not client time?

Thanks in advance for your help, much appreciated.

CODE:

local Jobs     = {}
local LastTime = nil
local text1 = "SERVER RESTART IN 15 MINS"
function RunAt(h, m, cb)

	table.insert(Jobs, {
		h  = h,
		m  = m,
		cb = cb
	})

end

function GetTime()

	local timestamp = os.time()
	local d         = os.date('*t', timestamp).wday
	local h         = tonumber(os.date('%H', timestamp))
	local m         = tonumber(os.date('%M', timestamp))

	return {d = d, h = h, m = m}

end

function OnTime(d, h, m)

	for i=1, #Jobs, 1 do
		if Jobs[i].h == h and Jobs[i].m == m then
			Jobs[i].cb(d, h, m)
		end
	end

end

function Tick()

	local time = GetTime()

	if time.h ~= LastTime.h or time.m ~= LastTime.m then
		OnTime(time.d, time.h, time.m)
		LastTime = time
	end

	SetTimeout(60000, Tick)
end

LastTime = GetTime()

Tick()

AddEventHandler('cron:runAt', function(h, m, cb)
	RunAt(h, m, cb)
end)

-- Execute task 20:00, every day
function CronTask(d, h, m)
  print('Task done')
  TriggerClientEvent('chatMessage', -1, "ALERT", {255, 0, 0}, text1)
end

TriggerEvent('cron:runAt', 20, 00, CronTask)

I’ve found an answer to a similiar problem on stackoverflow.

So you need to offset your timestamp local variable in your GetTime function.
You can do this then:

local timestamp = os.time() - 1 * 60 * 60

This is not the best option I admit, because the offset should be a variable, which you retrieve from somewhere, but if you know for sure that the server is in Germany you could use UTC time instead: https://stackoverflow.com/questions/18982730/how-to-get-current-utc-time-in-lua-script
And offset that with a constant.

Have you even tried to use google? :smiley:

haha Yes I tried Google first but I didnt see that. Thanks for the suggestion, I will check it out now. :slight_smile: