date_utc_parser 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/.gitignore +4 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/README.md +53 -0
- data/Rakefile +12 -0
- data/date_utc_parser.gemspec +19 -0
- data/lib/date_utc_parser/version.rb +3 -0
- data/lib/date_utc_parser.rb +32 -0
- data/spec/lib/date_utc_parser_spec.rb +65 -0
- metadata +89 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Tired of Dating?
|
2
|
+
|
3
|
+
Date UTC Parser was written so that I could guarantee to either get back a UTC Date Time object, or nil - while writing some API's for mobile apps. That's all I wanted, and that's what this guy does.
|
4
|
+
|
5
|
+
Along with that, it will return the humanized UTC example string, and it contains the regex that will match that string, can be used for error handling.
|
6
|
+
|
7
|
+
# Using It
|
8
|
+
|
9
|
+
Install it as a gem
|
10
|
+
|
11
|
+
```bash
|
12
|
+
gem install date_utc_parser
|
13
|
+
```
|
14
|
+
|
15
|
+
Require it in your app
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
require 'date_utc_parser'
|
19
|
+
```
|
20
|
+
|
21
|
+
Start using it.
|
22
|
+
|
23
|
+
# Some Examples
|
24
|
+
|
25
|
+
## Time Object to UTC Time Object
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
unparsed_time = Time.now
|
29
|
+
unparsed_time.utc? #=> false
|
30
|
+
parsed_time = DateUtcParser.parse unparsed_time # => 2012-07-07 18:59:02 UTC
|
31
|
+
parsed_time.utc? #=> true
|
32
|
+
```
|
33
|
+
|
34
|
+
## UTC Time String to UTC Time Object
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
unparsed_time = "2012-07-08T12:13:14Z"
|
38
|
+
parsed_time = DateUtcParser.parse unparsed_time # => 2012-07-08 12:13:14 UTC
|
39
|
+
parsed_time.utc? #=> true
|
40
|
+
```
|
41
|
+
|
42
|
+
## Everything else
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
unparsed_time = "white russian"
|
46
|
+
parsed_time = DateUtcParser.parse unparsed_time # => nil
|
47
|
+
```
|
48
|
+
|
49
|
+
# Contribute
|
50
|
+
|
51
|
+
Fork it, branch your topic, test it, make a pull request.
|
52
|
+
|
53
|
+
I suggest you bounce your idea off of me first so you don't make wasted effort.
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "date_utc_parser/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "date_utc_parser"
|
7
|
+
s.version = DateUtcParser::VERSION
|
8
|
+
s.authors = ["Matt Simpson"]
|
9
|
+
s.email = ["matt.simpson3@gmail.com"]
|
10
|
+
s.homepage = "http://github.com/ionicmobile/date_utc_parser"
|
11
|
+
s.summary = %q{Make working with UTC Time more fun}
|
12
|
+
s.description = %q{Takes an object and returns the equivalent UTC Time object}
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
s.required_ruby_version = "~> 1.9.2"
|
17
|
+
|
18
|
+
s.add_development_dependency "rspec", "2.10.0"
|
19
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "date_utc_parser/version"
|
2
|
+
require 'time'
|
3
|
+
|
4
|
+
module DateUtcParser
|
5
|
+
def self.parse(date)
|
6
|
+
date.is_a?(Time) ? parse_from_time(date) : parse_from_string(date)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.parse_from_string(date)
|
10
|
+
Time.parse(date) if date && date =~ iso8601_pattern
|
11
|
+
rescue ArgumentError
|
12
|
+
# date could not be parsed
|
13
|
+
end
|
14
|
+
|
15
|
+
# http://www.w3.org/TR/NOTE-datetime
|
16
|
+
# http://tools.ietf.org/html/rfc3339
|
17
|
+
#
|
18
|
+
# example: 1994-11-05T13:15:30Z
|
19
|
+
#
|
20
|
+
def self.iso8601_pattern
|
21
|
+
/\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z\Z/
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.iso8601_humanized_pattern
|
25
|
+
"YYYY-MM-DDTmm:dd:ssZ"
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.parse_from_time(date)
|
29
|
+
date.utc? ? date : date.utc
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require_relative '../../lib/date_utc_parser'
|
2
|
+
require 'time'
|
3
|
+
|
4
|
+
describe DateUtcParser, "Parsing:" do
|
5
|
+
subject { described_class.parse(date) }
|
6
|
+
|
7
|
+
context "when there is no date given" do
|
8
|
+
let(:date) { nil }
|
9
|
+
|
10
|
+
it 'returns nil' do
|
11
|
+
subject.should be_nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "when the date is a string, is a valid date time format, but is not utc" do
|
16
|
+
let(:date) { "2012-05-14 10:49:38 -0500" }
|
17
|
+
|
18
|
+
it 'returns nil' do
|
19
|
+
subject.should be_nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "when the date is a string, is a valid format, but is not a valid date" do
|
24
|
+
let(:date) { "2012-50-12T20:46:51Z" }
|
25
|
+
|
26
|
+
it 'returns nil' do
|
27
|
+
subject.should be_nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "when the date is a string, but is not a valid date time format" do
|
32
|
+
let(:date) { "asdf" }
|
33
|
+
|
34
|
+
it 'returns nil' do
|
35
|
+
subject.should be_nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "when the date is a valid date time format, and is a valid date, and is utc" do
|
40
|
+
let(:date) { "2012-05-12T20:46:51Z" }
|
41
|
+
|
42
|
+
it 'returns the parsed date' do
|
43
|
+
subject.should == Time.parse(date)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "when the date is already a Time object" do
|
48
|
+
context 'and is a utc date' do
|
49
|
+
let(:date) { Time.now.utc }
|
50
|
+
|
51
|
+
it "returns the time object" do
|
52
|
+
subject.should == date
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'but is not a utc date' do
|
57
|
+
let(:date) { Time.now }
|
58
|
+
|
59
|
+
it "returns the time object as utc" do
|
60
|
+
subject.utc?.should be_true
|
61
|
+
subject.should == date.utc
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: date_utc_parser
|
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
|
+
- Matt Simpson
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2012-07-07 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - "="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 10
|
31
|
+
- 0
|
32
|
+
version: 2.10.0
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Takes an object and returns the equivalent UTC Time object
|
36
|
+
email:
|
37
|
+
- matt.simpson3@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
files:
|
45
|
+
- .gitignore
|
46
|
+
- .rspec
|
47
|
+
- Gemfile
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- date_utc_parser.gemspec
|
51
|
+
- lib/date_utc_parser.rb
|
52
|
+
- lib/date_utc_parser/version.rb
|
53
|
+
- spec/lib/date_utc_parser_spec.rb
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: http://github.com/ionicmobile/date_utc_parser
|
56
|
+
licenses: []
|
57
|
+
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
segments:
|
69
|
+
- 1
|
70
|
+
- 9
|
71
|
+
- 2
|
72
|
+
version: 1.9.2
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.3.7
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Make working with UTC Time more fun
|
88
|
+
test_files: []
|
89
|
+
|