모듈:시간변환
창팝위키
다른 명령
설명문서
local p = {}
function p.mm_ss_to_seconds(frame)
local time_str = frame.args[1]
if not time_str or time_str == "" then
return "" -- 입력값이 없거나 빈 문자열이면 빈 문자열 반환
end
local parts = mw.text.split(time_str, ":")
-- mm:ss 형식인지 확인 (두 부분으로 나뉘었는지)
if #parts == 2 then
local minutes = tonumber(parts[1])
local seconds = tonumber(parts[2])
-- 분과 초가 유효한 숫자인지 (nil이 아닌지) 그리고 음수가 아닌지 확인
if minutes ~= nil and seconds ~= nil and minutes >= 0 and seconds >= 0 then
return tostring(minutes * 60 + seconds)
end
end
-- 유효하지 않은 형식이나 값인 경우 오류 메시지 반환
return "<span style=\"color:red;\">오류: 잘못된 시간 형식 (mm:ss)</span>"
end
function p.iso8601_duration_to_seconds(frame)
local duration = frame.args['duration'] or frame.args[1]
-- 'P'로 시작하는지 확인
if not string.match(duration, "^P") then
return "<span class=\"error\">오류: 잘못된 시간 형식</span>"
end
local timePart = mw.ustring.match(duration, "T.*") or ""
local dayPart = mw.ustring.match(duration, "^P.-T") and mw.ustring.match(duration, "^P(.-)T") or mw.ustring.match(duration, "^P(.+)")
local totalSeconds = 0
-- 날짜 구성 요소
local days = tonumber(mw.ustring.match(dayPart or "", "(%d+)D")) or 0
-- 시간 구성 요소
local hours = tonumber(mw.ustring.match(timePart, "(%d+)H")) or 0
local minutes = tonumber(mw.ustring.match(timePart, "(%d+)M")) or 0
local seconds = tonumber(mw.ustring.match(timePart, "(%d+)S")) or 0
totalSeconds = totalSeconds + (days * 86400)
totalSeconds = totalSeconds + (hours * 3600)
totalSeconds = totalSeconds + (minutes * 60)
totalSeconds = totalSeconds + seconds
return totalSeconds
end
function p.seconds_to_m_ss(frame)
local seconds_str
if type(frame) == 'table' and frame.args then
seconds_str = frame.args[1] -- 모듈 호출 시 첫 번째 인자 (예: {{#invoke:모듈이름|seconds_to_m_ss|123}})
else
seconds_str = frame -- 직접 함수 호출 시 (예: p.seconds_to_m_ss("123"))
end
local totalSeconds = tonumber(seconds_str)
if totalSeconds == nil then
return "<span class=\"error\">오류: 유효하지 않은 초 값</span>"
end
local minutes = math.floor(totalSeconds / 60)
local seconds = totalSeconds % 60
-- 초가 한 자리 수일 경우 앞에 0을 붙여 두 자리로 만듭니다.
local formattedSeconds = string.format("%02d", seconds)
return string.format("%d:%s", minutes, formattedSeconds)
end
return p