Handle time with zone without ActiveSupport or ENV['TZ']
MIT License
Handle time with zone without ActiveSupport, or ENV['TZ']
for thread-safety.
Add this line to your application's Gemfile:
gem 'time_with_zone'
And then execute:
$ bundle
Or install it yourself as:
$ gem install time_with_zone
Assume your localtime zone is +09:00
(Asia/Tokyo
):
require 'time'
Time.strptime('2015-01-01', '%Y-%m-%d')
#=> 2015-01-01 00:00:00 +0900
But, you want to get time in +08:00
(Asia/Taipei
) as 2015-01-01 00:00:00 +0800
.
If the timezone format is in numeric formats such as [+-]HH:MM
, [+-]HHMM
, [+-]HH
, you may use %z
of strptime:
require 'time'
date = '2015-01-01'
timezone = '+08:00'
Time.strptime("#{date} #{timezone}", '%Y-%m-%d %z')
#=> 2015-01-01 00:00:00 +0800
However, if the timezone format is in the Region/Zone
format such as Asia/Taipei
, %Z
or %z
won't work. So, use time_with_zone
gem as:
require 'time_with_zone'
TimeWithZone.strptime_with_zone('2015-01-01', '%Y-%m-%d', 'Asia/Taipei')
#=> 2015-01-01 00:00:00 +0800
TimeWithZone gem accepts numeric formats, and the Region/Zone
format, and some of short-abbreviations of timezone such as UTC.
Available methods are:
See docs for details
bundle exec rake
bundle exec yard
bundle exec rake release
Bug reports and pull requests are welcome on GitHub at https://github.com/sonots/time_with_zone. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.