Class: TimeWithZone
- Inherits:
-
Object
- Object
- TimeWithZone
- Defined in:
- lib/time_with_zone.rb,
lib/time_with_zone/version.rb
Overview
Handle time with zone withtout ActiveSupport or ENV
require 'time_with_zone'
TimeWithZone.strptime_with_zone('2016-10-10', '%Y-%m-%d', 'Asia/Taipei')
# => '2016-10-10 00:00:00 +0800'
TimeWithZone.parse_with_zone('2016-10-10', 'Asia/Taipei')
# => '2016-10-10 00:00:00 +0800'
time = Time.parse('2016-10-10 00:00:00 +00:00')
TimeWithZone.set_zone!(time, 'Asia/Taipei')
# => '2016-10-10 00:00:00 +0800'
time = Time.parse('2016-10-10 00:00:00 +00:00')
TimeWithZone.localtime_with_zone(time, 'Asia/Taipei')
# => '2016-10-10 08:00:00 +0800'
Constant Summary collapse
- NUMERIC_PATTERN =
[+-]HH:MM, [+-]HHMM, [+-]HH
%r{\A[+-]\d\d(:?\d\d)?\z}- NAME_PATTERN =
Region/Zone, Region/Zone/Zone
%r{\A[^/]+/[^/]+(/[^/]+)?\z}- ZoneOffset =
Short-abbreviation timezone which Ruby’s Time class supports
(class << Time; self; end)::ZoneOffset
- VERSION =
"0.2.0"
Class Method Summary collapse
-
.localtime_with_zone(time, timezone) ⇒ Object
Time#localtime with timezone (non-destructive).
-
.localtime_with_zone!(time, timezone) ⇒ Object
Time#localtime with timezone (destructive).
-
.parse_with_zone(date, timezone) ⇒ Object
Time.parse with timezone.
-
.set_zone(time, timezone) ⇒ Time
This method simply sets the zone field of Time object (non-destructive).
-
.set_zone!(time, timezone) ⇒ Object
This method simply sets the zone field of Time object (destructive).
-
.strptime_with_zone(date, format, timezone) ⇒ Object
Time.strptime with timezone.
-
.zone_offset(timezone, time = nil) ⇒ Integer
Returns zone offset for given timezone string.
Class Method Details
.localtime_with_zone(time, timezone) ⇒ Object
Time#localtime with timezone (non-destructive)
ENV['TZ'] = '+09:00' # Assume your local timezone is +09:00
require 'time'
time = Time.parse("2016-10-20 00:00:00 +00:00")
time.dup.localtime("+08:00")
#=> 2010-10-20 08:00:00 +0800
time.dump.localtime("CDT")
#=> error
time.dump.localtime("Asia/Taipei")
#=> error
require 'time_with_zone'
time = Time.parse("2016-10-20 00:00:00 +00:00")
TimeWithZone.localtime_with_zone(time, "+08:00")
#=> 2010-10-20 08:00:00 +0800
TimeWithZone.localtime_with_zone(time, "CDT")
#=> 2016-10-19 19:00:00 -0500
TimeWithZone.localtime_with_zone(time, "Asia/Taipei")
#=> 2010-10-20 08:00:00 +0800
61 62 63 |
# File 'lib/time_with_zone.rb', line 61 def self.localtime_with_zone(time, timezone) localtime_with_zone!(time.dup, timezone) end |
.localtime_with_zone!(time, timezone) ⇒ Object
Time#localtime with timezone (destructive)
70 71 72 73 |
# File 'lib/time_with_zone.rb', line 70 def self.localtime_with_zone!(time, timezone) _zone_offset = zone_offset(timezone, time) time.localtime(_zone_offset) end |
.parse_with_zone(date, timezone) ⇒ Object
Time.parse with timezone
ENV['TZ'] = '+09:00' # Assume your local timezone is +09:00
require 'time'
Time.parse("2016-10-20")
#=> 2016-10-20 00:00:00 +0900
Time.parse("2016-10-20 00:00:00 +08:00")
#=> 2016-10-20 00:00:00 +0800
Time.parse("2016-10-20 00:00:00 CDT")
#=> 2016-10-20 00:00:00 -0500
Time.parse("2016-10-20 00:00:00 Asia/Taipei")
#=> 2016-10-20 00:00:00 +0900 (does not work)
require 'time_with_zone'
TimeWithZone.parse_with_zone("2016-10-20", "+08:00")
#=> 2016-10-20 00:00:00 +0800
TimeWithZone.parse_with_zone("2016-10-20", "CDT")
#=> 2016-10-20 00:00:00 -0500
TimeWithZone.parse_with_zone("2016-10-20", "Asia/Taipei")
#=> 2016-10-20 00:00:00 +0800
101 102 103 104 |
# File 'lib/time_with_zone.rb', line 101 def self.parse_with_zone(date, timezone) time = Time.parse(date) set_zone!(time, timezone) end |
.set_zone(time, timezone) ⇒ Time
This method simply sets the zone field of Time object (non-destructive)
require 'time_with_zone'
time = Time.parse("2016-02-02 00:00:00 +00:00")
TimeWithZone.set_zone(time, "+08:00")
#=> "2016-02-02 00:00:00 +0800"
# Note that it is not "2016-02-02 08:00:00 +0800" like Time#localtime(timezone)
150 151 152 |
# File 'lib/time_with_zone.rb', line 150 def self.set_zone(time, timezone) set_zone!(time.dup, timezone) end |
.set_zone!(time, timezone) ⇒ Object
This method simply sets the zone field of Time object (destructive)
159 160 161 162 163 |
# File 'lib/time_with_zone.rb', line 159 def self.set_zone!(time, timezone) _utc_offset = time.utc_offset _zone_offset = zone_offset(timezone, time) time.localtime(_zone_offset) + _utc_offset - _zone_offset end |
.strptime_with_zone(date, format, timezone) ⇒ Object
Time.strptime with timezone
ENV['TZ'] = '+09:00' # Assume your local timezone is +09:00
require 'time'
Time.strptime("2016-10-20", "%Y-%m-%d")
#=> 2016-10-20 00:00:00 +0900
Time.strptime("2016-10-20 +08:00", "%Y-%m-%d %z")
#=> 2016-10-20 00:00:00 +0800
Time.strptime("2016-10-20 CDT", "%Y-%m-%d %Z")
#=> 2016-10-20 00:00:00 -0500
Time.strptime("2016-10-20 Asia/Taipei", "%Y-%m-%d %Z")
#=> 2016-10-20 00:00:00 +0900 (does not work)
require 'time_with_zone'
TimeWithZone.strptime_with_zone("2016-10-20", "%Y-%m-%d", "+08:00")
#=> 2016-10-20 00:00:00 +0800
TimeWithZone.strptime_with_zone("2016-10-20", "%Y-%m-%d", "CDT")
#=> 2016-10-20 00:00:00 -0500
TimeWithZone.strptime_with_zone("2016-10-20", "%Y-%m-%d", "Asia/Taipei")
#=> 2016-10-20 00:00:00 +0800
133 134 135 136 |
# File 'lib/time_with_zone.rb', line 133 def self.strptime_with_zone(date, format, timezone) time = Time.strptime(date, format) set_zone!(time, timezone) end |
.zone_offset(timezone, time = nil) ⇒ Integer
Returns zone offset for given timezone string
require 'time_with_zone'
TimeWithZone.zone_offset("+08:00")
#=> 28800
TimeWithZone.zone_offset("Asia/Taipei")
#=> 28800
TimeWithZone.zone_offset("PST")
#=> -28800
TimeWithZone.zone_offset("America/Los_Angeles")
#=> -25200
TimeWithZone.zone_offset("America/Los_Angeles", Time.parse("2016-07-07 00:00:00 +00:00"))
#=> -25200 # DST (Daylight Saving Time)
TimeWithZone.zone_offset("America/Los_Angeles", Time.parse("2016-01-01 00:00:00 +00:00"))
#=> -28800 # non DST
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/time_with_zone.rb', line 185 def self.zone_offset(timezone, time = nil) if NUMERIC_PATTERN === timezone Time.zone_offset(timezone) elsif NAME_PATTERN === timezone tz = TZInfo::Timezone.get(timezone) if time tz.period_for_utc(time).utc_total_offset else tz.current_period.utc_total_offset end elsif ZoneOffset.include?(timezone) ZoneOffset[timezone] * 3600 else raise ArgumentError, "timezone format is invalid: #{timezone}" end end |