overextended / ox_lib

A FiveM resource and script library for Lua and JS.
https://overextended.dev/ox_lib
GNU Lesser General Public License v3.0
282 stars 378 forks source link

[Suggestion] - lib.cron #585

Closed Mesrine67 closed 1 month ago

Mesrine67 commented 1 month ago

Would it be possible to synchronize the time of lib.cron with setr ox:locale or setr ox:localeTime?

thelindat commented 1 month ago

lib.cron uses system time...

Mesrine67 commented 1 month ago

example of the code that I want to propose, this is what I am talking about


local timeZones = {
    fr = "Europe/Paris",
    us = "America/New_York",
    jp = "Asia/Tokyo"
}
local defaultTimeZone = 'fr'

local function getTimeZone()
    local localTime = GetConvar('localTime', defaultTimeZone)
    return timeZones[localTime] or timeZones[defaultTimeZone]
end

local function formatDateTime(dateTime)
    local year, month, day, hour, min, sec = dateTime:match("(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)")
    if year and month and day and hour and min and sec then
        return string.format("%02d-%02d-%04d %02d:%02d:%02d", day, month, year, hour, min, sec)
    else
        return dateTime
    end
end

local function fetchCurrentDateTime(callback)
    local timeZone = getTimeZone()
    PerformHttpRequest("http://worldtimeapi.org/api/timezone/" .. timeZone, function(statusCode, response, headers)
        if statusCode == 200 then
            local data = json.decode(response)
            if data and data.datetime then
                local dateTime = formatDateTime(data.datetime)
                callback(dateTime)
            else
                callback(nil)
            end
        else
            callback(nil)
        end
    end, "GET", "", {})
end

RegisterCommand('time', function(source, args, rawCommand)
    fetchCurrentDateTime(function(dateTime)
        if dateTime then
            local notification = {
                id = 'current_time',
                title = 'Current Date and Time',
                description = string.format('**Date and Time:** %s', dateTime),
                duration = 5000,
                position = 'top-right',
                type = 'inform',
                style = {
                    backgroundColor = '#141517',
                    color = '#C1C2C5',
                    ['.description'] = {
                        color = '#00FF00'
                    }
                },
                icon = 'clock',
                iconColor = '#00FF00',
                iconAnimation = 'pulse'
            }
            TriggerClientEvent('ox_lib:notify', source, notification)
            lib.print.info(("Current DateTime for player %s: %s"):format(source, dateTime))
        else
            local notification = {
                id = 'time_fetch_failed',
                title = 'Error',
                description = '**Failed to fetch current date and time.**',
                duration = 5000,
                position = 'top-right',
                type = 'error',
                style = {
                    backgroundColor = '#FF0000',
                    color = '#FFFFFF',
                    ['.description'] = {
                        color = '#FFFFFF'
                    }
                },
                icon = 'times-circle',
                iconColor = '#FF0000',
                iconAnimation = 'shake'
            }
            TriggerClientEvent('ox_lib:notify', source, notification)
            lib.print.error(("Failed to fetch current date and time for player %s"):format(source))
        end
    end)
end, true)
thelindat commented 1 month ago

https://support.microsoft.com/en-us/windows/how-to-set-your-time-and-time-zone-dfaa7122-479f-5b98-2a7b-fa0b6e01b261

CeebDev commented 1 month ago

Wtf cron is using system time, Txadmin also for reboots etc

You need to change your system time. That's all