Improved format_time

This commit is contained in:
Cooldude2606
2019-03-24 17:37:33 +00:00
parent 6f6a1732ce
commit 2f4f100b63
2 changed files with 58 additions and 55 deletions

View File

@@ -135,64 +135,75 @@ function Public.ext_require(path,...)
return Public.extract_keys(rtn,...) return Public.extract_keys(rtn,...)
end end
--- Formats ticks into a time format - this is alot of work and will do later --- Formats tick into a clean format, denominations from highest to lowest
-- time denominations: D,H,M,S,T days,hours,minutes,seconds,ticks -- long will use words rather than letters
-- time prefixes (minutes as example): %m,%m,%M,%MM just the value, value with short tag, value with long tag -- time will use : separates
-- adding a number after the prefix AND denomination will show that many decimal palaces -- when a denomination is false it will overflow into the next one
-- examples: '%H %M' => '0H 0M'; '%MM and %SS3' => '0 Minutes and 0.000 Seconds'
function Public.format_time(ticks,format)
local has_days, has_hours, has_minutes, has_seconds, has_ticks = false,false,false,false,false
local max_days, max_hours = ticks/5184000, ticks/216000
local max_minutes, max_seconds, max_ticks = ticks/3600, ticks/60, ticks
local days, hours = max_days, max_hours-math.floor(max_days)*5184000
local minutes, seconds = max_minutes-math.floor(max_hours)*216000, max_seconds-math.floor(max_minutes)*3600
local tags = {}
return 'Use format_time_simple currently WIP'
end
--- Formats tick into a time format, this format is predefined to either H:M:S; HH MM SS or H Hours M Minutes S seconds
-- seconds are not required to be shown with option show_seconds = false, true to show them, default false
-- show_sub_seconds will show three decimal places for the seconds
-- long_format will use words rather than letters
-- tagged is default to true when false it will remove all letters and use :
-- @tparam ticks number the number of ticks that represents a time -- @tparam ticks number the number of ticks that represents a time
-- @tparam options table a table of options to use for the format -- @tparam options table a table of options to use for the format
function Public.format_time_simple(ticks,options) -- @treturn string a locale string that can be used
function Public.format_time(ticks,options)
-- Sets up the options -- Sets up the options
options = { options = options or {
show_seconds = options.show_seconds or false, days=false,
show_sub_seconds = options.show_sub_seconds or false, hours=true,
long_format = options.long_format or false, minutes=true,
tagged = options.tagged or true seconds=false,
long=false,
time=false
} }
-- Basic numbers that are used in calculations -- Basic numbers that are used in calculations
local max_hours, max_minutes, max_seconds = ticks/216000, ticks/3600, ticks/60 local max_days, max_hours, max_minutes, max_seconds = ticks/5184000, ticks/216000, ticks/3600, ticks/60
local hours, minutes, seconds = max_hours, max_minutes-math.floor(max_hours)*216000, max_seconds-math.floor(max_minutes)*3600 local days, hours = max_days, max_hours-math.floor(max_days)*24
local minutes, seconds = max_minutes-math.floor(max_hours)*60, max_seconds-math.floor(max_minutes)*60
-- Handles overflow of disabled denominations
local rtn_days, rtn_hours, rtn_minutes, rtn_seconds = math.floor(days), math.floor(hours), math.floor(minutes), math.floor(seconds)
if not options.days then
rtn_hours = rtn_hours + rtn_days*24
end
if not options.hours then
rtn_minutes = rtn_minutes + rtn_hours*60
end
if not options.minutes then
rtn_seconds = rtn_seconds + rtn_minutes*60
end
-- Format options -- Format options
local suffix = 'time-format.short-' local suffix = 'time-symbol-'
if options.long_format then local suffix_2 = '-short'
suffix = 'time-format.long-' if options.long then
suffix = ''
suffix_2 = ''
end end
local div = 'time-format.simple-format-tagged' local div = 'time-format.simple-format-tagged'
if options.tagged then if options.time then
div = 'time-format.simple-format-div' div = 'time-format.simple-format-div'
suffix = false suffix = false
end end
-- The returned numbers in the right format -- Adds formatting
local rtn_hours, rtn_minutes, rtn_seconds = math.floor(hours), math.floor(minutes), math.floor(seconds) if suffix ~= false then
if suffix then rtn_days = {suffix..'days'..suffix_2,rtn_days}
rtn_hours = {suffix..'hours',rtn_hours} rtn_hours = {suffix..'hours'..suffix_2,rtn_hours}
rtn_minutes = {suffix..'minutes',rtn_minutes} rtn_minutes = {suffix..'minutes'..suffix_2,rtn_minutes}
if options.show_sub_seconds then rtn_seconds = {suffix..'seconds'..suffix_2,rtn_seconds}
rtn_seconds = {suffix..'seconds',string.format('%d03',seconds)}
else else
rtn_seconds = {suffix..'seconds',rtn_seconds} rtn_days = string.format('%02d',rtn_days)
end rtn_hours = string.format('%02d',rtn_hours)
rtn_minutes = string.format('%02d',rtn_minutes)
rtn_seconds = string.format('%02d',rtn_seconds)
end end
-- The final return is construed -- The final return is construed
local rtn = {div,rtn_hours,rtn_minutes} local rtn
if options.show_seconds then if options.days then
rtn = {div,rtn,rtn_seconds} rtn = rtn_days
end
if options.hours then
rtn = rtn and {div,rtn,rtn_hours} or rtn_hours
end
if options.minutes then
rtn = rtn and {div,rtn,rtn_minutes} or rtn_minutes
end
if options.seconds then
rtn = rtn and {div,rtn,rtn_seconds} or rtn_seconds
end end
return rtn return rtn
end end

View File

@@ -1,3 +1,5 @@
time-symbol-days-short=__1__d
[expcore-commands] [expcore-commands]
unauthorized=Unauthorized, Access is denied due to invalid credentials unauthorized=Unauthorized, Access is denied due to invalid credentials
reject-string-options=Invalid Option, Must be one of: __1__ reject-string-options=Invalid Option, Must be one of: __1__
@@ -20,13 +22,3 @@ command-error-log-format=[ERROR] command/__1__ :: __2__
simple-format-none=__1__ simple-format-none=__1__
simple-format-div=__1__:__2__ simple-format-div=__1__:__2__
simple-format-tagged=__1__ __2__ simple-format-tagged=__1__ __2__
long-days=__1__ __plural_for_parameter_1_{1=Day|rest=Days}__
short-days=__1__D
long-hours=__1__ __plural_for_parameter_1_{1=Hour|rest=Hours}__
short-hours=__1__H
long-minutes=__1__ __plural_for_parameter_1_{1=Minute|rest=Minutes}__
short-minutes=__1__M
long-seconds=__1__ __plural_for_parameter_1_{1=Second|rest=Seconds}__
short-seconds=__1__S
long-ticks=__1__ __plural_for_parameter_1_{1=Tick|rest=Ticks}__
short-ticks=__1__T