Class: Neo4j::Driver::Types::Duration

Inherits:
TemporalValue show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#daysObject (readonly)

Returns the value of attribute days.



10
11
12
# File 'lib/neo4j/driver/types/duration.rb', line 10

def days
  @days
end

#monthsObject (readonly)

Returns the value of attribute months.



10
11
12
# File 'lib/neo4j/driver/types/duration.rb', line 10

def months
  @months
end

#nanosecondsObject (readonly)

Returns the value of attribute nanoseconds.



10
11
12
# File 'lib/neo4j/driver/types/duration.rb', line 10

def nanoseconds
  @nanoseconds
end

#secondsObject (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 = (::Regexp.last_match(1) || 0).to_i
  months = (::Regexp.last_match(2) || 0).to_i
  days = (::Regexp.last_match(3) || 0).to_i
  hours = (::Regexp.last_match(4) || 0).to_i
  minutes = (::Regexp.last_match(5) || 0).to_i
  seconds_with_fraction = (::Regexp.last_match(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_fieldsObject



12
# File 'lib/neo4j/driver/types/duration.rb', line 12

def self.significant_fields = %i[months days seconds nanoseconds]

Instance Method Details

#partsObject



44
45
46
# File 'lib/neo4j/driver/types/duration.rb', line 44

def parts
  { months: @months, days: @days, seconds: @seconds, nanoseconds: @nanoseconds }
end

#to_sObject

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