Module: Couchbase::Utils::Time

Defined in:
lib/couchbase/utils/time.rb

Overview

Various Time utilities

Constant Summary collapse

RELATIVE_EXPIRY_CUTOFF_SECONDS =
30 * 24 * 60 * 60
WORKAROUND_EXPIRY_CUTOFF_SECONDS =
50 * 365 * 24 * 60 * 60

Class Method Summary collapse

Class Method Details

.extract_duration(number_or_duration) ⇒ Object

This method converts its argument to milliseconds

  1. Integer values are interpreted as a number of milliseconds

  2. If the argument is a Duration-like object and responds to #in_milliseconds, then use it and convert result to Integer

  3. Otherwise invoke #to_i on the argument and interpret it as a number of milliseconds



59
60
61
62
63
64
65
66
67
68
# File 'lib/couchbase/utils/time.rb', line 59

def extract_duration(number_or_duration)
  return unless number_or_duration
  return number_or_duration if number_or_duration.class == Integer # rubocop:disable Style/ClassEqualityComparison avoid overrides of #is_a?, #kind_of?

  if number_or_duration.respond_to?(:in_milliseconds)
    number_or_duration.in_milliseconds
  else
    number_or_duration
  end.to_i
end

.extract_expiry_time(time_or_duration) ⇒ Object

Parameters:

  • time_or_duration (Integer, #in_seconds, Time, nil)

    expiration time to associate with the document



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/couchbase/utils/time.rb', line 29

def extract_expiry_time(time_or_duration)
  if time_or_duration.respond_to?(:in_seconds) # Duration
    [:duration, time_or_duration.in_seconds]
  elsif time_or_duration.respond_to?(:tv_sec) # Time
    [:time_point, time_or_duration.tv_sec]
  elsif time_or_duration.is_a?(Integer)
    if time_or_duration < RELATIVE_EXPIRY_CUTOFF_SECONDS
      # looks like valid relative duration as specified in protocol (less than 30 days)
      [:duration, time_or_duration]
    elsif time_or_duration > WORKAROUND_EXPIRY_CUTOFF_SECONDS
      effective_expiry = ::Time.at(time_or_duration).utc
      warn "The specified expiry duration #{time_or_duration} is longer than 50 years. For bug-compatibility " \
           "with previous versions of SDK 3.0.x, the number of seconds in the duration will be interpreted as " \
           "the epoch second when the document should expire (#{effective_expiry}). Stuffing an epoch second " \
           "into a Duration is deprecated and will no longer work in SDK 3.1. Consider using Time instance instead."
      [:time_point, time_or_duration]
    else
      [:time_point, ::Time.now.tv_sec + time_or_duration]
    end
  else
    [:duration, time_or_duration]
  end
end