Class: Time::Beat

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/time/beat.rb,
lib/time/beat/version.rb

Constant Summary collapse

VERSION =
"1.0.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(beats = BigDecimal("0.0")) ⇒ Beat

Returns a new instance of Beat.



33
34
35
36
37
# File 'lib/time/beat.rb', line 33

def initialize(beats = BigDecimal("0.0"))
  raise "beat value out of range" unless beats >= 0 && beats < 1000

  self.beats = beats
end

Instance Attribute Details

#beatsObject

Returns the value of attribute beats.



31
32
33
# File 'lib/time/beat.rb', line 31

def beats
  @beats
end

Class Method Details

.from_str(s) ⇒ Object



19
20
21
22
23
# File 'lib/time/beat.rb', line 19

def self.from_str(s)
  m = /^@(\d{3}\.\d{3})$/.match(s)
  raise "invalid beat string" if m.nil?
  Time::Beat.new(BigDecimal(m.captures.first))
end

.from_time(t) ⇒ Object



13
14
15
16
17
# File 'lib/time/beat.rb', line 13

def self.from_time(t)
  t = t.utc
  offset = t.sec + (t.min * 60) + (t.hour * 3600)
  Time::Beat.new(BigDecimal(offset) / BigDecimal("86.4"))
end

.nowObject



25
26
27
# File 'lib/time/beat.rb', line 25

def self.now
  from_time(Time.now)
end

Instance Method Details

#<=>(other) ⇒ Object



39
40
41
# File 'lib/time/beat.rb', line 39

def <=>(other)
  beats <=> other.beats
end

#to_sObject



43
44
45
# File 'lib/time/beat.rb', line 43

def to_s
  format("@%03d.%03d", beats.truncate, (1000 * beats.frac).round)
end

#to_timeObject



47
48
49
50
51
52
# File 'lib/time/beat.rb', line 47

def to_time
  t = Time.now.utc
  t = Time.new(t.year, t.mon, t.day, 0, 0, 0, in: "UTC")
  t += (86.4 * beats).round
  t
end