Home | History | Annotate | Download | only in core
      1 # Copyright 2015 gRPC authors.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #     http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 
     15 require_relative '../grpc'
     16 
     17 # GRPC contains the General RPC module.
     18 module GRPC
     19   module Core
     20     # TimeConsts is a module from the C extension.
     21     #
     22     # Here it's re-opened to add a utility func.
     23     module TimeConsts
     24       # Converts a time delta to an absolute deadline.
     25       #
     26       # Assumes timeish is a relative time, and converts its to an absolute,
     27       # with following exceptions:
     28       #
     29       # * if timish is one of the TimeConsts.TimeSpec constants the value is
     30       # preserved.
     31       # * timish < 0 => TimeConsts.INFINITE_FUTURE
     32       # * timish == 0 => TimeConsts.ZERO
     33       #
     34       # @param timeish [Number|TimeSpec]
     35       # @return [Number|TimeSpec]
     36       def from_relative_time(timeish)
     37         if timeish.is_a? TimeSpec
     38           timeish
     39         elsif timeish.nil?
     40           TimeConsts::ZERO
     41         elsif !timeish.is_a? Numeric
     42           fail(TypeError,
     43                "Cannot make an absolute deadline from #{timeish.inspect}")
     44         elsif timeish < 0
     45           TimeConsts::INFINITE_FUTURE
     46         elsif timeish.zero?
     47           TimeConsts::ZERO
     48         else
     49           Time.now + timeish
     50         end
     51       end
     52 
     53       module_function :from_relative_time
     54     end
     55   end
     56 end
     57