date_time_with_zone 0.0.1

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: 8ac3adfc5a0c428d08f29c594b13a47a8b4da4df
4
+ data.tar.gz: 86dcd5251c1f7e7733d8fcfcfb571c9546637381
5
+ SHA512:
6
+ metadata.gz: 2e948c19423475cc11e94a558b463f49b7cd1facdabe2e07d38f082dcc4085a5eccb5ba263a0325c105a0acff8ae5d97877f242c499f4854741fa34f05f452a8
7
+ data.tar.gz: 030c360100380925b2d17ad69bfa457afbaf2344f33ad727889ad52dc29e869c5e48714820d4dceeed19dd4b5347fbe45837126d4d5c145d9e7dc4bc02e8e285
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in date_time_with_zone.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Lin He
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,36 @@
1
+ # DateTimeWithZone
2
+
3
+ Helps with set time with a zone.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'date_time_with_zone'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install date_time_with_zone
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ input = {date: "2014-03-14", time: "05:15 PM", zone: "Beijing"}
23
+ datetime = DateTimeWithZone::DateTime.new(input).to_datetime
24
+ p datetime.zone
25
+ # => CST
26
+ p datetime.strftime('%Y-%m-%d')
27
+ # => 2014-03-14
28
+ ```
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it ( https://github.com/he9lin/date_time_with_zone/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 a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/rspec ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rspec' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rspec-core', 'rspec')
@@ -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 'date_time_with_zone/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "date_time_with_zone"
8
+ spec.version = DateTimeWithZone::VERSION
9
+ spec.authors = ["Lin He"]
10
+ spec.email = ["he9lin@gmail.com"]
11
+ spec.summary = %q{Help deal with datetime with zones.}
12
+ spec.description = %q{Help deal with datetime with zones.}
13
+ spec.homepage = "https://github.com/he9lin/date_time_with_zone"
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_dependency "activesupport", ">= 3.1.2"
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec", "~> 3.1.0"
25
+ end
@@ -0,0 +1,25 @@
1
+ require "date_time_with_zone/version"
2
+
3
+ module DateTimeWithZone
4
+ def self.default_zone
5
+ @default_zone ||= "Pacific Time (US & Canada)".freeze
6
+ end
7
+
8
+ # A custom default zone can be set by add the following in
9
+ # an initializer file. This method takes a string which
10
+ # is the name of that zone.
11
+ #
12
+ # DateTimeWithZone.default_zone = "Alaska"
13
+ #
14
+ def self.default_zone=(zone)
15
+ validate_time_zone! zone
16
+ @default_zone = zone
17
+ end
18
+
19
+ def self.validate_time_zone!(zone)
20
+ raise ArgumentError, "time zone is invalid. #{zone.inspect}" \
21
+ unless ActiveSupport::TimeZone[zone]
22
+ end
23
+ end
24
+
25
+ require "date_time_with_zone/date_time"
@@ -0,0 +1,44 @@
1
+ require 'active_support/time'
2
+
3
+ module DateTimeWithZone
4
+ class DateTime
5
+ DATE_FORMATTER = "%Y-%m-%d"
6
+ TIME_FORMATTER = "%I:%M %p"
7
+
8
+ def initialize(*args)
9
+ time_or_hash = args.shift
10
+ case time_or_hash
11
+ when Hash
12
+ initialize_with_hash(time_or_hash)
13
+ when Time
14
+ with_timezone = args.pop
15
+ initialize_with_time(time_or_hash, with_timezone)
16
+ else
17
+ raise ArgumentError, "only accepts a Hash or Time object, got #{time_or_hash}"
18
+ end
19
+ end
20
+
21
+ def to_datetime
22
+ Time.use_zone(@zone) { Time.zone.parse [@date, @time].join(" ") }
23
+ end
24
+
25
+ attr_accessor :date, :time, :zone
26
+
27
+ private
28
+
29
+ def initialize_with_hash(hash)
30
+ @date = hash.fetch(:date) { raise ArgumentError, "date is missing" }
31
+ @time = hash.fetch(:time) { raise ArgumentError, "time is missing" }
32
+ @zone = hash.fetch(:zone, DateTimeWithZone.default_zone)
33
+ DateTimeWithZone.validate_time_zone!(@zone)
34
+ @zone = ActiveSupport::TimeZone[zone]
35
+ end
36
+
37
+ def initialize_with_time(time, zone)
38
+ time_in_zone = time.in_time_zone(zone)
39
+ @date = time_in_zone.strftime(DATE_FORMATTER)
40
+ @time = time_in_zone.strftime(TIME_FORMATTER)
41
+ @zone = ActiveSupport::TimeZone[zone]
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module DateTimeWithZone
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe DateTimeWithZone::DateTime do
4
+ it 'sets the correct datetime object with specified hash' do
5
+ input = {date: "2014-03-14", time: "05:15 PM", zone: "Beijing"}
6
+ datetime = described_class.new(input).to_datetime
7
+ expect(datetime.zone).to eq('CST')
8
+ expect(datetime.strftime(described_class::DATE_FORMATTER)).to eq('2014-03-14')
9
+ expect(datetime.strftime(described_class::TIME_FORMATTER)).to eq('05:15 PM')
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe DateTimeWithZone do
4
+ it 'has a default zone' do
5
+ expect(described_class.default_zone).to eq("Pacific Time (US & Canada)")
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'date_time_with_zone'
5
+
6
+ RSpec.configure do |config|
7
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: date_time_with_zone
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Lin He
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.1.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.1.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.1.0
69
+ description: Help deal with datetime with zones.
70
+ email:
71
+ - he9lin@gmail.com
72
+ executables:
73
+ - rspec
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/rspec
84
+ - date_time_with_zone.gemspec
85
+ - lib/date_time_with_zone.rb
86
+ - lib/date_time_with_zone/date_time.rb
87
+ - lib/date_time_with_zone/version.rb
88
+ - spec/date_time_spec.rb
89
+ - spec/date_time_with_zone_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: https://github.com/he9lin/date_time_with_zone
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.0.14
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Help deal with datetime with zones.
115
+ test_files:
116
+ - spec/date_time_spec.rb
117
+ - spec/date_time_with_zone_spec.rb
118
+ - spec/spec_helper.rb
119
+ has_rdoc: