time_zone_converter 0.2.3 → 0.2.4

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: d9203b26c2098a8c002ad9edc24f867bd3089a1c772b68ec136ee7c477384ba5
4
- data.tar.gz: 6dac940cca915e4da0e631a02de0d658cbdc6374d9ddee9e93a905ec98e25fa2
3
+ metadata.gz: 9da080f96c7de30913760de1e43e35f3fb7f0d088e8b6b9ac217e6a68ffd964b
4
+ data.tar.gz: a91db4a3b1364222e35b247f760f2caa9ebc1dbecfa76430b798f697c94d21d8
5
5
  SHA512:
6
- metadata.gz: 602cc22d5a521bbc444fc1e542fcaa73d4d3c058d188ed58a3f711224dee70e5a305f141a8ce67ba32ffb61cbee033bc06df3a2b4bf2e43ae5003b1e526c240c
7
- data.tar.gz: cc9245f61a6dfbbadf1c133f728d86db14d464e11fcd0a3c3e69beacf3f27b1e37fb16b4ebdb3fedbf059976f7363739e39ee43cc4c4048d98d72d6d3dd5ddca
6
+ metadata.gz: fb07e9d4d82bc44b1094df087463a90a228ae9552c99cbb77b40598128795f69d9a81b98b029a4a2825fa08bc85c2e0bcc0b11ab0714246403277c1b3c95a07b
7
+ data.tar.gz: fb80ef816c59ac156469c8b7b1d7966a13ba3ddd16424da31bd9ae4530327677cda0b2c2c803052b2025ea67b8940d090106555fe5510bc84ae322e16e21f4cc
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- time_zone_converter (0.2.3)
4
+ time_zone_converter (0.2.4)
5
5
  activesupport
6
6
  nearest_time_zone
7
7
  oj
@@ -25,7 +25,7 @@ GEM
25
25
  multi_json (~> 1.8)
26
26
  concurrent-ruby (1.1.5)
27
27
  diff-lcs (1.3)
28
- i18n (1.6.0)
28
+ i18n (1.7.0)
29
29
  concurrent-ruby (~> 1.0)
30
30
  kdtree (0.4)
31
31
  minitest (5.12.2)
@@ -36,7 +36,7 @@ GEM
36
36
  require_all
37
37
  oj (3.9.2)
38
38
  rake (10.5.0)
39
- require_all (2.0.0)
39
+ require_all (3.0.0)
40
40
  rspec (3.8.0)
41
41
  rspec-core (~> 3.8.0)
42
42
  rspec-expectations (~> 3.8.0)
@@ -60,7 +60,7 @@ GEM
60
60
  timecop (0.9.1)
61
61
  tzinfo (1.2.5)
62
62
  thread_safe (~> 0.1)
63
- zeitwerk (2.1.10)
63
+ zeitwerk (2.2.0)
64
64
 
65
65
  PLATFORMS
66
66
  ruby
data/README.md CHANGED
@@ -1,14 +1,13 @@
1
1
  # TimeZoneConverter
2
2
 
3
- A small gem that prints out the time in other locations.
4
- It does support a multiple cities, but it may be a bit slow in that case.
5
- Any ideas how to make it faster are highly welcomed.
3
+ ![screenshot](img/time-zone.gif)
6
4
 
7
- Inspired by:
8
- https://www.timeanddate.com/worldclock/converter.html
5
+ A small gem, that can be used as a Command Line Interface. Inspired by:
9
6
 
10
- It uses cities data extracted from cities gem: https://github.com/joecorcoran/cities
11
- For lat, long calculation it uses another gem: https://rubygems.org/gems/nearest_time_zone
7
+ - https://www.timeanddate.com/worldclock/converter.html
8
+ - https://stackoverflow.com/questions/8349817/ruby-gem-for-finding-timezone-of-location
9
+
10
+ In a few words, it prints out the time in other cities. Cities are passed as arguments.
12
11
 
13
12
  ## Usage:
14
13
 
@@ -16,22 +15,61 @@ For lat, long calculation it uses another gem: https://rubygems.org/gems/nearest
16
15
  ```
17
16
  time_zone_converter c Warszawa Bangkok
18
17
  ```
18
+ Output:
19
+ ```
20
+ [
21
+ ["Warszawa", Tue, 22 Oct 2019 13:30:02 CEST +02:00],
22
+ ["Bangkok", Tue, 22 Oct 2019 18:30:02 +07 +07:00]
23
+ ]
24
+ ```
19
25
 
20
26
  ### 'ct' command: given time in other cities (formant 'HH:MM' 24-hour)
21
27
  ```
22
28
  time_zone_converter ct 'Chiang Mai' Skopje '19:00'
23
29
  ```
30
+ Output:
31
+ ```
32
+ [
33
+ ["Chiang Mai", 2019-10-22 19:00:00 +0700],
34
+ ["Skopje", Tue, 22 Oct 2019 14:00:00 CEST +02:00]
35
+ ]
36
+ ```
37
+
38
+ Note: Time range are also supported:
24
39
 
25
- ### 'ctu' command: given UTC+0 time in other cities
26
40
  ```
27
- time_zone_converter ctu Bangkok Warszawa '10:00'
41
+ time_zone_converter ct Warszawa Bangkok '19:00-21:00'
28
42
  ```
29
43
 
