File: active_support/vendor/tzinfo-0.3.12/tzinfo/offset_rationals.rb

Overview
Module Structure
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: TZInfo#26
  module: OffsetRationals#31
has properties
function: rational_for_offset / 1 #93

Code

   1  #--
   2  # Copyright (c) 2006 Philip Ross
   3  # 
   4  # Permission is hereby granted, free of charge, to any person obtaining a copy
   5  # of this software and associated documentation files (the "Software"), to deal
   6  # in the Software without restriction, including without limitation the rights
   7  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   8  # copies of the Software, and to permit persons to whom the Software is
   9  # furnished to do so, subject to the following conditions:
  10  # 
  11  # The above copyright notice and this permission notice shall be included in all
  12  # copies or substantial portions of the Software.
  13  #
  14  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20  # THE SOFTWARE.
  21  #++
  22 
  23  require 'rational'
  24  require 'tzinfo/ruby_core_support'
  25 
  26  module TZInfo
  27    
  28    # Provides a method for getting Rationals for a timezone offset in seconds.
  29    # Pre-reduced rationals are returned for all the half-hour intervals between
  30    # -14 and +14 hours to avoid having to call gcd at runtime.  
  31    module OffsetRationals #:nodoc:
  32      @@rational_cache = {
  33        -50400 => RubyCoreSupport.rational_new!(-7,12), 
  34        -48600 => RubyCoreSupport.rational_new!(-9,16),
  35        -46800 => RubyCoreSupport.rational_new!(-13,24),
  36        -45000 => RubyCoreSupport.rational_new!(-25,48),
  37        -43200 => RubyCoreSupport.rational_new!(-1,2),
  38        -41400 => RubyCoreSupport.rational_new!(-23,48),
  39        -39600 => RubyCoreSupport.rational_new!(-11,24),
  40        -37800 => RubyCoreSupport.rational_new!(-7,16),
  41        -36000 => RubyCoreSupport.rational_new!(-5,12),
  42        -34200 => RubyCoreSupport.rational_new!(-19,48),
  43        -32400 => RubyCoreSupport.rational_new!(-3,8),
  44        -30600 => RubyCoreSupport.rational_new!(-17,48),
  45        -28800 => RubyCoreSupport.rational_new!(-1,3),
  46        -27000 => RubyCoreSupport.rational_new!(-5,16),
  47        -25200 => RubyCoreSupport.rational_new!(-7,24),
  48        -23400 => RubyCoreSupport.rational_new!(-13,48),
  49        -21600 => RubyCoreSupport.rational_new!(-1,4),
  50        -19800 => RubyCoreSupport.rational_new!(-11,48),
  51        -18000 => RubyCoreSupport.rational_new!(-5,24),
  52        -16200 => RubyCoreSupport.rational_new!(-3,16),
  53        -14400 => RubyCoreSupport.rational_new!(-1,6),
  54        -12600 => RubyCoreSupport.rational_new!(-7,48),
  55        -10800 => RubyCoreSupport.rational_new!(-1,8),
  56         -9000 => RubyCoreSupport.rational_new!(-5,48),
  57         -7200 => RubyCoreSupport.rational_new!(-1,12),
  58         -5400 => RubyCoreSupport.rational_new!(-1,16),
  59         -3600 => RubyCoreSupport.rational_new!(-1,24),
  60         -1800 => RubyCoreSupport.rational_new!(-1,48),
  61             0 => RubyCoreSupport.rational_new!(0,1),
  62          1800 => RubyCoreSupport.rational_new!(1,48),
  63          3600 => RubyCoreSupport.rational_new!(1,24),
  64          5400 => RubyCoreSupport.rational_new!(1,16),
  65          7200 => RubyCoreSupport.rational_new!(1,12),
  66          9000 => RubyCoreSupport.rational_new!(5,48),
  67         10800 => RubyCoreSupport.rational_new!(1,8),
  68         12600 => RubyCoreSupport.rational_new!(7,48),
  69         14400 => RubyCoreSupport.rational_new!(1,6),
  70         16200 => RubyCoreSupport.rational_new!(3,16),
  71         18000 => RubyCoreSupport.rational_new!(5,24),
  72         19800 => RubyCoreSupport.rational_new!(11,48),
  73         21600 => RubyCoreSupport.rational_new!(1,4),
  74         23400 => RubyCoreSupport.rational_new!(13,48),
  75         25200 => RubyCoreSupport.rational_new!(7,24),
  76         27000 => RubyCoreSupport.rational_new!(5,16),
  77         28800 => RubyCoreSupport.rational_new!(1,3),
  78         30600 => RubyCoreSupport.rational_new!(17,48),
  79         32400 => RubyCoreSupport.rational_new!(3,8),
  80         34200 => RubyCoreSupport.rational_new!(19,48),
  81         36000 => RubyCoreSupport.rational_new!(5,12),
  82         37800 => RubyCoreSupport.rational_new!(7,16),
  83         39600 => RubyCoreSupport.rational_new!(11,24),
  84         41400 => RubyCoreSupport.rational_new!(23,48),
  85         43200 => RubyCoreSupport.rational_new!(1,2),
  86         45000 => RubyCoreSupport.rational_new!(25,48),
  87         46800 => RubyCoreSupport.rational_new!(13,24),
  88         48600 => RubyCoreSupport.rational_new!(9,16),
  89         50400 => RubyCoreSupport.rational_new!(7,12)}
  90      
  91      # Returns a Rational expressing the fraction of a day that offset in 
  92      # seconds represents (i.e. equivalent to Rational(offset, 86400)). 
  93      def rational_for_offset(offset)
  94        @@rational_cache[offset] || Rational(offset, 86400)      
  95      end
  96      module_function :rational_for_offset
  97    end
  98  end