Custom Time Formatting

  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

Jacob105

Active Member
Aug 7, 2020
73
1
8
24
Right, so I'm making a time formatting system because I'm not really a big fan of skript's default system (annoying decimals, weird times, ex 1 year = 365 days??)
but unfortunately, it isn't showing the correct time formats.
[1y shows up as 1 year and 1 day, oh GOD it's a headache]
/timetest is the debug command I used for this
Code:
function getTimeString(input: timespan) :: string:   
    
    set {_now} to now
    add {_input} to {_now}
    set {_time} to unix timestamp of {_now}
    remove (unix timestamp of now) from {_time}

    set {_years} to floor({_time} / 31536000)
    set {_months} to floor(mod({_time} / 2505600, 12))
    set {_weeks} to floor(mod({_time} / 604800, 4))
    set {_days} to floor(mod({_time} / 86400, 7))
    set {_hours} to floor(mod({_time} / 3600, 24))
    set {_minutes} to floor(mod({_time} / 60, 60))
    set {_seconds} to floor(mod({_time} / 1, 60))

    set {_string} to ""
    if {_years} > 0:
        if {_years} > 1:
            set {_string} to "%{_years}% years"
        else:
            set {_string} to "%{_years}% year"
    if {_months} > 0:
        if {_string} != "":
            if {_months} > 1:
                set {_string} to "%{_string}% %{_months}% months"
            else:
                set {_string} to "%{_string}% %{_months}% month"
        else:
            if {_months} > 1:
                set {_string} to "%{_months}% months"
            else:
                set {_string} to "%{_months}% month"
    if {_weeks} > 0:
        if {_string} != "":
            if {_weeks} > 1:
                set {_string} to "%{_string}% %{_weeks}% weeks"
            else:
                set {_string} to "%{_string}% %{_weeks}% week"
        else:
            if {_weeks} > 1:
                set {_string} to "%{_weeks}% weeks"
            else:
                set {_string} to "%{_weeks}% week"
    if {_days} > 0:
        if {_string} != "":
            if {_days} > 1:
                set {_string} to "%{_string}% %{_days}% days"
            else:
                set {_string} to "%{_string}% %{_days}% day"
        else:
            if {_days} > 1:
                set {_string} to "%{_days}% days"
            else:
                set {_string} to "%{_days}% day"
    if {_hours} > 0:
        if {_string} != "":
            if {_hours} > 1:
                set {_string} to "%{_string}% %{_hours}% hours"
            else:
                set {_string} to "%{_string}% %{_hours}% hour"
        else:
            if {_hours} > 1:
                set {_string} to "%{_hours}% hours"
            else:
                set {_string} to "%{_hours}% hour"
    if {_minutes} > 0:
        if {_string} != "":
            if {_minutes} > 1:
                set {_string} to "%{_string}% %{_minutes}% minutes"
            else:
                set {_string} to "%{_string}% %{_minutes}% minute"
        else:
            if {_minutes} > 1:
                set {_string} to "%{_minutes}% minutes"
            else:
                set {_string} to "%{_minutes}% minute"
    if {_seconds} > 0:
        if {_string} != "":
            if {_seconds} > 1:
                set {_string} to "%{_string}% %{_seconds}% seconds"
            else:
                set {_string} to "%{_string}% %{_seconds}% second"
        else:
            if {_seconds} > 1:
                set {_string} to "%{_seconds}% seconds"
            else:
                set {_string} to "%{_seconds}% second"

    if {_string} is "":
        set {_string} to "0 seconds"

    return {_string}


command /timetest [<string>] [<text>]:
    trigger:
        send "&aYour input: %arg-1%"
        set {_s} to arg-1
        replace all "y" with " year " in {_s}
        replace all "d" with " day " in {_s}
        replace all "s" with " second " in {_s}
        replace all "h" with " hour " in {_s}
        replace all "m" with " month " in {_s}
        replace all "mi" with " minute " in {_s}
        replace all "w" with " week " in {_s}
        set {_s} to subtext of {_s} from characters 1 to (length of {_s}-1)
        send "&eWhat I parsed: %{_s}%"
        set {_s} to {_s} parsed as timespan
        send "&6Skript TS: %{_s}%"
        send "&cgetTimeString: %getTimeString({_s})%"