tzformater 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 14421c3b083881ac7df704c121140f0c1b8c2c1e
4
+ data.tar.gz: 52dc257c7d6990550345744f5428891dcafb0e50
5
+ SHA512:
6
+ metadata.gz: 2b8d8f22cfb4156d2284fc69bddc61a1f93ab8c9e6fcf15c8fffa7c7b5b65291be95fbfdd5545fb68866da3ec5761cb30617b08d4458bd6dcfbed84c8b1a1561
7
+ data.tar.gz: 2ef74245bdb03fbd9faacc3d7347cb4b21369d0c019b2ad87cc24e4955d3159157156f35531e3e8914cb600b7715062dbc6ce5104380558cbeb4eca39772c8d2
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.2"
4
+ - "1.9.3"
5
+ - "2.0.0"
6
+ - ruby-head
7
+ - jruby-19mode # JRuby in 1.9 mode
8
+ # uncomment this line if your project needs to run something other than `rake`:
9
+ # script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in TZ-formater.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Dmitry Tonkonogov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Dmitry Tonkonogov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # TZ::Formater
2
+ [![Build Status](https://travis-ci.org/tonkonogov/TZ-formater.png?branch=master)](https://travis-ci.org/tonkonogov/TZ-formater)
3
+ [![Coverage Status](https://coveralls.io/repos/tonkonogov/TZ-formater/badge.png)](https://coveralls.io/r/tonkonogov/TZ-formater)
4
+ [![Code Climate](https://codeclimate.com/github/tonkonogov/TZ-formater.png)](https://codeclimate.com/github/tonkonogov/TZ-formater)
5
+ [![Dependency Status](https://gemnasium.com/tonkonogov/TZ-formater.png)](https://gemnasium.com/tonkonogov/TZ-formater)
6
+
7
+ Ruby gem for timezone format converting.
8
+
9
+ This gem provides four class for converting a timezone format into each other.
10
+ * PosixTZ
11
+ * OlsonTZ
12
+ * WinTZ
13
+ * WinRegTZ
14
+
15
+ NOTE: this gem doesn't modify base ruby classes
16
+
17
+ ## Example of timezones
18
+
19
+ Drawing on the example of the Alaskan timezone:
20
+ * Olson - America/Anchorage
21
+ * POSIX - AKST9AKDT,M3.2.0,M11.1.0
22
+ * Win - (UTC-09:00) Alaska
23
+ * Windows registry - Alaskan Standard Time
24
+
25
+ ## Installation
26
+
27
+ Add this line to your application's Gemfile:
28
+
29
+ gem 'TZ-formater'
30
+
31
+ And then execute:
32
+
33
+ $ bundle
34
+
35
+ Or install it yourself as:
36
+
37
+ $ gem install TZ-formater
38
+
39
+ ## Usage
40
+
41
+ Get all timezones in current format:
42
+
43
+ TZFormater::PosixTZ.get_all
44
+
45
+ Create timezone with specific format:
46
+
47
+ TZFormater::OlsonTZ.new('Europe/London')
48
+ TZFormater::PosixTZ.new('MST7')
49
+
50
+ Get string representation for current timezone
51
+
52
+ london_olson.name # => "Europe/London"
53
+
54
+ Convert timezone to the other format (returns new object):
55
+
56
+ olson_format.to_win
57
+ olson_format.to_win_reg
58
+ olson_format.to_posix
59
+ posix_format.to_olson
60
+
61
+ Note: you able to convert, for example, a timezone in the posix format to one in the posix format but instead of converting the self object will be returned.
62
+
63
+ Get timezone offset:
64
+
65
+ london_win.offset # => 0
66
+
67
+ ### More complicated examples
68
+
69
+ Get name of converted timezone:
70
+
71
+ london_olson.to_posix.name # => "GMT0BST,M3.5.0/1,M10.5.0"
72
+
73
+ Get string represantation of all timezones:
74
+
75
+ TZFormater::PosixTZ.get_all.map(&:name) # => Very long array with posix timezones
76
+
77
+ Get list of timezones sorted by utc offset:
78
+
79
+ TZFormater::WinTZ.get_all.sort_by(&:offset)
80
+
81
+ ## Contributing
82
+
83
+ 1. Fork it ( http://github.com/tonkonogov/TZ-formater/fork )
84
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
85
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
86
+ 4. Push to the branch (`git push origin my-new-feature`)
87
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
data/lib/tzformater.rb ADDED
@@ -0,0 +1,10 @@
1
+ module TZFormater
2
+ FILENAME = File.expand_path('tzformater/data/formats.pstore', File.dirname(__FILE__))
3
+ end
4
+
5
+ require 'tzformater/common/pstore_adapter'
6
+ require 'tzformater/common/timezone'
7
+ require 'tzformater/posixtz'
8
+ require 'tzformater/wintz'
9
+ require 'tzformater/olsontz'
10
+ require 'tzformater/winregtz'
@@ -0,0 +1,30 @@
1
+ require 'pstore'
2
+
3
+ module TZFormater
4
+ module Common
5
+ class PstoreAdapter
6
+ READ_ONLY = true
7
+
8
+ def initialize(filename)
9
+ raise PStore::Error.new("Cannot find such file -- " << filename) unless File.exist?(filename)
10
+ @file = PStore.new(filename)
11
+ end
12
+
13
+ def get_by(type, timezone)
14
+ get_all(type).fetch(timezone, nil)
15
+ end
16
+
17
+ def get_all_keys(type)
18
+ get_all(type).keys
19
+ end
20
+
21
+ def get_all(type)
22
+ list = {}
23
+ @file.transaction(READ_ONLY) do
24
+ list = @file.fetch(type, {})
25
+ end
26
+ list
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,46 @@
1
+ module TZFormater
2
+ module Common
3
+ class Timezone
4
+ attr_reader :name
5
+
6
+ methods = {
7
+ posix: :PosixTZ,
8
+ olson: :OlsonTZ,
9
+ win: :WinTZ,
10
+ win_reg: :WinRegTZ
11
+ }
12
+
13
+ @@storage = TZFormater::Common::PstoreAdapter.new(TZFormater::FILENAME)
14
+
15
+ def self.all
16
+ @@storage.get_all(@key).map{|key, value| self.new(key, value) }
17
+ end
18
+
19
+ def initialize(tz, info = nil)
20
+ @name = tz
21
+ @info = info || @@storage.get_by(@key, @name)
22
+ raise ArgumentError.new("Unknown #{@key} timezone") if @info.nil?
23
+ end
24
+
25
+ methods.each do |name, klass|
26
+ define_method("to_#{name}") do
27
+ return self if @key == name
28
+ process_tzs(@info[name]) {|tz| TZFormater.const_get(klass).new(tz) }
29
+ end
30
+ end
31
+
32
+ def offset
33
+ @info[:offset]
34
+ end
35
+
36
+ private
37
+ def process_tzs(tz_list)
38
+ if tz_list.kind_of? Array
39
+ tz_list.map{|tz| yield(tz) }
40
+ else
41
+ yield(tz_list)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
Binary file
@@ -0,0 +1,10 @@
1
+ module TZFormater
2
+ class OlsonTZ < TZFormater::Common::Timezone
3
+ @key = :olson
4
+
5
+ def initialize(*args)
6
+ @key = :olson
7
+ super
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module TZFormater
2
+ class PosixTZ < TZFormater::Common::Timezone
3
+ @key = :posix
4
+
5
+ def initialize(*args)
6
+ @key = :posix
7
+ super
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module TZFormater
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ module TZFormater
2
+ class WinRegTZ < TZFormater::Common::Timezone
3
+ @key = :win_reg
4
+
5
+ def initialize(*args)
6
+ @key = :win_reg
7
+ super
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module TZFormater
2
+ class WinTZ < TZFormater::Common::Timezone
3
+ @key = :win
4
+
5
+ def initialize(*args)
6
+ @key = :win
7
+ super
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,123 @@
1
+ require 'spec_helper'
2
+
3
+ describe TZFormater::OlsonTZ do
4
+ let(:tz) { 'Asia/Vladivostok' } # Vladivostok
5
+ let(:posix_class) { TZFormater::PosixTZ }
6
+ let(:olson_class) { TZFormater::OlsonTZ }
7
+ let(:win_class) { TZFormater::WinTZ }
8
+ let(:win_reg_class) { TZFormater::WinRegTZ }
9
+
10
+ let(:olson_tz) { olson_class.new(tz) }
11
+
12
+ context 'class method' do
13
+ subject { olson_class }
14
+
15
+ context '.all' do
16
+ it 'contains one of timezone' do
17
+ expect(subject.all.map(&:name)).to include(tz)
18
+ end
19
+
20
+ it 'contains instances of the same class' do
21
+ expect(subject.all[0]).to be_an_instance_of subject
22
+ end
23
+
24
+ it 'returns an enumerable object' do
25
+ expect(subject.all).to be_a_kind_of Enumerable
26
+ end
27
+ end
28
+
29
+ context '.new' do
30
+ context 'without tz' do
31
+ it 'raises an ArgumentError' do
32
+ expect { subject.new }.to raise_error ArgumentError
33
+ end
34
+
35
+ it 'raises with correct standard message' do
36
+ expect { subject.new }.to raise_error(/wrong number of arguments/)
37
+ end
38
+ end
39
+
40
+ context 'with incorrect tz' do
41
+ let(:tz) { 'random stuff' }
42
+
43
+ it 'raises an ArgumentError' do
44
+ expect { olson_tz }.to raise_error ArgumentError
45
+ end
46
+
47
+ it 'raises with correct custom message' do
48
+ expect { olson_tz }.to raise_error 'Unknown olson timezone'
49
+ end
50
+ end
51
+
52
+ context 'with correct tz' do
53
+ it 'creates valid object' do
54
+ expect(olson_tz).to be_an_instance_of olson_class
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ context 'instance method' do
61
+ subject(:olsontz_instance) { olson_tz }
62
+
63
+ context '#name' do
64
+ subject { olsontz_instance.name }
65
+
66
+ it 'returns a string' do
67
+ expect(subject).to be_an_instance_of String
68
+ end
69
+
70
+ it 'returns its name' do
71
+ expect(subject).to eql tz
72
+ end
73
+ end
74
+
75
+ context '#to_olson' do
76
+ subject { olsontz_instance.to_olson }
77
+
78
+ it 'returns an olsontz instance' do
79
+ expect(subject).to be_an_instance_of olson_class
80
+ end
81
+
82
+ it 'doesn\'t create a new instance' do
83
+ expect(subject).to be olsontz_instance
84
+ end
85
+ end
86
+
87
+ context '#to_posix' do
88
+ subject { olsontz_instance.to_posix }
89
+
90
+ it 'returns a posixtz instance' do
91
+ expect(subject).to be_an_instance_of posix_class
92
+ end
93
+ end
94
+
95
+ context '#to_win' do
96
+ subject { olsontz_instance.to_win }
97
+
98
+ it 'returns a wintz instance' do
99
+ expect(subject).to be_an_instance_of win_class
100
+ end
101
+ end
102
+
103
+ context '#to_win_reg' do
104
+ subject { olsontz_instance.to_win_reg }
105
+
106
+ it 'returns a list' do
107
+ expect(subject).to be_an_instance_of Array
108
+ end
109
+
110
+ it 'contains WinRegTZ objects' do
111
+ expect(subject.first).to be_an_instance_of win_reg_class
112
+ end
113
+ end
114
+
115
+ context '#offset' do
116
+ subject { olsontz_instance.offset }
117
+
118
+ it 'returns an integer' do
119
+ expect(subject).to be_a_kind_of Integer
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,131 @@
1
+ require 'spec_helper'
2
+
3
+ describe TZFormater::PosixTZ do
4
+ let(:tz) { 'VLAT-11' } # Vladivostok
5
+ let(:posix_class) { TZFormater::PosixTZ }
6
+ let(:olson_class) { TZFormater::OlsonTZ }
7
+ let(:win_class) { TZFormater::WinTZ }
8
+ let(:win_reg_class) { TZFormater::WinRegTZ }
9
+
10
+ let(:posix_tz) { posix_class.new(tz) }
11
+
12
+ context 'class method' do
13
+ subject { posix_class }
14
+
15
+ context '.all' do
16
+ it 'contains one of timezone' do
17
+ expect(subject.all.map(&:name)).to include tz
18
+ end
19
+
20
+ it 'contains instances of the same class' do
21
+ expect(subject.all[0]).to be_an_instance_of subject
22
+ end
23
+
24
+ it 'returns an enumerable object' do
25
+ expect(subject.all).to be_a_kind_of Enumerable
26
+ end
27
+ end
28
+
29
+ context '.new' do
30
+ context 'without tz' do
31
+ it 'raises an ArgumentError' do
32
+ expect { subject.new }.to raise_error ArgumentError
33
+ end
34
+
35
+ it 'raises with correct standard message' do
36
+ expect { subject.new }.to raise_error(/wrong number of arguments/)
37
+ end
38
+ end
39
+
40
+ context 'with incorrect tz' do
41
+ let(:tz) { 'random stuff' }
42
+
43
+ it 'raises an ArgumentError' do
44
+ expect { posix_tz }.to raise_error ArgumentError
45
+ end
46
+
47
+ it 'raises with correct custom message' do
48
+ expect { posix_tz }.to raise_error 'Unknown posix timezone'
49
+ end
50
+ end
51
+
52
+ context 'with correct tz' do
53
+ it 'creates valid object' do
54
+ expect(posix_tz).to be_an_instance_of posix_class
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ context 'instance method' do
61
+ subject(:posixtz_instance) { posix_tz }
62
+
63
+ context '#name' do
64
+ subject { posixtz_instance.name }
65
+
66
+ it 'returns a string' do
67
+ expect(subject).to be_an_instance_of String
68
+ end
69
+
70
+ it 'returns its name' do
71
+ expect(subject).to eql tz
72
+ end
73
+ end
74
+
75
+ context '#to_posix' do
76
+ subject { posixtz_instance.to_posix }
77
+
78
+ it 'returns a posix instance' do
79
+ expect(subject).to be_an_instance_of posix_class
80
+ end
81
+
82
+ it 'doesn\'t create new object' do
83
+ expect(subject).to be posixtz_instance
84
+ end
85
+ end
86
+
87
+ context '#to_olson' do
88
+ subject { posixtz_instance.to_olson }
89
+
90
+ it 'returns a list' do
91
+ expect(subject).to be_an_instance_of Array
92
+ end
93
+
94
+ it 'contains OlsonTZ objects' do
95
+ expect(subject.first).to be_an_instance_of olson_class
96
+ end
97
+ end
98
+
99
+ context '#to_win' do
100
+ subject { posixtz_instance.to_win }
101
+
102
+ it 'returns a list' do
103
+ expect(subject).to be_an_instance_of Array
104
+ end
105
+
106
+ it 'contains WinTZ objects' do
107
+ expect(subject.first).to be_an_instance_of win_class
108
+ end
109
+ end
110
+
111
+ context '#to_win_reg' do
112
+ subject { posixtz_instance.to_win_reg }
113
+
114
+ it 'returns a list' do
115
+ expect(subject).to be_an_instance_of Array
116
+ end
117
+
118
+ it 'contains WinRegTZ objects' do
119
+ expect(subject.first).to be_an_instance_of win_reg_class
120
+ end
121
+ end
122
+
123
+ context '#offset' do
124
+ subject { posixtz_instance.offset }
125
+
126
+ it 'returns an integer' do
127
+ expect(subject).to be_a_kind_of Integer
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,112 @@
1
+ require 'spec_helper'
2
+
3
+ describe TZFormater::Common::PstoreAdapter, depends: :PstoreHelper do
4
+ let(:pstore_adapter) { TZFormater::Common::PstoreAdapter.new(filename) }
5
+ after(:all) { delete_test_file }
6
+
7
+ context 'when incorrect file provided' do
8
+ let!(:filename) { 'azaza' }
9
+
10
+ it 'raises an PStore::Error' do
11
+ expect { pstore_adapter }.to raise_error(PStore::Error)
12
+ end
13
+
14
+ it 'contains correct description for exception' do
15
+ expect { pstore_adapter }.to raise_error("Cannot find such file -- " + filename)
16
+ end
17
+ end
18
+
19
+ context 'when correct file provided' do
20
+ let!(:filename) { generate_test_file }
21
+
22
+ context '#get_all_keys' do
23
+ subject { pstore_adapter.get_all_keys(type) }
24
+
25
+ context 'with existed type' do
26
+ let!(:type) { :posix }
27
+
28
+ it 'returns all keys of type' do
29
+ expect(subject).to eql(['VLAT-11', 'CET-1CEST,M3.5.0,M10.5.0/3']) # TODO: get assertion from stub_file
30
+ end
31
+ end
32
+
33
+ context 'with non-existed type' do
34
+ let!(:type) { :gavno }
35
+
36
+ it 'returns empty object' do
37
+ expect(subject.size).to eql(0)
38
+ end
39
+
40
+ it 'returns an Array object' do
41
+ expect(subject).to be_an_instance_of(Array)
42
+ end
43
+ end
44
+ end
45
+
46
+ context '#get_all' do
47
+ subject { pstore_adapter.get_all(type) }
48
+
49
+ context 'with existed type' do
50
+ let!(:type) { :posix }
51
+
52
+ it 'returns a complete value of such root' do
53
+ expect(subject).to eql({"VLAT-11" => "test_string", "CET-1CEST,M3.5.0,M10.5.0/3" => "test_string2"}) # TODO: get assertion from stub_file
54
+ end
55
+ end
56
+
57
+ context 'with non-existed type' do
58
+ let!(:type) { :gavno }
59
+
60
+ it 'returns empty object' do
61
+ expect(subject.size).to eql(0)
62
+ end
63
+
64
+ it 'returns a Hash object' do
65
+ expect(subject).to be_an_instance_of(Hash)
66
+ end
67
+ end
68
+ end
69
+
70
+ context '#get_by' do
71
+ let!(:key) { 'VLAT-11' }
72
+ subject { pstore_adapter.get_by(type, key) }
73
+
74
+ context 'with non-existed type' do
75
+ let!(:type) { :gavno }
76
+
77
+ context 'and existed key' do # actualy it makes no sense because any key doesn't exist in nonexisted type
78
+ it 'returns nil' do
79
+ expect(subject).to be_nil
80
+ end
81
+ end
82
+
83
+ context 'and non-existed key' do
84
+ let!(:key) { 'VLAT-12' }
85
+
86
+ it 'returns nil' do
87
+ expect(subject).to be_nil
88
+ end
89
+ end
90
+ end
91
+
92
+ context 'with existed type' do
93
+ let!(:type) { :posix }
94
+
95
+ context 'and existed key' do
96
+ it 'returns required values' do
97
+ # TODO: get information from stub_file
98
+ expect(subject).to eql('test_string')
99
+ end
100
+ end
101
+
102
+ context 'and non-existed key' do
103
+ let!(:key) { 'VLAT-12' }
104
+
105
+ it 'returns nil' do
106
+ expect(subject).to be_nil
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,119 @@
1
+ require 'spec_helper'
2
+
3
+ describe TZFormater::WinRegTZ do
4
+ let(:tz) { 'Vladivostok Standard Time' } # Vladivostok
5
+ let(:posix_class) { TZFormater::PosixTZ }
6
+ let(:olson_class) { TZFormater::OlsonTZ }
7
+ let(:win_class) { TZFormater::WinTZ }
8
+ let(:win_reg_class) { TZFormater::WinRegTZ }
9
+
10
+ let(:win_reg_tz) { win_reg_class.new(tz) }
11
+
12
+ context 'class method' do
13
+ subject { win_reg_class }
14
+
15
+ context '.all' do
16
+ it 'contains one of timezone' do
17
+ expect(subject.all.map(&:name)).to include(tz)
18
+ end
19
+
20
+ it 'contains instances of the same class' do
21
+ expect(subject.all[0]).to be_an_instance_of subject
22
+ end
23
+
24
+ it 'returns an enumerable object' do
25
+ expect(subject.all).to be_a_kind_of Enumerable
26
+ end
27
+ end
28
+
29
+ context '.new' do
30
+ context 'without tz' do
31
+ it 'raises an ArgumentError' do
32
+ expect { subject.new }.to raise_error ArgumentError
33
+ end
34
+
35
+ it 'raises with correct standard message' do
36
+ expect { subject.new }.to raise_error(/wrong number of arguments/)
37
+ end
38
+ end
39
+
40
+ context 'with incorrect tz' do
41
+ let(:tz) { 'random stuff' }
42
+
43
+ it 'raises an ArgumentError' do
44
+ expect { win_reg_tz }.to raise_error ArgumentError
45
+ end
46
+
47
+ it 'raises with correct custom message' do
48
+ expect { win_reg_tz }.to raise_error 'Unknown win_reg timezone'
49
+ end
50
+ end
51
+
52
+ context 'with correct tz' do
53
+ it 'creates valid object' do
54
+ expect(win_reg_tz).to be_an_instance_of win_reg_class
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ context 'instance method' do
61
+ subject(:winregtz_instance) { win_reg_tz }
62
+
63
+ context '#name' do
64
+ subject { winregtz_instance.name }
65
+
66
+ it 'returns a string' do
67
+ expect(subject).to be_an_instance_of String
68
+ end
69
+
70
+ it 'returns its name' do
71
+ expect(subject).to eql tz
72
+ end
73
+ end
74
+
75
+ context '#to_win_reg' do
76
+ subject { winregtz_instance.to_win_reg }
77
+
78
+ it 'returns a winregtz instance' do
79
+ expect(subject).to be_an_instance_of win_reg_class
80
+ end
81
+
82
+ it 'doesn\'t create a new instance' do
83
+ expect(subject).to be winregtz_instance
84
+ end
85
+ end
86
+
87
+ context '#to_posix' do
88
+ subject { winregtz_instance.to_posix }
89
+
90
+ it 'returns a posixtz instance' do
91
+ expect(subject).to be_an_instance_of posix_class
92
+ end
93
+ end
94
+
95
+ context '#to_olson' do
96
+ subject { winregtz_instance.to_olson }
97
+
98
+ it 'returns an olsontz instance' do
99
+ expect(subject).to be_an_instance_of olson_class
100
+ end
101
+ end
102
+
103
+ context '#to_win' do
104
+ subject { winregtz_instance.to_win }
105
+
106
+ it 'returns a wintz instance' do
107
+ expect(subject).to be_an_instance_of win_class
108
+ end
109
+ end
110
+
111
+ context '#offset' do
112
+ subject { winregtz_instance.offset }
113
+
114
+ it 'returns an integer' do
115
+ expect(subject).to be_a_kind_of Integer
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,123 @@
1
+ require 'spec_helper'
2
+
3
+ describe TZFormater::WinTZ do
4
+ let(:tz) { '(UTC+11:00) Vladivostok' } # Vladivostok
5
+ let(:posix_class) { TZFormater::PosixTZ }
6
+ let(:olson_class) { TZFormater::OlsonTZ }
7
+ let(:win_class) { TZFormater::WinTZ }
8
+ let(:win_reg_class) { TZFormater::WinRegTZ }
9
+
10
+ let(:win_tz) { win_class.new(tz) }
11
+
12
+ context 'class method' do
13
+ subject { win_class }
14
+
15
+ context '.all' do
16
+ it 'contains one of timezone' do
17
+ expect(subject.all.map(&:name)).to include(tz)
18
+ end
19
+
20
+ it 'contains instances of the same class' do
21
+ expect(subject.all[0]).to be_an_instance_of subject
22
+ end
23
+
24
+ it 'returns an enumerable object' do
25
+ expect(subject.all).to be_a_kind_of Enumerable
26
+ end
27
+ end
28
+
29
+ context '.new' do
30
+ context 'without tz' do
31
+ it 'raises an ArgumentError' do
32
+ expect { subject.new }.to raise_error ArgumentError
33
+ end
34
+
35
+ it 'raises with correct standard message' do
36
+ expect { subject.new }.to raise_error(/wrong number of arguments/)
37
+ end
38
+ end
39
+
40
+ context 'with incorrect tz' do
41
+ let(:tz) { 'random stuff' }
42
+
43
+ it 'raises an ArgumentError' do
44
+ expect { win_tz }.to raise_error ArgumentError
45
+ end
46
+
47
+ it 'raises with correct custom message' do
48
+ expect { win_tz }.to raise_error 'Unknown win timezone'
49
+ end
50
+ end
51
+
52
+ context 'with correct tz' do
53
+ it 'creates valid object' do
54
+ expect(win_tz).to be_an_instance_of win_class
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ context 'instance method' do
61
+ subject(:wintz_instance) { win_tz }
62
+
63
+ context '#name' do
64
+ subject { wintz_instance.name }
65
+
66
+ it 'returns a string' do
67
+ expect(subject).to be_an_instance_of String
68
+ end
69
+
70
+ it 'returns its name' do
71
+ expect(subject).to eql tz
72
+ end
73
+ end
74
+
75
+ context '#to_win' do
76
+ subject { wintz_instance.to_win }
77
+
78
+ it 'returns a wintz instance' do
79
+ expect(subject).to be_an_instance_of win_class
80
+ end
81
+
82
+ it 'doesn\'t create a new instance' do
83
+ expect(subject).to be wintz_instance
84
+ end
85
+ end
86
+
87
+ context '#to_posix' do
88
+ subject { wintz_instance.to_posix }
89
+
90
+ it 'returns a posixtz instance' do
91
+ expect(subject).to be_an_instance_of posix_class
92
+ end
93
+ end
94
+
95
+ context '#to_olson' do
96
+ subject { wintz_instance.to_olson }
97
+
98
+ it 'returns an olsontz instance' do
99
+ expect(subject).to be_an_instance_of olson_class
100
+ end
101
+ end
102
+
103
+ context '#to_win_reg' do
104
+ subject { wintz_instance.to_win_reg }
105
+
106
+ it 'returns a list' do
107
+ expect(subject).to be_an_instance_of Array
108
+ end
109
+
110
+ it 'contains WinRegTZ objects' do
111
+ expect(subject.first).to be_an_instance_of win_reg_class
112
+ end
113
+ end
114
+
115
+ context '#offset' do
116
+ subject { wintz_instance.offset }
117
+
118
+ it 'returns an integer' do
119
+ expect(subject).to be_a_kind_of Integer
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,29 @@
1
+ require 'pstore'
2
+ require 'json'
3
+
4
+ module PstoreHelper
5
+ FILENAME = File.expand_path('../test_data/test.pstore', File.dirname(__FILE__))
6
+ STUB_FILE = File.expand_path('../test_data/pstore_stub.json', File.dirname(__FILE__))
7
+
8
+ def generate_test_file
9
+ delete_test_file
10
+ generate_file(FILENAME, stub_data)
11
+ FILENAME
12
+ end
13
+
14
+ def delete_test_file
15
+ File.delete(FILENAME) if File.exist? FILENAME
16
+ end
17
+
18
+ private
19
+ def generate_file(filename, data)
20
+ file = PStore.new(filename)
21
+ file.transaction do
22
+ data.each{|key, value| file[key.to_sym] = value } # A Hash object can't be just assigned to a PStore object
23
+ end
24
+ end
25
+
26
+ def stub_data
27
+ JSON.load(File.read(STUB_FILE))
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+ require 'require_all'
4
+ require_rel 'helpers'
5
+
6
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
7
+ SimpleCov::Formatter::HTMLFormatter,
8
+ Coveralls::SimpleCov::Formatter
9
+ ]
10
+ SimpleCov.start do
11
+ add_filter '/spec/'
12
+ end
13
+
14
+ require 'tzformater'
15
+
16
+ RSpec.configure do |config|
17
+ config.include PstoreHelper, depends: :PstoreHelper
18
+
19
+ config.expect_with :rspec do |c|
20
+ c.syntax = :expect # disable the `should` syntax
21
+ end
22
+ end
@@ -0,0 +1,6 @@
1
+ {
2
+ "posix": {
3
+ "VLAT-11": "test_string",
4
+ "CET-1CEST,M3.5.0,M10.5.0/3": "test_string2"
5
+ }
6
+ }
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tzformater/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'tzformater'
8
+ spec.version = TZFormater::VERSION
9
+ spec.authors = ["Dmitry Tonkonogov"]
10
+ spec.email = ["omun.spambox@gmail.com"]
11
+ spec.summary = %q{Ruby timezone convefrting library}
12
+ spec.description = %q{Ruby gem for converting timezone to different formats (POSIX, Olson, windows-like, windows-registry)}
13
+ spec.homepage = "https://github.com/tonkonogov/TZ-formater"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency 'coveralls', '>= 0.7.0'
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "require_all"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "simplecov", ">= 0.8.2"
27
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tzformater
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Tonkonogov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: coveralls
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.7.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.7.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: require_all
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: 0.8.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: 0.8.2
97
+ description: Ruby gem for converting timezone to different formats (POSIX, Olson,
98
+ windows-like, windows-registry)
99
+ email:
100
+ - omun.spambox@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - .gitignore
106
+ - .rspec
107
+ - .travis.yml
108
+ - Gemfile
109
+ - LICENSE
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - lib/tzformater.rb
114
+ - lib/tzformater/common/pstore_adapter.rb
115
+ - lib/tzformater/common/timezone.rb
116
+ - lib/tzformater/data/formats.pstore
117
+ - lib/tzformater/olsontz.rb
118
+ - lib/tzformater/posixtz.rb
119
+ - lib/tzformater/version.rb
120
+ - lib/tzformater/winregtz.rb
121
+ - lib/tzformater/wintz.rb
122
+ - spec/case/olson_spec.rb
123
+ - spec/case/posix_spec.rb
124
+ - spec/case/pstore_adapter_spec.rb
125
+ - spec/case/win_reg_spec.rb
126
+ - spec/case/win_spec.rb
127
+ - spec/helpers/pstore_helper.rb
128
+ - spec/spec_helper.rb
129
+ - spec/test_data/pstore_stub.json
130
+ - tzformater.gemspec
131
+ homepage: https://github.com/tonkonogov/TZ-formater
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.0.14
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Ruby timezone convefrting library
155
+ test_files:
156
+ - spec/case/olson_spec.rb
157
+ - spec/case/posix_spec.rb
158
+ - spec/case/pstore_adapter_spec.rb
159
+ - spec/case/win_reg_spec.rb
160
+ - spec/case/win_spec.rb
161
+ - spec/helpers/pstore_helper.rb
162
+ - spec/spec_helper.rb
163
+ - spec/test_data/pstore_stub.json