Skip to content

Commit 0c0875f

Browse files
committed
[DOC] Mention Time.find_timezone method
1 parent 2ecc372 commit 0c0875f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

doc/timezones.rdoc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The value given with any of these must be one of the following
1717
- {Single-letter offset}[rdoc-ref:timezones.rdoc@Single-Letter+Offsets].
1818
- {Integer offset}[rdoc-ref:timezones.rdoc@Integer+Offsets].
1919
- {Timezone object}[rdoc-ref:timezones.rdoc@Timezone+Objects].
20+
- {Timezone name}[rdoc-ref:timezones.rdoc@Timezone+Names].
2021

2122
=== Hours/Minutes Offsets
2223

@@ -102,3 +103,29 @@ which will be called if defined:
102103
- Called when <tt>Marshal.dump(t)</tt> is invoked
103104
- Argument: none.
104105
- Returns: the string name of the timezone.
106+
107+
=== Timezone Names
108+
109+
If the class (the receiver of class methods, or the class of the receiver
110+
of instance methods) has `find_timezone` singleton method, this method is
111+
called to achieve the corresponding timezone object from a timezone name.
112+
113+
For example, using {Timezone}[https://github.com/panthomakos/timezone]:
114+
class TimeWithTimezone < Time
115+
require 'timezone'
116+
def self.find_timezone(z) = Timezone[z]
117+
end
118+
119+
TimeWithTimezone.now(in: "America/New_York") #=> 2023-12-25 00:00:00 -0500
120+
TimeWithTimezone.new("2023-12-25 America/New_York") #=> 2023-12-25 00:00:00 -0500
121+
122+
Or, using {TZInfo}[https://tzinfo.github.io]:
123+
class TimeWithTZInfo < Time
124+
require 'tzinfo'
125+
def self.find_timezone(z) = TZInfo::Timezone.get(z)
126+
end
127+
128+
TimeWithTZInfo.now(in: "America/New_York") #=> 2023-12-25 00:00:00 -0500
129+
TimeWithTZInfo.new("2023-12-25 America/New_York") #=> 2023-12-25 00:00:00 -0500
130+
131+
You can define this method per subclasses, or on the toplevel `Time` class.

0 commit comments

Comments
 (0)