Class: Neo4j::Driver::Types::Duration
- Inherits:
-
TemporalValue
- Object
- TemporalValue
- Neo4j::Driver::Types::Duration
- Defined in:
- lib/neo4j/driver/types/duration.rb
Overview
Bolt Duration. Stores months, days, seconds, nanoseconds as distinct ints — Neo4j keeps these separate because months are variable-length (no fixed conversion to seconds).
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
-
#days ⇒ Object
readonly
Returns the value of attribute days.
-
#months ⇒ Object
readonly
Returns the value of attribute months.
-
#nanoseconds ⇒ Object
readonly
Returns the value of attribute nanoseconds.
-
#seconds ⇒ Object
readonly
Returns the value of attribute seconds.
Class Method Summary collapse
-
.parse(string) ⇒ Object
Parse ISO 8601 duration: "P1Y2M3DT4H5M6.789S", "P3M", "PT2H30M".
- .significant_fields ⇒ Object
Instance Method Summary collapse
-
#initialize(months, days, seconds, nanoseconds) ⇒ Duration
constructor
A new instance of Duration.
- #parts ⇒ Object
-
#to_s ⇒ Object
ISO 8601 form.
Methods inherited from TemporalValue
#<=>, #==, #hash, #significant
Constructor Details
#initialize(months, days, seconds, nanoseconds) ⇒ Duration
Returns a new instance of Duration.
14 15 16 17 18 19 20 |
# File 'lib/neo4j/driver/types/duration.rb', line 14 def initialize(months, days, seconds, nanoseconds) @months = months.to_i @days = days.to_i @seconds = seconds.to_i @nanoseconds = nanoseconds.to_i normalize! end |
Instance Attribute Details
#days ⇒ Object (readonly)
Returns the value of attribute days.
10 11 12 |
# File 'lib/neo4j/driver/types/duration.rb', line 10 def days @days end |
#months ⇒ Object (readonly)
Returns the value of attribute months.
10 11 12 |
# File 'lib/neo4j/driver/types/duration.rb', line 10 def months @months end |
#nanoseconds ⇒ Object (readonly)
Returns the value of attribute nanoseconds.
10 11 12 |
# File 'lib/neo4j/driver/types/duration.rb', line 10 def nanoseconds @nanoseconds end |
#seconds ⇒ Object (readonly)
Returns the value of attribute seconds.
10 11 12 |
# File 'lib/neo4j/driver/types/duration.rb', line 10 def seconds @seconds end |
Class Method Details
.parse(string) ⇒ Object
Parse ISO 8601 duration: "P1Y2M3DT4H5M6.789S", "P3M", "PT2H30M"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/neo4j/driver/types/duration.rb', line 23 def self.parse(string) unless string =~ /^P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+(?:\.\d+)?)S)?)?$/ raise ArgumentError, "Invalid ISO 8601 duration format: #{string}" end years = ($1 || 0).to_i months = ($2 || 0).to_i days = ($3 || 0).to_i hours = ($4 || 0).to_i minutes = ($5 || 0).to_i seconds_with_fraction = ($6 || 0).to_f total_months = years * 12 + months whole_seconds = seconds_with_fraction.to_i fractional_seconds = seconds_with_fraction - whole_seconds nanos = (fractional_seconds * NANOS_PER_SECOND).round total_seconds = hours * 3600 + minutes * 60 + whole_seconds new(total_months, days, total_seconds, nanos) end |
.significant_fields ⇒ Object
12 |
# File 'lib/neo4j/driver/types/duration.rb', line 12 def self.significant_fields = %i[months days seconds nanoseconds] |
Instance Method Details
#parts ⇒ Object
44 45 46 |
# File 'lib/neo4j/driver/types/duration.rb', line 44 def parts { months: @months, days: @days, seconds: @seconds, nanoseconds: @nanoseconds } end |
#to_s ⇒ Object
ISO 8601 form. Nanoseconds get a 9-digit zero-padded suffix on seconds so 1s + 5ns prints as "PT1.000000005S", not "PT1.5S".
50 51 52 53 54 55 56 |
# File 'lib/neo4j/driver/types/duration.rb', line 50 def to_s if @nanoseconds.zero? format('P%dM%dDT%dS', @months, @days, @seconds) else format('P%dM%dDT%d.%09dS', @months, @days, @seconds, @nanoseconds) end end |