public final class RubyDateTimeZones extends Object
static method to interpret a timezone string
in the manner of Ruby's Date and DateTime classes.
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 int |
toOffsetInSeconds(String zoneName)
Converts a timezone string into a time offset integer in seconds.
|
public static int toOffsetInSeconds(String zoneName)
Note that it has some limitations compared from Ruby's implementation.
"UTC+10.5") is parsed into an
integer rounded down in seconds, not into a Rational.
"UTC+9.111111111111")
is rejected. It throws NumberFormatException in that case.
zoneName - a timezone stringNumberFormatException - if failed to parse a numeric part