Jump to content

Quest Function - Time Calculation


Recommended Posts

  • Premium

M2 Download Center

This is the hidden content, please
( Internal )

        function ToTimeFormat(seconds)
            local time = tonumber(seconds)
            if time <= 0 then
                return "00:00:00";
            else
                hours = string.format("%02.f", math.floor(time/3600));
                mins = string.format("%02.f", math.floor(time/60 - (hours*60)));
                secs = string.format("%02.f", math.floor(time - hours*3600 - mins *60));
                return hours..":"..mins..":"..secs
            end
        end

Without waste resources with loop or shits for exhibitionism

Edited by WeedHex
  • Metin2 Dev 23
  • kekw 1
  • Eyes 1
  • Good 3
  • Love 1
  • Love 9
Link to comment
https://metin2.dev/topic/18923-quest-function-time-calculation/
Share on other sites

  • Honorable Member
--[[
    Description:
        Returns a string representing the total time in either the "Days, Hours, Minutes and Seconds" or "d:h:m:s" format.

    Arguments:
        sec (number): The total amount of seconds.
        format_type (string): Optional parameter, either "verbose" or "clock".
        simplify (boolean): Optional parameter, valid only if format_type is "clock". If true, the labels ("d", "h", "m", "s") will be omitted.
    
    Example:
        get_time_format(52165786, "verbose") -- Returns "603 days, 18 hours, 29 minutes and 46 seconds"
        get_time_format(52165786, "clock") -- Returns "603d:18h:29m:46s"
        get_time_format(52165786, "clock", true) -- Returns "603:18:29:46"

    Returns:
        string: A string representing the total time.

    Time complexity: O(1).
    Space complexity: O(1).
]]
get_time_format = function(sec, format_type, simplify)
    format_type = format_type or "verbose";
    simplify = simplify or false;

    local days = math.floor(sec / time_day_to_sec(1));
    local hours = math.floor(math.mod(sec, time_day_to_sec(1)) / time_hour_to_sec(1));
    local minutes = math.floor(math.mod(sec, time_hour_to_sec(1)) / time_min_to_sec(1));
    local seconds = math.mod(sec, time_min_to_sec(1));

    if (format_type == "verbose") then
        local parts = {};
        local function add_part(quantity, unit)
            if (quantity > 0) then
                table.insert(parts, string.format("%d %s%s", quantity, unit, quantity > 1 and "s" or ""));
            end -- if
        end -- function

        add_part(days, "day");
        add_part(hours, "hour");
        add_part(minutes, "minute");
        add_part(seconds, "second");

        if (table.getn(parts) > 1) then
            parts[table.getn(parts) - 1] = string.format("%s and %s", parts[table.getn(parts) - 1], parts[table.getn(parts)]);
            table.remove(parts);
        end -- if

        return table.concat(parts, ", ");

    elseif (format_type == "clock") then
        if (simplify) then
            return string.format("%02d:%02d:%02d:%02d", days, hours, minutes, seconds);
        end -- if

        return string.format("%02dd:%02dh:%02dm:%02ds", days, hours, minutes, seconds);
    end -- if/elseif
end -- function

 

Edited by Syreldar
Updated.
  • Lmao 1
  • Love 6

 

"Nothing's free in this life.

Ignorant people have an obligation to make up for their ignorance by paying those who help them.

Either you got the brains or cash, if you lack both you're useless."

Syreldar

  • 5 years later...
  • Honorable Member

I have updated the function to current @ WeedHex, although you could've checked on my functions file which i constantly keep updated.. this post was almost 6 years old.

This version supports clock and simplified formats.

  • Love 1

 

"Nothing's free in this life.

Ignorant people have an obligation to make up for their ignorance by paying those who help them.

Either you got the brains or cash, if you lack both you're useless."

Syreldar

Don't use any images from : imgur, turkmmop, freakgamers, inforge, hizliresim... Or your content will be deleted without notice...
Use : https://metin2.download/media/add/

Please use https://metin2.download/ when uploading files smaller than 100MB, otherwise the approval will take longer due to manual upload.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.