timezonify 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 74ced003ca7ad8a1584e8ab112da1f2124f19b88
4
+ data.tar.gz: 54bb3fb3f2c3849fbb3e71591752405dd9ca504d
5
+ SHA512:
6
+ metadata.gz: 8b71de51c5b8600aa961cac0b1ad2529db4a9f307970c88745139acf53aa6c6f56cfddae9e259b1a44d185c25ffc3432a63c8658ae6a97b4ce5a601a9d611430
7
+ data.tar.gz: 0676c5adcf056b8c3c2e06725046ae6d4c342115923f61cf880887543026e465fb23e0096b933528c904cebe6c142094247ea19128badb6ddfd28b6c21afa95e
@@ -0,0 +1,2 @@
1
+ service_name: travis-ci
2
+ repo_token: zkrjRduOvPBRRjQl3sPfbhweWZYtrFUrr bundle exec rspec spec
@@ -0,0 +1,5 @@
1
+ build_steps_local
2
+ *.gem
3
+ .bundle
4
+ coverage
5
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ - "2.0.0"
5
+ script:
6
+ - bundle exec rake
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in timezonify.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ashish Upadhyay
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.
@@ -0,0 +1,36 @@
1
+ # Timezonify [![GitHub version](https://badge.fury.io/gh/gemathon-warriors%2Fzonify.png)](http://badge.fury.io/gh/gemathon-warriors%2Fzonify) [![Build Status](https://travis-ci.org/gemathon-warriors/timezonify.png?branch=master)](https://travis-ci.org/gemathon-warriors/timezonify) [![Coverage Status](https://coveralls.io/repos/gemathon-warriors/zonify/badge.png)](https://coveralls.io/r/gemathon-warriors/zonify)
2
+
3
+ > Wanna hold the World Clock in your hands ?? Then its about "time" you use timezonify.
4
+
5
+
6
+ ## Supports
7
+
8
+ Rails 1.8.7+
9
+
10
+ > Warning for 1.8.7 users : Use at your own risk as Ruby community has dropped support for 1.8.7
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'timezonify'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install timezonify
25
+
26
+ ## Usage
27
+
28
+ Timezonify::Timezone.timezone_for_time('10:00 AM')
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it ( http://github.com/<my-github-username>/timezonify/fork )
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create new Pull Request
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
@@ -0,0 +1,80 @@
1
+ require "timezonify/version"
2
+ require 'active_support'
3
+ require 'active_support/version'
4
+
5
+ if ActiveSupport::VERSION::MAJOR > 2
6
+ require 'active_support/core_ext/numeric'
7
+ end
8
+
9
+ module Timezonify
10
+ class Timezone
11
+
12
+ def self.timezone_for_time(time='Time.now.utc')
13
+ offset = find_offset_for_required_time(time.to_s, current_time="#{Time.now.utc.strftime("%I:%M %p UTC")}")
14
+ ((offset < 0) ? formatted_negative_offset(offset) : formatted_positive_offset(offset))
15
+ end
16
+
17
+ private
18
+
19
+ def self.find_offset_for_required_time(required_time='8:00 AM UTC', current_time="#{Time.now.utc.strftime("%I:%M %p UTC")}")
20
+ current_time_in_hours = TimeHelper.time_in_hours(Time.parse(required_time))
21
+ required_time_in_hours = TimeHelper.time_in_hours(Time.parse(current_time))
22
+ (current_time < required_time) ? offset_calculator(required_time_in_hours,current_time_in_hours) : offset_calculator(current_time_in_hours,required_time_in_hours)
23
+ end
24
+
25
+ def self.offset_calculator(time1,time2)
26
+ -(time1 - time2)
27
+ end
28
+
29
+ def self.formatted_positive_offset(offset_in_hours)
30
+ return 'GMT' if (offset_in_hours == 0)
31
+ offset_hours = hour_from_offset(offset_in_hours)
32
+ offset_minutes = minutes_from_offset(offset_in_hours)
33
+ timezone_as_string(offset_hours, offset_minutes, '+')
34
+ end
35
+
36
+ def self.formatted_negative_offset(negative_offset_in_hours)
37
+ offset_in_hours = negative_offset_in_hours.to_s.split('-').last.to_f
38
+ offset_hours = hour_from_offset(offset_in_hours)
39
+ offset_minutes = minutes_from_offset(offset_in_hours)
40
+ timezone_as_string(offset_hours, offset_minutes, '-')
41
+ end
42
+
43
+ def self.hour_from_offset(offset_in_hours)
44
+ offset_in_hours.to_s.split('.').first.to_i
45
+ end
46
+
47
+ def self.minutes_from_offset(offset_in_hours)
48
+ offset_in_hours.to_s.split('.').last.to_i
49
+ end
50
+
51
+ def self.timezone_as_string(offset_hours, offset_minutes, state)
52
+ if offset_minutes > 0
53
+ if offset_hours < 10
54
+ "GMT#{state}0" + "#{offset_hours}" + ":30"
55
+ else
56
+ "GMT#{state}" + "#{offset_hours}" + ":30"
57
+ end
58
+ else
59
+ if offset_hours < 10
60
+ "GMT#{state}0" + "#{offset_hours}"
61
+ else
62
+ "GMT#{state}" + "#{offset_hours}"
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ class TimeHelper
69
+ def self.time_in_hours(time)
70
+ (time.strftime("%H").to_f + (((time.strftime("%M")).to_f)/60).round(1))
71
+ end
72
+ end
73
+
74
+ class TimezoneHelper
75
+ def self.find_all_timezones_with_offset(offset_in_hours)
76
+ offset_in_seconds = offset_in_hours * 3600
77
+ ActiveSupport::TimeZone.all.select{|tz| tz.utc_offset == offset_in_seconds}
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module Timezonify
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+ require 'timezonify'
4
+
5
+ require 'coveralls'
6
+ Coveralls.wear!
7
+
8
+
9
+ # This file was generated by the `rspec --init` command. Conventionally, all
10
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
11
+ # Require this file using `require "spec_helper"` to ensure that it is only
12
+ # loaded once.
13
+ #
14
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
15
+ RSpec.configure do |config|
16
+ config.treat_symbols_as_metadata_keys_with_true_values = true
17
+ config.run_all_when_everything_filtered = true
18
+ # config.filter_run :focus
19
+
20
+ # Run specs in random order to surface order dependencies. If you find an
21
+ # order dependency and want to debug it, you can fix the order by providing
22
+ # the seed, which is printed after each run.
23
+ # --seed 1234
24
+ config.order = 'random'
25
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Find timezone where the time is" do
4
+
5
+ it "if time is given at GMT return current timezone should be GMT" do
6
+ offset = Timezonify::Timezone.timezone_for_time(Time.now.utc)
7
+ offset.should == 'GMT'
8
+ end
9
+
10
+ it "if time is given at IST return current timezone should be GMT+05:30" do
11
+ offset = Timezonify::Timezone.timezone_for_time(Time.now.utc+5.hours+30.minutes)
12
+ offset.should == 'GMT+05:30'
13
+ end
14
+
15
+ it "if time is given at EST return current timezone should be GMT-05:00" do
16
+ offset = Timezonify::Timezone.timezone_for_time(Time.now.utc-5.hours)
17
+ offset.should == 'GMT-05'
18
+ end
19
+
20
+ describe "TimeHelper" do
21
+ it "should return time in hours when time is in complete hour" do
22
+ time = Time.parse('8:00 AM')
23
+ Timezonify::TimeHelper.time_in_hours(time).should == 8.0
24
+ end
25
+ it "should return time in hours when time is with 30 mins past a complete hour" do
26
+ time = Time.parse('5:30 AM')
27
+ Timezonify::TimeHelper.time_in_hours(time).should == 5.5
28
+ end
29
+ end
30
+
31
+ describe "TimeZoneHelper" do
32
+ it "should return all timezones with the given offset" do
33
+ # IST
34
+ time_in_hours = 5.5
35
+ expected_results = ActiveSupport::TimeZone.all.select{|tz| tz.utc_offset == time_in_hours * 3600}
36
+ Timezonify::TimezoneHelper.find_all_timezones_with_offset(time_in_hours).should == expected_results
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'timezonify/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "timezonify"
8
+ spec.version = Timezonify::VERSION
9
+ spec.authors = ["Nikhil Nanjappa", "Ashish Upadhyay", "Ankur Gera", "Gourav Tiwari", "Hrishita Vaish"]
10
+ spec.email = ["kainikhil@gmail.com", "ashish.upadhyaye@gmail.com", "ankurgera@gmail.com", "gouravtiwari21@gmail.com", "vaish.hrishita@tcs.com"]
11
+ spec.summary = %q{Finds timezone for a specific time eg. Where is it 8 AM now ?}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.5"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "activesupport",'>= 2.3.16'
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'coveralls'
25
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: timezonify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nikhil Nanjappa
8
+ - Ashish Upadhyay
9
+ - Ankur Gera
10
+ - Gourav Tiwari
11
+ - Hrishita Vaish
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+ date: 2014-02-19 00:00:00.000000000 Z
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: bundler
19
+ requirement: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: '1.5'
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '1.5'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake
33
+ requirement: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ - !ruby/object:Gem::Dependency
46
+ name: activesupport
47
+ requirement: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - '>='
50
+ - !ruby/object:Gem::Version
51
+ version: 2.3.16
52
+ type: :development
53
+ prerelease: false
54
+ version_requirements: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - '>='
57
+ - !ruby/object:Gem::Version
58
+ version: 2.3.16
59
+ - !ruby/object:Gem::Dependency
60
+ name: rspec
61
+ requirement: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ - !ruby/object:Gem::Dependency
74
+ name: coveralls
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ type: :development
81
+ prerelease: false
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ description:
88
+ email:
89
+ - kainikhil@gmail.com
90
+ - ashish.upadhyaye@gmail.com
91
+ - ankurgera@gmail.com
92
+ - gouravtiwari21@gmail.com
93
+ - vaish.hrishita@tcs.com
94
+ executables: []
95
+ extensions: []
96
+ extra_rdoc_files: []
97
+ files:
98
+ - .coveralls.yml
99
+ - .gitignore
100
+ - .rspec
101
+ - .travis.yml
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - lib/timezonify.rb
107
+ - lib/timezonify/version.rb
108
+ - spec/spec_helper.rb
109
+ - spec/timezonify_spec.rb
110
+ - timezonify.gemspec
111
+ homepage: ''
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.0.6
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Finds timezone for a specific time eg. Where is it 8 AM now ?
135
+ test_files:
136
+ - spec/spec_helper.rb
137
+ - spec/timezonify_spec.rb