Module: Console::Clock

Defined in:
lib/console/clock.rb

Overview

A simple clock utility for tracking and formatting time.

Class Method Summary collapse

Class Method Details

.formatted_duration(duration) ⇒ Object

Format a duration in seconds as a human readable string.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/console/clock.rb', line 14

def self.formatted_duration(duration)
	if duration < 60.0
		return format("%.2fs", duration)
	end
	
	minutes = duration / 60.0
	
	if minutes < 60.0
		seconds = duration % 60
		return format("%dm%02ds", minutes, seconds)
	end
	
	hours = minutes / 60.0
	
	if hours < 24.0
		minutes = minutes % 60
		return format("%dh%02dm", hours, minutes)
	end
	
	days = hours / 24.0
	
	if days < 100.0
		hours = hours % 24
		return format("%dd%02dh", days, hours)
	end
	
	return format("%dd", days)
end

.nowObject



44
45
46
# File 'lib/console/clock.rb', line 44

def self.now
	::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
end