time_zone_converter 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 23de7d2a76414b511e4322aad37a2ddb56803247861f7a8182197ffd37d7cea1
4
- data.tar.gz: 1e52e262174c681d71fff650d147340a6ded44abe34ccdfc709a0f6b281e9127
3
+ metadata.gz: df821687ee506d0d1b1d0bebf2f928ffcbfbcd068afcc2d43acdb941c33b90ad
4
+ data.tar.gz: 4f374c9d6270b157b04c4472186a297eb261f0541b4c20f01899680c3fc44aa6
5
5
  SHA512:
6
- metadata.gz: 6da257eddd5c8cf82ff94ca3fa0b07a10721d7845c3ff8e33258a9bf5e81fd1b7e7ce9867a16b0302ab7d393bc6b497f91690dde94faf789d13754681797fd6b
7
- data.tar.gz: fdd50f7b6a9396d8522fac924c7820a6955aeee3fd72d3a17c4c646bdc900c13856cac2e16c70133d7fbbd62a5bd23bb42c5b0bac83deb5a80c3c7cbd35e1116
6
+ metadata.gz: 837ab5580d01ec1cab2308806894442494852d851f3b1c0068f6c6712691648c88703927d0c4d86a2840ed7a3c51e4a9caa49ddb4ec0d04bbad1b179da2b95d9
7
+ data.tar.gz: ac9c36c131134eb16838989056656042732d9222829e0792e9dce803a9aed9c0751a7034db619402cc3665cb19684f5340448bc9e74416d0c81db5eca1af26b9
data/.gitignore CHANGED
@@ -12,3 +12,4 @@
12
12
 
13
13
  .DS_Store
14
14
  .byebug_history
15
+ *.gem
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- time_zone_converter (0.1.0)
4
+ time_zone_converter (0.2.0)
5
5
  activesupport
6
6
  nearest_time_zone
7
7
  oj
@@ -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
- if method == :local
17
- time_zone = get_zone(args.first)
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
- def self.get_time(city, time)
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.get_zone(city)
40
- json_file = 'data/cities.json'
41
- json = Oj.load(File.read(json_file))
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
- # returns UTC
55
- def self.string_to_time(string, method, time_zone)
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
- if method == :utc
60
- offset = 0 # UTC +0
61
- current_time = Time.new.utc
62
-
63
- Time.new(
64
- current_time.year,
65
- current_time.month,
66
- current_time.day,
67
- $1.to_i,
68
- $2.to_i,
69
- 0,
70
- offset
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
@@ -1,3 +1,3 @@
1
1
  module TimeZoneConverter
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  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.1.1
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-13 00:00:00.000000000 Z
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