TimeZone
The TimeZone
class serves as a helper to work with time zones in Crystal.
It allows to do the following:
- Limit the set of zones to a meaningful subset of 151 zones.
- Retrieve and display zones with a friendlier name (e.g., "Eastern Time (US & Canada)" instead of "America/New_York").
Installation
-
Add the dependency to your
shard.yml
:dependencies: time_zone: github: mamantoha/time_zone
-
Run
shards install
Usage
require "time_zone"
zone = TimeZone.new("Taipei")
zone # => #<TimeZone:0x102b64f00 @name="Taipei", @location=#<Time::Location Asia/Taipei>, @utc_offset=28800>
puts zone
# (GMT+08:00) Taipei
zone.local # returns the current time in time zone "Asia/Taipei"
Converting times between timezones
zone_ua = TimeZone.new("Kyiv")
zone_ar = TimeZone.new("Buenos Aires")
time_ua = zone_ua.local(2022, 2, 24, 5, 30, 0)
time_ar = time_ua.in(zone_ar.location)
time_ua
# => 2022-02-24 05:30:00.0 +02:00 Europe/Kiev
time_ar
# => 2022-02-24 00:30:00.0 -03:00 America/Argentina/Buenos_Aires
Contributing
- Fork it (https://github.com/mamantoha/time_zone/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- Anton Maminov - creator and maintainer