Class: Cel::Timestamp

Inherits:
Literal
  • Object
show all
Defined in:
lib/cel/ast/elements/timestamp.rb

Constant Summary collapse

TIMESTAMP_RANGE =
(Time.parse("0001-01-01T00:00:00Z")..Time.parse("9999-12-31T23:59:59.999999999Z"))

Instance Attribute Summary

Attributes inherited from Literal

#type, #value

Instance Method Summary collapse

Methods inherited from Literal

to_cel_type

Methods included from CelMethods

included

Constructor Details

#initialize(value) ⇒ Timestamp

Returns a new instance of Timestamp.

Raises:



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cel/ast/elements/timestamp.rb', line 9

def initialize(value)
  value = case value
          when ::String then Time.parse(value)
          when Numeric then Time.at(value)
          else value
  end

  raise EvaluateError, "out of range" unless TIMESTAMP_RANGE.include?(value)

  super(:timestamp, value.round(9))
end

Instance Method Details

#to_ruby_typeObject



85
86
87
# File 'lib/cel/ast/elements/timestamp.rb', line 85

def to_ruby_type
  Protobuf.timestamp_class.from_time(@value)
end

#to_sObject



89
90
91
# File 'lib/cel/ast/elements/timestamp.rb', line 89

def to_s
  @value.utc.iso8601
end