Template:GetPersianDate: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Blanked the page)
Tags: Blanking Manual revert
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
== getPersianDate ==
This function calculates and returns the Persian (Jalali) date based on the server's real time.


=== Syntax ===
<syntaxhighlight lang="lua">
getPersianDate()
</syntaxhighlight>
=== Returns ===
* '''year''': The Persian (Jalali) year.
* '''month''':  The Persian (Jalali) month.
* '''day''':  The Persian (Jalali) day.
=== Code ===
<section name="Server/Client side Script" class="both" show="true">
<syntaxhighlight lang="lua">
function getPersianDate()
    -- Get the current real time
    local time = getRealTime()
   
    -- Extract Gregorian year, month, and day
    local gy = time.year + 1900
    local gm = time.month + 1
    local gd = time.monthday
    -- Table of cumulative days for each month in a non-leap year
    local g_d_m = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}
   
    -- Adjust the year if the month is after February (for leap year calculation)
    local gy2 = (gm > 2) and (gy + 1) or gy
   
    -- Calculate the total number of days since a fixed starting point
    local days = 355666 + (365 * gy) + math.floor((gy2 + 3) / 4) - math.floor((gy2 + 99) / 100) + math.floor((gy2 + 399) / 400) + gd + g_d_m[gm]
   
    -- Calculate the Jalali year
    local jy = -1595 + (33 * math.floor(days / 12053))
    days = days % 12053
    jy = jy + (4 * math.floor(days / 1461))
    days = days % 1461
   
    -- Adjust the Jalali year if there are remaining days
    if days > 365 then
        jy = jy + math.floor((days - 1) / 365)
        days = (days - 1) % 365
    end
   
    -- Calculate the Jalali month and day
    local jm = (days < 186) and math.floor((days - 1) / 31) + 1 or math.floor((days - 186) / 30) + 7
    local jd = (days < 186) and ((days - 1) % 31) + 1 or ((days - 186) % 30) + 1
    -- Return the Persian (Jalali) date as a table
    return {year = jy, month = jm, day = jd}
end
</syntaxhighlight>
</section>
== Example ==
<section name="client" class="client" show="true">
<syntaxhighlight lang="lua">
addCommandHandler("date", function()
    local PersianDate = getPersianDate()
    outputChatBox("Persian Date: " .. PersianDate.year .. "/" .. PersianDate.month .. "/" .. PersianDate.day)
end)
</syntaxhighlight>
</section>
<section name="server" class="server" show="true">
<syntaxhighlight lang="lua">
addCommandHandler("date", function(player)
    local PersianDate = getPersianDate()
    outputChatBox("Persian Date: " .. PersianDate.year .. "/" .. PersianDate.month .. "/" .. PersianDate.day, player)
end)
</syntaxhighlight>
</section>

Latest revision as of 22:00, 14 January 2025