public final class RubyTimeZones extends Object
static
method to interpret a timezone string
in the manner of Ruby's Time
class.
Ruby's Date
and DateTime
classes accept a superset of
timezone strings which are accepted by Ruby's Time
class. Ruby's
Time.strptime
internally calls Date._strptime
just to expand
a date-time string into elements, and then resolves the expanded elements by
Time
's own way. In other words, some timezone strings are recognized
once by Date._strptime
at first, and then by Time.strptime
ignores them.
For example, Date
recognizes "CEST"
although Time
does not recognize it. On the other hand, both recognizes "PST"
.
$ env TZ=UTC irb
irb(main):001:0> require 'date'
=> true
irb(main):002:0> require 'time'
=> true
irb(main):003:0> Date._strptime("CEST", "%z")
=> {:zone=>"CEST", :offset=>7200}
irb(main):004:0> DateTime.strptime("2017-12-31 12:34:56 CEST", "%Y-%m-%d %H:%M:%S %z")
=> #<DateTime: 2017-12-31T12:34:56+02:00 ((2458119j,38096s,0n),+7200s,2299161j)>
irb(main):005:0> Time.strptime("2017-12-31 12:34:56 CEST", "%Y-%m-%d %H:%M:%S %z")
=> 2017-12-31 12:34:56 +0000
irb(main):006:0> Date._strptime("PST", "%z")
=> {:zone=>"PST", :offset=>-28800}
irb(main):007:0> DateTime.strptime("2017-12-31 12:34:56 PST", "%Y-%m-%d %H:%M:%S %z")
=> #<DateTime: 2017-12-31T12:34:56-08:00 ((2458119j,74096s,0n),-28800s,2299161j)>
irb(main):008:0> Time.strptime("2017-12-31 12:34:56 PST", "%Y-%m-%d %H:%M:%S %z")
=> 2017-12-31 12:34:56 -0800
Modifier and Type | Method and Description |
---|---|
static ZoneOffset |
toZoneOffset(String zoneName,
ZoneOffset defaultZoneOffset)
Converts a timezone string into
ZoneOffset in the manner
of Ruby's Time class. |
public static ZoneOffset toZoneOffset(String zoneName, ZoneOffset defaultZoneOffset)
ZoneOffset
in the manner
of Ruby's Time
class.
Note that it has a difference from Ruby Time.strptime
, where it
does not consider the local timezone. It returns defaultZoneOffset
if the given timezone string is invalid -- neither numerical nor predefined
textual timezone names. In contrast, Ruby's Time.strptime
considers
the local timezone in that case.
zoneName
- a timezone stringdefaultZoneOffset
- a ZoneOffset
for defaultZoneOffset
converted