Froze rails gems
[depot.git] / vendor / rails / activesupport / lib / active_support / vendor / tzinfo-0.3.12 / tzinfo / timezone_offset_info.rb
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 module TZInfo
24 # Represents an offset defined in a Timezone data file.
25 class TimezoneOffsetInfo #:nodoc:
26 # The base offset of the timezone from UTC in seconds.
27 attr_reader :utc_offset
28
29 # The offset from standard time for the zone in seconds (i.e. non-zero if
30 # daylight savings is being observed).
31 attr_reader :std_offset
32
33 # The total offset of this observance from UTC in seconds
34 # (utc_offset + std_offset).
35 attr_reader :utc_total_offset
36
37 # The abbreviation that identifies this observance, e.g. "GMT"
38 # (Greenwich Mean Time) or "BST" (British Summer Time) for "Europe/London". The returned identifier is a
39 # symbol.
40 attr_reader :abbreviation
41
42 # Constructs a new TimezoneOffsetInfo. utc_offset and std_offset are
43 # specified in seconds.
44 def initialize(utc_offset, std_offset, abbreviation)
45 @utc_offset = utc_offset
46 @std_offset = std_offset
47 @abbreviation = abbreviation
48
49 @utc_total_offset = @utc_offset + @std_offset
50 end
51
52 # True if std_offset is non-zero.
53 def dst?
54 @std_offset != 0
55 end
56
57 # Converts a UTC DateTime to local time based on the offset of this period.
58 def to_local(utc)
59 TimeOrDateTime.wrap(utc) {|wrapped|
60 wrapped + @utc_total_offset
61 }
62 end
63
64 # Converts a local DateTime to UTC based on the offset of this period.
65 def to_utc(local)
66 TimeOrDateTime.wrap(local) {|wrapped|
67 wrapped - @utc_total_offset
68 }
69 end
70
71 # Returns true if and only if toi has the same utc_offset, std_offset
72 # and abbreviation as this TimezoneOffsetInfo.
73 def ==(toi)
74 toi.respond_to?(:utc_offset) && toi.respond_to?(:std_offset) && toi.respond_to?(:abbreviation) &&
75 utc_offset == toi.utc_offset && std_offset == toi.std_offset && abbreviation == toi.abbreviation
76 end
77
78 # Returns true if and only if toi has the same utc_offset, std_offset
79 # and abbreviation as this TimezoneOffsetInfo.
80 def eql?(toi)
81 self == toi
82 end
83
84 # Returns a hash of this TimezoneOffsetInfo.
85 def hash
86 utc_offset.hash ^ std_offset.hash ^ abbreviation.hash
87 end
88
89 # Returns internal object state as a programmer-readable string.
90 def inspect
91 "#<#{self.class}: #@utc_offset,#@std_offset,#@abbreviation>"
92 end
93 end
94 end