Class: Neo4j::Driver::Types::OffsetTime

Inherits:
TemporalValue show all
Defined in:
lib/neo4j/driver/types/offset_time.rb

Overview

Bolt OffsetTime — time of day with a fixed UTC offset, stored as nanoseconds-since-midnight (in local wall clock) plus offset seconds. Equivalent to java.time.OffsetTime. Was previously called Types::Time, which collided cosmetically with ::Time.

Constant Summary

Constants inherited from TemporalValue

TemporalValue::NANOS_PER_DAY, TemporalValue::NANOS_PER_HOUR, TemporalValue::NANOS_PER_MINUTE, TemporalValue::NANOS_PER_SECOND

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TemporalValue

#==, #hash, #significant

Constructor Details

#initialize(nanoseconds, tz_offset_seconds) ⇒ OffsetTime

Returns a new instance of OffsetTime.



15
16
17
18
# File 'lib/neo4j/driver/types/offset_time.rb', line 15

def initialize(nanoseconds, tz_offset_seconds)
  @nanoseconds = nanoseconds % NANOS_PER_DAY
  @tz_offset_seconds = tz_offset_seconds
end

Instance Attribute Details

#nanosecondsObject (readonly)

Returns the value of attribute nanoseconds.



11
12
13
# File 'lib/neo4j/driver/types/offset_time.rb', line 11

def nanoseconds
  @nanoseconds
end

#tz_offset_secondsObject (readonly)

Returns the value of attribute tz_offset_seconds.



11
12
13
# File 'lib/neo4j/driver/types/offset_time.rb', line 11

def tz_offset_seconds
  @tz_offset_seconds
end

Class Method Details

.from_nanos(nanoseconds, tz_offset_seconds) ⇒ Object



20
# File 'lib/neo4j/driver/types/offset_time.rb', line 20

def self.from_nanos(nanoseconds, tz_offset_seconds) = new(nanoseconds, tz_offset_seconds)

.parse(string) ⇒ Object

Accepts "12:34:56.123456789+01:00", "12:34:56Z", or any string containing such a time-with-offset component.

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
# File 'lib/neo4j/driver/types/offset_time.rb', line 24

def self.parse(string)
  match = string.match(/(\d{1,2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?([Z+\-][\d:]*)?/)
  raise ArgumentError, "Invalid OffsetTime format: #{string}" unless match

  nanos = LocalTime.send(:parse_nanos,
                         match[1].to_i, match[2].to_i, (match[3] || 0).to_i, match[4] || '0')
  new(nanos, parse_offset(match[5] || 'Z'))
end

.significant_fieldsObject



13
# File 'lib/neo4j/driver/types/offset_time.rb', line 13

def self.significant_fields = %i[nanoseconds tz_offset_seconds]

Instance Method Details

#+(seconds) ⇒ Object

Add a numeric or ActiveSupport::Duration. Sub-second precision preserved (see LocalTime#+).



49
50
51
# File 'lib/neo4j/driver/types/offset_time.rb', line 49

def +(seconds)
  self.class.new(@nanoseconds + (seconds.to_f * NANOS_PER_SECOND).round, @tz_offset_seconds)
end

#<=>(other) ⇒ Object

Order by underlying UTC instant. Two OffsetTimes representing the same UTC instant in different offsets compare equal under <=>; == still requires same fields (they're not the same value).



67
68
69
70
# File 'lib/neo4j/driver/types/offset_time.rb', line 67

def <=>(other)
  return nil unless other.is_a?(OffsetTime)
  utc_nanos <=> other.utc_nanos
end

#hourObject



42
# File 'lib/neo4j/driver/types/offset_time.rb', line 42

def hour       = (@nanoseconds / NANOS_PER_HOUR) % 24

#minuteObject



43
# File 'lib/neo4j/driver/types/offset_time.rb', line 43

def minute     = (@nanoseconds / NANOS_PER_MINUTE) % 60

#nanosecondObject



45
# File 'lib/neo4j/driver/types/offset_time.rb', line 45

def nanosecond = @nanoseconds % NANOS_PER_SECOND

#secondObject



44
# File 'lib/neo4j/driver/types/offset_time.rb', line 44

def second     = (@nanoseconds / NANOS_PER_SECOND) % 60

#to_sObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/neo4j/driver/types/offset_time.rb', line 53

def to_s
  # Apply the sign to the formatted offset as a whole, not to
  # `hours` alone — Ruby integer division for `-12600 / 3600` is
  # -4 (floor toward -∞), which would render -03:30 as -04:30.
  abs = @tz_offset_seconds.abs
  sign = @tz_offset_seconds.negative? ? '-' : '+'
  format('%02d:%02d:%02d.%09d%s%02d:%02d',
         hour, minute, second, nanosecond,
         sign, abs / 3600, (abs % 3600) / 60)
end