Class: Neo4j::Driver::Types::OffsetTime
- Inherits:
-
TemporalValue
- Object
- TemporalValue
- Neo4j::Driver::Types::OffsetTime
- 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
-
#nanoseconds ⇒ Object
readonly
Returns the value of attribute nanoseconds.
-
#tz_offset_seconds ⇒ Object
readonly
Returns the value of attribute tz_offset_seconds.
Class Method Summary collapse
- .from_nanos(nanoseconds, tz_offset_seconds) ⇒ Object
-
.from_time(time) ⇒ Object
Build from a Ruby Time — its wall clock plus its UTC offset.
-
.parse(string) ⇒ Object
Accepts "12:34:56.123456789+01:00", "12:34:56Z", or any string containing such a time-with-offset component.
- .significant_fields ⇒ Object
Instance Method Summary collapse
-
#+(other) ⇒ Object
Add a numeric or ActiveSupport::Duration.
-
#<=>(other) ⇒ Object
Order by underlying UTC instant.
- #hour ⇒ Object
-
#initialize(nanoseconds, tz_offset_seconds) ⇒ OffsetTime
constructor
A new instance of OffsetTime.
- #minute ⇒ Object
- #nanosecond ⇒ Object
- #second ⇒ Object
- #to_s ⇒ Object
Methods inherited from TemporalValue
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
#nanoseconds ⇒ Object (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_seconds ⇒ Object (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) |
.from_time(time) ⇒ Object
Build from a Ruby Time — its wall clock plus its UTC offset. Reuses LocalTime's field extraction. Mirrors LocalDateTime.from_time.
24 25 26 |
# File 'lib/neo4j/driver/types/offset_time.rb', line 24 def self.from_time(time) new(LocalTime.from_time(time).nanoseconds, time.utc_offset) end |
.parse(string) ⇒ Object
Accepts "12:34:56.123456789+01:00", "12:34:56Z", or any string containing such a time-with-offset component.
30 31 32 33 34 35 36 37 |
# File 'lib/neo4j/driver/types/offset_time.rb', line 30 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_fields ⇒ Object
13 |
# File 'lib/neo4j/driver/types/offset_time.rb', line 13 def self.significant_fields = %i[nanoseconds tz_offset_seconds] |
Instance Method Details
#+(other) ⇒ Object
Add a numeric or ActiveSupport::Duration. Sub-second precision preserved (see LocalTime#+).
55 56 57 |
# File 'lib/neo4j/driver/types/offset_time.rb', line 55 def +(other) self.class.new(@nanoseconds + (other.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).
73 74 75 76 77 |
# File 'lib/neo4j/driver/types/offset_time.rb', line 73 def <=>(other) return nil unless other.is_a?(OffsetTime) utc_nanos <=> other.utc_nanos end |
#hour ⇒ Object
48 |
# File 'lib/neo4j/driver/types/offset_time.rb', line 48 def hour = (@nanoseconds / NANOS_PER_HOUR) % 24 |
#minute ⇒ Object
49 |
# File 'lib/neo4j/driver/types/offset_time.rb', line 49 def minute = (@nanoseconds / NANOS_PER_MINUTE) % 60 |
#nanosecond ⇒ Object
51 |
# File 'lib/neo4j/driver/types/offset_time.rb', line 51 def nanosecond = @nanoseconds % NANOS_PER_SECOND |
#second ⇒ Object
50 |
# File 'lib/neo4j/driver/types/offset_time.rb', line 50 def second = (@nanoseconds / NANOS_PER_SECOND) % 60 |
#to_s ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/neo4j/driver/types/offset_time.rb', line 59 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 |