mtimer/system/get_times.lua

151 lines
5.2 KiB
Lua
Raw Normal View History

2019-02-21 14:37:58 +01:00
local m = mtimer
local S = m.translator
2019-02-21 14:37:58 +01:00
local get_date_names = function (dates)
-- %w -> weekday [0-6 = Sunday-Saturday]
local weekdays = {
S('Sunday'), S('Monday'), S('Tuesday'), S('Wednesday'), S('Thursday'),
S('Friday'), S('Saturday')
}
-- %m -> month [01-12 = January-December]
local months = {
S('January'), S('February'), S('March'), S('April'), S('May'),
S('June'), S('July'), S('August'), S('September'), S('Oktober'),
S('November'), S('December')
}
return {
day = weekdays[tonumber(dates.day)],
month = months[tonumber(dates.month)]
}
end
local get_real_time_universal = function (player_name, time_type)
2019-02-21 14:37:58 +01:00
local player = minetest.get_player_by_name(player_name)
local player_meta = player:get_meta()
local m_meta = m.meta
local timezone_offset = player_meta:get_string(m_meta.timezone_offset.key)
local server_timestamp = ''
local local_timestamp = ''
local format = ''
2019-02-21 14:37:58 +01:00
if time_type == 'real' then
server_timestamp = os.time()
local_timestamp = server_timestamp + ((timezone_offset*60)*60)
format = player_meta:get_string(m_meta.real_time.key)
elseif time_type == 'session' then
server_timestamp = player_meta:get('mtimer:session_start')
local_timestamp = server_timestamp + ((timezone_offset*60)*60)
format = player_meta:get_string(m_meta.session_start_time.key)
end
local date_names = get_date_names({
day = os.date('!%w', local_timestamp),
month = os.date('!%m', local_timestamp)
})
local values = {
times = {
server_time = os.date('%Y-%m-%dT%T', server_timestamp),
local_time = os.date('!%Y-%m-%dT%T', local_timestamp),
offset = timezone_offset,
},
variables = {
hours_24 = os.date('!%H', local_timestamp),
hours_12 = os.date('!%I', local_timestamp),
minutes = os.date('!%M', local_timestamp),
seconds = os.date('!%S', local_timestamp),
dayname = date_names.day,
monthname = date_names.month,
year = os.date('!%Y', local_timestamp),
month = os.date('!%m', local_timestamp),
day = os.date('!%d', local_timestamp),
iso8601_date = os.date('!%Y-%m-%d', local_timestamp),
iso8601_time = os.date('!%H:%M:%S', local_timestamp),
timestamp = local_timestamp
},
format = format
2019-02-21 14:37:58 +01:00
}
values['formatted'] = format:gsub('{[a-z0-9]+}', {
['{24h}'] = values.variables.hours_24,
['{12h}'] = values.variables.hours_12,
['{min}'] = values.variables.minutes,
['{sec}'] = values.variables.seconds,
['{dname}'] = values.variables.dayname,
['{mname}'] = values.variables.monthname,
['{year}'] = values.variables.year,
['{month}'] = values.variables.month,
['{day}'] = values.variables.day,
['{isodate}'] = values.variables.iso8601_date,
['{isotime}'] = values.variables.iso8601_time,
['{timestamp}'] = values.variables.timestamp
})
return values
2019-02-21 14:37:58 +01:00
end
local get_ingame_time = function (player_name)
local player = minetest.get_player_by_name(player_name)
local format = player:get_meta():get_string(m.meta.ingame_time.key)
local time_of_day = tostring((minetest.get_timeofday() * 24000) * 3.6)
local ingame_timestamp = tonumber(string.format('%.0f', time_of_day))
local values = {
hours_24 = os.date('!%H', ingame_timestamp),
hours_12 = os.date('!%I', ingame_timestamp),
minutes = os.date('!%M', ingame_timestamp),
ingame_timestamp = ingame_timestamp,
format = format
}
values['formatted'] = format:gsub('{[a-z0-9]+}', {
['{24h}'] = values.hours_24,
['{12h}'] = values.hours_12,
['{min}'] = values.minutes,
['{its}'] = values.ingame_timestamp
})
return values
end
local get_session_duration = function (player_name)
local player = minetest.get_player_by_name(player_name)
local player_meta = player:get_meta()
local format = player_meta:get_string(m.meta.session_duration.key)
local start_timestamp = player_meta:get_string('mtimer:session_start')
local current_timestamp = os.time()
local difference = current_timestamp - start_timestamp
local values = {
format = format,
difference = difference,
days = string.format('%02d', math.floor(difference/86400)),
hours = string.format('%02d', math.floor((difference % 86400)/3600)),
minutes = string.format('%02d', math.floor((difference % 3600)/60)),
seconds = string.format('%02d', math.floor((difference % 60)))
}
values['formatted'] = format:gsub('{[a-z0-9]+}', {
['{days}'] = values.days,
['{hours}'] = values.hours,
['{minutes}'] = values.minutes,
['{seconds}'] = values.seconds
})
return values
end
2019-02-21 14:37:58 +01:00
mtimer.get_times = function (player_name)
return {
session_start_time = get_real_time_universal(player_name, 'session'),
session_duration = get_session_duration(player_name),
real_time = get_real_time_universal(player_name, 'real'),
ingame_time = get_ingame_time(player_name)
2019-02-21 14:37:58 +01:00
}
end