Jump to content

Getting unix time


Recommended Posts

Posted

Hello... I am looking for an func which gives me actual unix time... but I can't find any... os.time() doesnt work and MTA function getRealTime() is able to return timestamp only in MTA 1.1+

Is there any other way than getting {second, minute, day, month, year} from getRealTime() and calculate it by me own? Thanks

Posted

Do you mean this?

local realTime = getRealTime() 
date = string.format("%04d/%02d/%02d", realTime.year + 1900, realTime.month + 1, realTime.monthday ) 
time = string.format("%02d:%02d", realTime.hour, realTime.minute ) 

Posted

I think here's what you looking for:

https://wiki.multitheftauto.com/wiki/Useful_Functions

The script:

function isLeapYear(year) 
    if year then year = math.floor(year) 
    else year = getRealTime().year + 1900 end 
    return ((year % 4 == 0 and year % 100 ~= 0) or year % 400 == 0) 
end 
  
function getTimestamp(year, month, day, hour, minute, second) 
    -- initiate variables 
    local monthseconds = { 2678400, 2419200, 2678400, 2592000, 2678400, 2592000, 2678400, 2678400, 2592000, 2678400, 2592000, 2678400 } 
    local timestamp = 0 
    local datetime = getRealTime() 
    year, month, day = year or datetime.year + 1900, month or datetime.month + 1, day or datetime.monthday 
    hour, minute, second = hour or datetime.hour, minute or datetime.minute, second or datetime.second 
  
    -- calculate timestamp 
    for i=1970, year-1 do timestamp = timestamp + (isLeapYear(i) and 31622400 or 31536000) end 
    for i=1, month-1 do timestamp = timestamp + ((isLeapYear(year) and i == 2) and 2505600 or monthseconds[i]) end 
    timestamp = timestamp + 86400 * (day - 1) + 3600 * hour + 60 * minute + second 
  
    timestamp = timestamp - 3600 --GMT+1 compensation 
    if datetime.isdst then timestamp = timestamp - 3600 end 
  
    return timestamp 
end 

For me it returned: 1305222073

Posted

Hm, thanks... But I think there must be something "easier" anyway... because thoose things u use to get Unix time are originaly calculated from Unix time, it must got it from some alternative func to os.time...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...