Class: GrpcKit::GrpcTime

Inherits:
Object
  • Object
show all
Defined in:
lib/grpc_kit/grpc_time.rb

Constant Summary collapse

MAX =
10**9 - 1

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ GrpcTime

Returns a new instance of GrpcTime.

Parameters:

  • value (String, Integer)


8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/grpc_kit/grpc_time.rb', line 8

def initialize(value)
  @unit = nil
  @value = nil

  if value.is_a?(String)
    from_string(value)
  elsif value.is_a?(Integer)
    from_integer(value)
  else
    raise ArgumentError, "unsupported value: #{value}, class=#{value.class}"
  end
end

Instance Method Details

#to_absolute_timeTime

Returns:

  • (Time)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/grpc_kit/grpc_time.rb', line 47

def to_absolute_time
  case @unit
  when 'S'
    Time.now + @value
  when 'H'
    Time.now + @value * 60 * 60
  when 'M'
    Time.now + @value * 60
  when 'm'
    t = Time.now
    Time.at(t.to_i, (t.nsec * (10**-3)) + (@value * 10**3))
  when 'u'
    t = Time.now
    Time.at(t.to_i, (t.nsec * (10**-3)) + @value)
  when 'n'
    t = Time.now
    Time.at(t.to_i, (t.nsec * (10**-3)) + (@value * 10**-3))
  else
    raise 'This case would never happen'
  end
end

#to_fFloat

Returns:

  • (Float)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/grpc_kit/grpc_time.rb', line 22

def to_f
  case @unit
  when 'S'
    @value * 1.0
  when 'H'
    @value * 60 * 60.0
  when 'M'
    @value * 60.0
  when 'm'
    @value * 10**-3
  when 'u'
    @value * 10**-6
  when 'n'
    @value * 10**-9
  else
    raise 'This case would never happen'
  end
end

#to_sString

Returns:

  • (String)


42
43
44
# File 'lib/grpc_kit/grpc_time.rb', line 42

def to_s
  "#{@value}#{@unit}"
end