It is currently September 20th, 2019, 5:31 pm

Formatting seconds into weeks, days, hours, minutes, seconds

Discuss the use of Lua in Script measures.
User avatar
jsmorley
Developer
Posts: 19467
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Formatting seconds into weeks, days, hours, minutes, seconds

jsmorley » January 7th, 2015, 5:06 am

Example skin to show how to format seconds into weeks, days, hours, minutes, seconds in Lua.

Skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[MeasureLuaTime]
Measure=Script
ScriptFile=#CURRENTPATH#LuaTime.lua

[MeterTimer]
Meter=String
MeasureName=MeasureLuaTime
FontSize=11
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
LuaTime.lua:

Code: Select all

function Initialize()

	startTime = os.time()
	
end

function Update()

	local currentTime = os.time()
	local elapsedSeconds = (currentTime - startTime)
	
	local weeks, days, hours, minutes, seconds = FormatSeconds(elapsedSeconds)
	
	local weeksTxt, daysTxt, hoursTxt, minutesTxt, secondsTxt = ""
	if weeks == 1 then weeksTxt = 'week' else weeksTxt = 'weeks' end
	if days == 1 then daysTxt = 'day' else daysTxt = 'days' end
	if hours == 1 then hoursTxt = 'hour' else hoursTxt = 'hours' end
	if minutes == 1 then minutesTxt = 'minute' else minutesTxt = 'minutes' end
	if seconds == 1 then secondsTxt = 'second' else secondsTxt = 'seconds' end
	
	if elapsedSeconds >= 604800 then
		return weeks..' '..weeksTxt..', '..days..' '..daysTxt..', '..hours..' '..hoursTxt..', '..minutes..' '..minutesTxt..', '..seconds..' '..secondsTxt
	elseif elapsedSeconds >= 86400 then
		return days..' '..daysTxt..', '..hours..' '..hoursTxt..', '..minutes..' '..minutesTxt..', '..seconds..' '..secondsTxt
	elseif elapsedSeconds >= 3600 then
		return hours..' '..hoursTxt..', '..minutes..' '..minutesTxt..', '..seconds..' '..secondsTxt
	elseif elapsedSeconds >= 60 then
		return minutes..' '..minutesTxt..', '..seconds..' '..secondsTxt
	else
		return seconds..' '..secondsTxt
	end

end

function FormatSeconds(secondsArg)

	local weeks = math.floor(secondsArg / 604800)
	local remainder = secondsArg % 604800
	local days = math.floor(remainder / 86400)
	local remainder = remainder % 86400
	local hours = math.floor(remainder / 3600)
	local remainder = remainder % 3600
	local minutes = math.floor(remainder / 60)
	local seconds = remainder % 60
	
	return weeks, days, hours, minutes, seconds
	
end
1.jpg
You do not have the required permissions to view the files attached to this post.