time_parse_to_utc 0.0.1

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.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2010 Adam Cigánek
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,15 @@
1
+ = Time.parse_to_utc
2
+
3
+ Adds a class method to the Time class to parse a string and return it as a UTC time.
4
+
5
+ == Usage
6
+
7
+ # If the input string contains not time offset, assume it's in UTC already.
8
+ Time.parse_to_utc("2010-09-08 12:35:00") # Time.utc(2010, 9, 8, 12, 35)
9
+
10
+ # If it contains one, convert it to UTC.
11
+ TIme.parse_to_utc("2010-09-08 12:35:00 +0300") # Time.utc(2010, 9, 8, 9, 35)
12
+
13
+ == Legal
14
+
15
+ Copyright (c) 2010 Adam Cigánek. Released under the terms of the MIT license.
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ require 'rake/testtask'
3
+
4
+ task :default => :test
5
+
6
+ Rake::TestTask.new(:test) do |task|
7
+ task.test_files = FileList['test/**/*_test.rb']
8
+ task.verbose = true
9
+ end
10
+
11
+ begin
12
+ require 'jeweler'
13
+
14
+ Jeweler::Tasks.new do |gemspec|
15
+ gemspec.name = 'time_parse_to_utc'
16
+ gemspec.summary = 'Method to parse string into an UTC Time object'
17
+ gemspec.description = 'This gem extends the Time class with parse_to_utc method, which parses a string and returns a Time converted to UTC.'
18
+
19
+ gemspec.email = 'adam@3scale.net'
20
+ gemspec.homepage = 'http://www.3scale.net'
21
+ gemspec.authors = ['Adam Cigánek']
22
+ end
23
+
24
+ Jeweler::GemcutterTasks.new
25
+ rescue LoadError
26
+ puts "Jeweler not available. Install it with: gem install jeweler"
27
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,21 @@
1
+ require 'date'
2
+
3
+ module TimeParseToUtc
4
+ def parse_to_utc(input)
5
+ parts = Date._parse(input)
6
+ return if parts.nil? || parts.empty?
7
+
8
+ time = Time.utc(parts[:year],
9
+ parts[:mon],
10
+ parts[:mday],
11
+ parts[:hour],
12
+ parts[:min],
13
+ parts[:sec],
14
+ parts[:sec_fraction])
15
+
16
+ time -= parts[:offset] if parts[:offset]
17
+ time
18
+ end
19
+ end
20
+
21
+ Time.extend(TimeParseToUtc)
@@ -0,0 +1,16 @@
1
+ require 'test/unit'
2
+ require 'time_parse_to_utc'
3
+
4
+ class TimeParseToUtcTest < Test::Unit::TestCase
5
+ def test_parse_to_utc_with_input_without_offset
6
+ assert_equal Time.utc(2010, 9, 8, 14, 17, 12), Time.parse_to_utc('2010-09-08 14:17:12')
7
+ end
8
+
9
+ def test_parse_to_utc_with_input_with_offset
10
+ assert_equal Time.utc(2010, 9, 8, 10, 17, 12), Time.parse_to_utc('2010-09-08 14:17:12 +0400')
11
+ end
12
+
13
+ def test_parse_to_utc_returns_nil_on_invalid_input
14
+ assert_nil Time.parse_to_utc('choke on this!')
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ require 'test/unit'
2
+
3
+ class TimeParseToUtcTest < Test::Unit::TestCase
4
+ def test_parse_to_utc_with_input_without_offset
5
+ assert_equal Time.utc(2010, 9, 8, 14, 17, 12), Time.parse_to_utc('2010-09-08 14:17:12')
6
+ end
7
+
8
+ def test_parse_to_utc_with_input_with_offset
9
+ assert_equal Time.utc(2010, 9, 8, 10, 17, 12), Time.parse_to_utc('2010-09-08 14:17:12 +0400')
10
+ end
11
+
12
+ def test_parse_to_utc_returns_nil_on_invalid_input
13
+ assert_nil Time.parse_to_utc('choke on this!')
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time_parse_to_utc
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - "Adam Cig\xC3\xA1nek"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-08 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: This gem extends the Time class with parse_to_utc method, which parses a string and returns a Time converted to UTC.
22
+ email: adam@3scale.net
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.rdoc
30
+ files:
31
+ - LICENSE
32
+ - README.rdoc
33
+ - Rakefile
34
+ - VERSION
35
+ - lib/time_parse_to_utc.rb
36
+ - test/parse_to_utc_test.rb
37
+ - test/time_parse_to_utc_test.rb
38
+ has_rdoc: true
39
+ homepage: http://www.3scale.net
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.6
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Method to parse string into an UTC Time object
68
+ test_files:
69
+ - test/parse_to_utc_test.rb
70
+ - test/time_parse_to_utc_test.rb