Class: Arrow::Time

Inherits:
Object
  • Object
show all
Defined in:
lib/arrow/time.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unit, value) ⇒ Time

Returns a new instance of Time.



22
23
24
25
26
# File 'lib/arrow/time.rb', line 22

def initialize(unit, value)
  @unit = unit
  @value = value
  @unconstructed = false
end

Instance Attribute Details

#unitObject (readonly)

Returns the value of attribute unit.



20
21
22
# File 'lib/arrow/time.rb', line 20

def unit
  @unit
end

#valueObject (readonly)

Returns the value of attribute value.



21
22
23
# File 'lib/arrow/time.rb', line 21

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/arrow/time.rb', line 28

def ==(other)
  other.is_a?(self.class) and
    positive? == other.positive? and
    hour == other.hour and
    minute == other.minute and
    second == other.second and
    nano_second == other.nano_second
end

#cast(target_unit) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/arrow/time.rb', line 37

def cast(target_unit)
  return self.class.new(@unit, @value) if @unit == target_unit

  target_value = (hour * 60 * 60) + (minute * 60) + second
  case target_unit
  when TimeUnit::MILLI
    target_value *= 1000
    target_value += nano_second / 1000 / 1000
  when TimeUnit::MICRO
    target_value *= 1000 * 1000
    target_value += nano_second / 1000
  when TimeUnit::NANO
    target_value *= 1000 * 1000 * 1000
    target_value += nano_second
  end
  target_value = -target_value if negative?
  self.class.new(target_unit, target_value)
end

#hourObject



77
78
79
80
# File 'lib/arrow/time.rb', line 77

def hour
  unconstruct
  @hour
end

#minuteObject Also known as: min



82
83
84
85
# File 'lib/arrow/time.rb', line 82

def minute
  unconstruct
  @minute
end

#nano_secondObject Also known as: nsec



94
95
96
97
# File 'lib/arrow/time.rb', line 94

def nano_second
  unconstruct
  @nano_second
end

#negative?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/arrow/time.rb', line 73

def negative?
  @value.negative?
end

#positive?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/arrow/time.rb', line 69

def positive?
  @value.positive?
end

#secondObject Also known as: sec



88
89
90
91
# File 'lib/arrow/time.rb', line 88

def second
  unconstruct
  @second
end

#to_fObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/arrow/time.rb', line 56

def to_f
  case @unit
  when TimeUnit::SECOND
    @value.to_f
  when TimeUnit::MILLI
    @value.to_f / 1000.0
  when TimeUnit::MICRO
    @value.to_f / 1000.0 / 1000.0
  when TimeUnit::NANO
    @value.to_f / 1000.0 / 1000.0 / 1000.0
  end
end

#to_sObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/arrow/time.rb', line 100

def to_s
  unconstruct
  if @nano_second.zero?
    nano_second_string = ""
  else
    nano_second_string = (".%09d" % @nano_second).gsub(/0+\z/, "")
  end
  "%s%02d:%02d:%02d%s" % [
    @value.negative? ? "-" : "",
    @hour,
    @minute,
    @second,
    nano_second_string,
  ]
end