44
+ Output:
30
45
 
31
- ### Output:
46
+ ```
47
+ [
48
+ ["Warszawa",
49
+ [
50
+ 2019-10-29 19:00:00 +0100,
51
+ 2019-10-29 21:00:00 +0100
52
+ ]
53
+ ],
54
+ ["Bangkok",
55
+ [
56
+ Wed, 30 Oct 2019 01:00:00 +07 +07:00,
57
+ Wed, 30 Oct 2019 03:00:00 +07 +07:00
58
+ ]
59
+ ]
60
+ ]
61
+ ```
32
62
 
63
+ ### 'ctu' command: given UTC+0 time in other cities
33
64
  ```
34
- [["Warszawa", 2019-10-04 10:00:00 +0200], ["Bangkok", Fri, 04 Oct 2019 15:00:00 +07 +07:00]]
65
+ time_zone_converter ctu Bangkok Warszawa '10:00'
66
+ ```
67
+ Output:
68
+ ```
69
+ [
70
+ ["Bangkok", Tue, 22 Oct 2019 17:00:00 +07 +07:00],
71
+ ["Warszawa", Tue, 22 Oct 2019 12:00:00 CEST +02:00]
72
+ ]
35
73
  ```
36
74
 
37
75
  ### Help:
@@ -39,4 +77,21 @@ time_zone_converter ctu Bangkok Warszawa '10:00'
39
77
  ```
40
78
  time_zone_converter
41
79
  time_zone_converter help c
42
- ```
80
+ ```
81
+
82
+ ### More
83
+
84
+ It supports multiple cities, however, it may be a bit slow in that case.
85
+ Any ideas how to make it faster are highly welcomed :)
86
+
87
+ Technically, this gem uses other gems under the hood:
88
+ `cities` gem, see here:
89
+ https://github.com/joecorcoran/cities
90
+ which provides latitude and longitude for all cities around the world.
91
+
92
+ The code responsible for extracting json data is placed in `json_data_transformer.rb` file.
93
+
94
+ For nearest time zone calculation it uses another gem called `nearest_tine_zone`:
95
+ https://rubygems.org/gems/nearest_time_zone
96
+
97
+ Summing up, it does not use any external API, just pure Ruby with ActiveSupport dependency.
data/img/time-zone.gif ADDED
Binary file
@@ -50,29 +50,45 @@ module TimeZoneConverter
50
50
  time_zone = NearestTimeZone.to(lat.to_f, lng.to_f)
51
51
  end
52
52
 
53
+ # time is a Time object or an Array of Time objects
53
54
  def self.get_time(city, time)
54
55
  time_zone = get_nearest_time_zone(city)
55
- time.in_time_zone(time_zone)
56
+ if time.is_a? Array
57
+ time.map { |t| t.in_time_zone(time_zone) }
58
+ else
59
+ time.in_time_zone(time_zone)
60
+ end
56
61
  end
57
62
 
58
63
  ISO_TIME = /\A(\d\d):(\d\d)\z/
64
+ ISO_TIME_RANGE = /\A(\d\d):(\d\d)-(\d\d):(\d\d)\z/
59
65
 
66
+ # Inspired by: https://github.com/rails/rails/blob/aeba121a83965d242ed6d7fd46e9c166079a3230/activemodel/lib/active_model/type/helpers/time_value.rb#L65
67
+ # @returns: Time object or an Array of Time objects
60
68
  def self.string_to_time(time, time_zone)
61
69
  return time unless time.is_a? String
70
+ current_time = Time.new.utc
71
+ zone = ActiveSupport::TimeZone[time_zone]
62
72
 
63
73
  if time =~ ISO_TIME
64
- zone = ActiveSupport::TimeZone[time_zone]
65
- current_time = Time.new.utc
66
-
67
- Time.new(
68
- current_time.year,
69
- current_time.month,
70
- current_time.day,
71
- $1.to_i,
72
- $2.to_i,
73
- 0,
74
- zone
75
- )
74
+ new_time(current_time.year, current_time.month, current_time.day, $1.to_i, $2.to_i, 0, 0, zone)
75
+ elsif time =~ ISO_TIME_RANGE
76
+ [
77
+ new_time(current_time.year, current_time.month, current_time.day, $1.to_i, $2.to_i, 0, 0, zone),
78
+ new_time(current_time.year, current_time.month, current_time.day, $3.to_i, $4.to_i, 0, 0, zone)
79
+ ]
76
80
  end
77
81
  end
82
+
83
+ def self.new_time(year, mon, mday, hour, min, sec, microsec, zone)
84
+ Time.new(
85
+ year,
86
+ mon,
87
+ mday,
88
+ hour,
89
+ min,
90
+ sec,
91
+ zone
92
+ )
93
+ end
78
94
  end
@@ -1,3 +1,3 @@
1
1
  module TimeZoneConverter
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
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.2.3
4
+ version: 0.2.4
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-10-04 00:00:00.000000000 Z
11
+ date: 2019-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -185,6 +185,7 @@ files:
185
185
  - bin/setup
186
186
  - data/cities.json
187
187
  - exe/time_zone_converter
188
+ - img/time-zone.gif
188
189
  - lib/time_zone_converter.rb
189
190
  - lib/time_zone_converter/cli.rb
190
191
  - lib/time_zone_converter/json_data_transformer.rb