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



51
52
53
# File 'lib/couchbase/utils/time.rb', line 51

def extract_duration(number_or_duration)
  number_or_duration.respond_to?(:in_milliseconds) ? number_or_duration.public_send(:in_milliseconds) : number_or_duration
end

.extract_expiry_time(time_or_duration) ⇒ Object

Parameters:

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

    expiration time to associate with the document



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

def extract_expiry_time(time_or_duration)
  if time_or_duration.respond_to?(:in_seconds) # Duration
    time_or_duration.in_seconds
  elsif time_or_duration.respond_to?(:tv_sec) # Time
    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)
      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_or_duration
    else
      ::Time.now.tv_sec + time_or_duration
    end
  else
    time_or_duration
  end
end