time-zone 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +9 -0
- data/README.md +26 -3
- data/Rakefile +7 -0
- data/lib/time/zone.rb +4 -23
- data/lib/time/zone/locking.rb +45 -0
- data/lib/time/zone/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f87514b362f81ee4964adead9a93cd19994225c85e06b8c3584caabe8102123
|
4
|
+
data.tar.gz: 5a0f736d97c9ad2093d9069a22a043b96e1d91ec53c833b7f8a60ff87e332e3e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79db731b27df02f4a817709403dd0450f780697f47818fb6cd77e07c893509c25a436c2dbd8c2c257191a14f075bee4a3fc3f162ba7b10d532446b2e347a2544
|
7
|
+
data.tar.gz: 2c65c7d6ff558e6b00f3629dff240eb0ae0dd2e99dbd985f99a5d7e678962c7f9b7d96b5ddd338e12fa84592ecb953f17e60fd8910962743dfb9ab2f43d7c3ef
|
data/Gemfile
CHANGED
@@ -4,3 +4,12 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
|
4
4
|
|
5
5
|
# Specify your gem's dependencies in time-zone.gemspec
|
6
6
|
gemspec
|
7
|
+
|
8
|
+
group :development do
|
9
|
+
gem 'pry'
|
10
|
+
end
|
11
|
+
|
12
|
+
group :test do
|
13
|
+
gem 'simplecov'
|
14
|
+
gem 'coveralls', require: false
|
15
|
+
end
|
data/README.md
CHANGED
@@ -2,10 +2,16 @@
|
|
2
2
|
|
3
3
|
A very simple time-zone library which works by manipulating the `TZ` environment variable.
|
4
4
|
|
5
|
+
[![Build Status](https://secure.travis-ci.org/ioquatix/time-zone.svg)](http://travis-ci.org/ioquatix/time-zone)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/ioquatix/time-zone.svg)](https://codeclimate.com/github/ioquatix/time-zone)
|
7
|
+
[![Coverage Status](https://coveralls.io/repos/ioquatix/time-zone/badge.svg)](https://coveralls.io/r/ioquatix/time-zone)
|
8
|
+
|
5
9
|
## Motivation
|
6
10
|
|
7
11
|
`tzinfo` has an antiquated view of `Time`. It returns local time values with a UTC time zone which is pretty much wrong.
|
8
12
|
|
13
|
+
[![A brief history of Time.new](https://img.youtube.com/vi/UjdtH5gO_DQ/0.jpg)](https://www.youtube.com/watch?v=UjdtH5gO_DQ)
|
14
|
+
|
9
15
|
## Installation
|
10
16
|
|
11
17
|
Add this line to your application's Gemfile:
|
@@ -16,15 +22,15 @@ gem 'time-zone'
|
|
16
22
|
|
17
23
|
And then execute:
|
18
24
|
|
19
|
-
|
25
|
+
$ bundle
|
20
26
|
|
21
27
|
Or install it yourself as:
|
22
28
|
|
23
|
-
|
29
|
+
$ gem install time-zone
|
24
30
|
|
25
31
|
## Usage
|
26
32
|
|
27
|
-
There are two basic use cases
|
33
|
+
There are two basic use cases, getting the current time, and converting times.
|
28
34
|
|
29
35
|
### Current Time
|
30
36
|
|
@@ -40,6 +46,23 @@ Time::Zone.convert(Time.now.utc, "Pacific/Auckland")
|
|
40
46
|
=> 2018-05-25 14:14:22 +1200
|
41
47
|
```
|
42
48
|
|
49
|
+
### Multiple Operations
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
Time::Zone.with("US/Pacific") do
|
53
|
+
# Be aware that in some cases Time values are lazy initialized, so you need to use #localtime to force them to evaluate and use the current `TZ`.
|
54
|
+
time = Time.new.localtime
|
55
|
+
puts time.inspect
|
56
|
+
# => 2018-05-24 20:32:29 -0700
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
### Caveats
|
61
|
+
|
62
|
+
This implementation manipulates the per-process `TZ` environment variable. This means that if you don't put locking around all your usage of `localtime`, you may end up with strange results (i.e. in a timezone other than the system specified `TZ`). In the future, this requirement may go away.
|
63
|
+
|
64
|
+
This library is unlikely to work on Windows because it doesn't correctly handle changes to `TZ`.
|
65
|
+
|
43
66
|
## Contributing
|
44
67
|
|
45
68
|
1. Fork it
|
data/Rakefile
CHANGED
data/lib/time/zone.rb
CHANGED
@@ -20,39 +20,20 @@
|
|
20
20
|
|
21
21
|
require_relative 'zone/version'
|
22
22
|
|
23
|
-
|
23
|
+
require_relative 'zone/locking'
|
24
24
|
|
25
25
|
class Time
|
26
26
|
module Zone
|
27
|
-
@tz = Mutex.new
|
28
|
-
|
29
|
-
def self.with(zone)
|
30
|
-
@tz.synchronize do
|
31
|
-
original_zone = ENV['TZ']
|
32
|
-
|
33
|
-
begin
|
34
|
-
ENV['TZ'] = zone
|
35
|
-
|
36
|
-
yield
|
37
|
-
ensure
|
38
|
-
if original_zone
|
39
|
-
ENV['TZ'] = original_zone
|
40
|
-
else
|
41
|
-
ENV.delete('TZ')
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
27
|
def self.now(zone)
|
48
28
|
with(zone) do
|
49
|
-
|
29
|
+
# Time instances are lazy initialized, so we need to force it to pick up the current TZ by invoking #localtime
|
30
|
+
return Time.new.localtime
|
50
31
|
end
|
51
32
|
end
|
52
33
|
|
53
34
|
def self.convert(time, zone)
|
54
35
|
with(zone) do
|
55
|
-
return
|
36
|
+
return time.localtime
|
56
37
|
end
|
57
38
|
end
|
58
39
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'thread'
|
22
|
+
|
23
|
+
class Time
|
24
|
+
module Zone
|
25
|
+
LOCK = Mutex.new
|
26
|
+
|
27
|
+
def self.with(zone)
|
28
|
+
LOCK.synchronize do
|
29
|
+
original_zone = ENV['TZ']
|
30
|
+
|
31
|
+
begin
|
32
|
+
ENV['TZ'] = zone
|
33
|
+
|
34
|
+
yield
|
35
|
+
ensure
|
36
|
+
if original_zone
|
37
|
+
ENV['TZ'] = original_zone
|
38
|
+
else
|
39
|
+
ENV.delete('TZ')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/time/zone/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time-zone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
@@ -66,6 +66,7 @@ files:
|
|
66
66
|
- README.md
|
67
67
|
- Rakefile
|
68
68
|
- lib/time/zone.rb
|
69
|
+
- lib/time/zone/locking.rb
|
69
70
|
- lib/time/zone/version.rb
|
70
71
|
- time-zone.gemspec
|
71
72
|
homepage: https://github.com/ioquatix/time-zone
|