Class: Google::Cloud::Spanner::Interval

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/spanner/interval.rb

Overview

Interval

Represents an interval of time by storing the time components in months, days and nanoseconds.

Examples:

require "google/cloud/spanner"

iso_8601_string = "P1Y2M3DT4H5M6S"
interval = Google::Cloud::Spanner::Interval::parse iso_8601_string

puts interval # "P1Y2M3DT4H5M6S"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#daysInteger (readonly)



196
197
198
# File 'lib/google/cloud/spanner/interval.rb', line 196

def days
  @days
end

#monthsInteger (readonly)



193
194
195
# File 'lib/google/cloud/spanner/interval.rb', line 193

def months
  @months
end

#nanosecondsInteger (readonly)



199
200
201
# File 'lib/google/cloud/spanner/interval.rb', line 199

def nanoseconds
  @nanoseconds
end

Class Method Details

.from_days(days) ⇒ Interval

Returns an Interval instance with the given days.



124
125
126
# File 'lib/google/cloud/spanner/interval.rb', line 124

def from_days days
  Interval.new 0, days, 0
end

.from_microseconds(microseconds) ⇒ Interval

Returns an Interval instance with the given microseconds.



150
151
152
153
# File 'lib/google/cloud/spanner/interval.rb', line 150

def from_microseconds microseconds
  nanoseconds = microseconds * NANOSECONDS_IN_A_MICROSECOND
  Interval.new 0, 0, nanoseconds
end

.from_milliseconds(milliseconds) ⇒ Interval

Returns an Interval instance with the given milliseconds.



141
142
143
144
# File 'lib/google/cloud/spanner/interval.rb', line 141

def from_milliseconds milliseconds
  nanoseconds = milliseconds * NANOSECONDS_IN_A_MILLISECOND
  Interval.new 0, 0, nanoseconds
end

.from_months(months) ⇒ Interval

Returns an Interval instance with the given months.



116
117
118
# File 'lib/google/cloud/spanner/interval.rb', line 116

def from_months months
  Interval.new months, 0, 0
end

.from_nanoseconds(nanoseconds) ⇒ Interval

Returns an Interval instance with the given nanoseconds.



159
160
161
# File 'lib/google/cloud/spanner/interval.rb', line 159

def from_nanoseconds nanoseconds
  Interval.new 0, 0, nanoseconds
end

.from_seconds(seconds) ⇒ Interval

Returns an Interval instance with the given seconds.



132
133
134
135
# File 'lib/google/cloud/spanner/interval.rb', line 132

def from_seconds seconds
  nanoseconds = seconds * NANOSECONDS_IN_A_SECOND
  Interval.new 0, 0, nanoseconds
end

.parse(interval_string) ⇒ Google::Cloud::Spanner::Interval

Parses an ISO8601 string and returns an Interval instance.

The accepted format for the ISO8601 standard is: P[n]Y[n]M[n]DT[n]H[n]M[n[.fraction]]S where n represents an integer number.

Examples:

require "google/cloud/spanner"

iso_8601_string = "P1Y2M3DT4H5M6S"
interval = Google::Cloud::Spanner::Interval::parse iso_8601_string

puts interval # "P1Y2M3DT4H5M6S"

Raises:

  • (ArgumentError)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/google/cloud/spanner/interval.rb', line 69

def parse interval_string
  pattern = /^
    P(?!$)
    (?:(?<years>-?\d+)Y)?
    (?:(?<months>-?\d+)M)?
    (?:(?<days>-?\d+)D)?
    (?:T(?!$)
    (?:(?<hours>-?\d+)H)?
    (?:(?<minutes>-?\d+)M)?
    (?:(?<seconds>-?(?!S)\d*(?:[.,]\d{1,9})?)S)?)?
    $
  /x
  interval_months = 0
  interval_days = 0
  interval_nanoseconds = 0

  matches = interval_string.match pattern

  raise ArgumentError, "The provided string does not follow ISO8601 standard." if matches.nil?

  raise ArgumentError, "The provided string does not follow ISO8601 standard." if matches.captures.empty?

  interval_months += matches[:years].to_i * 12 if matches[:years]

  interval_months += matches[:months].to_i if matches[:months]

  interval_days = matches[:days].to_i if matches[:days]

  interval_nanoseconds += matches[:hours].to_i * NANOSECONDS_IN_AN_HOUR if matches[:hours]

  interval_nanoseconds += matches[:minutes].to_i * NANOSECONDS_IN_A_MINUTE if matches[:minutes]

  # Only seconds can be fractional. Both period and comma are valid inputs.
  if matches[:seconds]
    interval_nanoseconds += (matches[:seconds].gsub(",", ".").to_f * NANOSECONDS_IN_A_SECOND).to_i
  end

  Interval.new interval_months, interval_days, interval_nanoseconds
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Standard value equality check for this object.



207
208
209
210
211
212
# File 'lib/google/cloud/spanner/interval.rb', line 207

def eql? other
  other.is_a?(Interval) &&
    months == other.months &&
    days == other.days &&
    nanoseconds == other.nanoseconds
end

#hashInteger

Generate standard hash code for this object.



220
221
222
# File 'lib/google/cloud/spanner/interval.rb', line 220

def hash
  @hash ||= [@months, @days, @nanoseconds].hash
end

#to_sString

Converts the [Interval] to an ISO8601 Standard string.



167
168
169
170
# File 'lib/google/cloud/spanner/interval.rb', line 167

def to_s
  # Memoizing it as the logic can be a bit heavy.
  @to_s ||= to_string
end