time_zone_converter 0.1.1 → 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/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/lib/time_zone_converter.rb +38 -54
- data/lib/time_zone_converter/version.rb +1 -1
- metadata +2 -3
- data/time_zone_converter-0.1.0.gem +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df821687ee506d0d1b1d0bebf2f928ffcbfbcd068afcc2d43acdb941c33b90ad
|
4
|
+
data.tar.gz: 4f374c9d6270b157b04c4472186a297eb261f0541b4c20f01899680c3fc44aa6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 837ab5580d01ec1cab2308806894442494852d851f3b1c0068f6c6712691648c88703927d0c4d86a2840ed7a3c51e4a9caa49ddb4ec0d04bbad1b179da2b95d9
|
7
|
+
data.tar.gz: ac9c36c131134eb16838989056656042732d9222829e0792e9dce803a9aed9c0751a7034db619402cc3665cb19684f5340448bc9e74416d0c81db5eca1af26b9
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/lib/time_zone_converter.rb
CHANGED
@@ -13,77 +13,61 @@ module TimeZoneConverter
|
|
13
13
|
# https://stackoverflow.com/questions/8349817/ruby-gem-for-finding-timezone-of-location
|
14
14
|
|
15
15
|
def self.call(args, time = Time.current, method = :utc)
|
16
|
-
|
17
|
-
|
16
|
+
arr = Array.new
|
17
|
+
|
18
|
+
if method == :utc
|
19
|
+
time = string_to_time(time, "UTC")
|
20
|
+
arr = args.map { |city| [city, get_time(city, time)] }
|
21
|
+
else # :local
|
22
|
+
# Get time with zone for the first city
|
23
|
+
time_zone = get_nearest_time_zone(args.first)
|
24
|
+
time = string_to_time(time, time_zone)
|
25
|
+
|
26
|
+
# Add first item
|
27
|
+
arr << [args.first, time]
|
28
|
+
|
29
|
+
# Convert time for the rest of the cities in the args
|
30
|
+
args[1..-1].each { |city| arr << [city, get_time(city, time)] }
|
31
|
+
arr
|
18
32
|
end
|
19
|
-
time = string_to_time(time, method, time_zone) if time.is_a? String
|
20
|
-
args.map { |city| [city, get_time(city, time)] }
|
21
33
|
end
|
22
34
|
|
23
35
|
private
|
24
36
|
|
25
|
-
|
26
|
-
json_file = 'data/cities.json'
|
27
|
-
json = Oj.load(File.read(json_file))
|
28
|
-
item = json.select! { |k, _| k == city }
|
37
|
+
JSON_FILE = 'data/cities.json'
|
29
38
|
|
39
|
+
def self.get_nearest_time_zone(city)
|
40
|
+
json = Oj.load(File.read(JSON_FILE))
|
41
|
+
item = json.select! { |k, _| k == city }
|
30
42
|
raise "Not found #{city}" unless item
|
31
43
|
|
32
|
-
lat = item[city][0]
|
33
|
-
lng = item[city][1]
|
34
|
-
|
44
|
+
lat, lng = item[city][0], item[city][1]
|
35
45
|
time_zone = NearestTimeZone.to(lat.to_f, lng.to_f)
|
36
|
-
time = time.in_time_zone(time_zone)
|
37
46
|
end
|
38
47
|
|
39
|
-
def self.
|
40
|
-
|
41
|
-
|
42
|
-
item = json.select! { |k, _| k == city }
|
43
|
-
|
44
|
-
raise "Not found #{city}" unless item
|
45
|
-
|
46
|
-
lat = item[city][0]
|
47
|
-
lng = item[city][1]
|
48
|
-
|
49
|
-
time_zone = NearestTimeZone.to(lat.to_f, lng.to_f)
|
48
|
+
def self.get_time(city, time)
|
49
|
+
time_zone = get_nearest_time_zone(city)
|
50
|
+
time.in_time_zone(time_zone)
|
50
51
|
end
|
51
52
|
|
52
53
|
ISO_TIME = /\A(\d\d):(\d\d)\z/
|
53
54
|
|
54
|
-
|
55
|
-
|
56
|
-
# https://github.com/rails/rails/blob/aeba121a83965d242ed6d7fd46e9c166079a3230/activemodel/lib/active_model/type/helpers/time_value.rb#L65
|
55
|
+
def self.string_to_time(string, time_zone)
|
56
|
+
return unless string.is_a? String
|
57
57
|
|
58
58
|
if string =~ ISO_TIME
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
)
|
72
|
-
else
|
73
|
-
current_time = Time.current
|
74
|
-
zone = ActiveSupport::TimeZone[time_zone]
|
75
|
-
offset = zone.utc_offset
|
76
|
-
|
77
|
-
Time.new(
|
78
|
-
current_time.year,
|
79
|
-
current_time.month,
|
80
|
-
current_time.day,
|
81
|
-
$1.to_i,
|
82
|
-
$2.to_i,
|
83
|
-
0,
|
84
|
-
offset
|
85
|
-
)
|
86
|
-
end
|
59
|
+
zone = ActiveSupport::TimeZone[time_zone]
|
60
|
+
current_time = Time.new.utc
|
61
|
+
|
62
|
+
Time.new(
|
63
|
+
current_time.year,
|
64
|
+
current_time.month,
|
65
|
+
current_time.day,
|
66
|
+
$1.to_i,
|
67
|
+
$2.to_i,
|
68
|
+
0,
|
69
|
+
zone
|
70
|
+
)
|
87
71
|
end
|
88
72
|
end
|
89
73
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_zone_converter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rafał Trojanowski
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-09-
|
11
|
+
date: 2019-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -176,7 +176,6 @@ files:
|
|
176
176
|
- lib/time_zone_converter/cli.rb
|
177
177
|
- lib/time_zone_converter/json_data_transformer.rb
|
178
178
|
- lib/time_zone_converter/version.rb
|
179
|
-
- time_zone_converter-0.1.0.gem
|
180
179
|
- time_zone_converter.gemspec
|
181
180
|
homepage: https://github.com/rafaltrojanowski/time_zone_converter
|
182
181
|
licenses:
|
Binary file
|