iso8601-basic 0.1.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: 5cedeab4786ea677611d4ae3438f7b879e5f840b
4
+ data.tar.gz: 470d1affea932d51f6353c19296d5de59192b81f
5
+ SHA512:
6
+ metadata.gz: c80808fc31b2a0fc58d989261f74765ad9de385667af0d8c73856ae4e65ec8eb5f34bd0ea9d67bc53668d7a7a2dda23e279aca8ba4003c08d7d2931004712eea
7
+ data.tar.gz: 16897a017aae22dc408195fa8ec9ea56f44889bb7cf77087a64d4ca3e562d5ffe8e58dca09b9bdbb7bcb1b67bcd027ba237541c6886d4a2c326923df45730c58
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ # Gemfile.lock
30
+ # .ruby-version
31
+ # .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 TicketingHub
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 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,
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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ iso8601-basic
2
+ =============
3
+
4
+ A very basic implementation of the ISO8601 spec
5
+
6
+ ```ruby
7
+ gem 'iso8601-basic', require 'iso8601'
8
+ ```
9
+
10
+ ## Joining date/time parts:
11
+
12
+ ```ruby
13
+ date = ISO8601::Date.new '2010-01-01' # => #<ISO8601::Date: 2010-01-01>
14
+ time = ISO8601::Time.new '10:30' # => #<ISO8601::Time: T10:30:00+00:00>
15
+
16
+ date + time # => #<ISO8601::DateTime: 2010-01-01T10:30:00+00:00>
17
+ ```
18
+
19
+ ## Working with durations:
20
+
21
+ When working with times:
22
+
23
+ ```ruby
24
+ duration = ISO8601::Duration.new 'PT1H' # => #<ISO8601::Duration: PT1H>
25
+ time = ISO8601::Time.new '10:30' # => #<ISO8601::Time: T10:30:00+00:00>
26
+
27
+ time + duration # => #<ISO8601::Time: T11:30:00+00:00>
28
+ ```
29
+
30
+ And with dates:
31
+
32
+ ```ruby
33
+ duration = ISO8601::Duration.new 'P1D' # => #<ISO8601::Duration: P1D>
34
+ date = ISO8601::Date.new '2010-01-01' # => #<ISO8601::Date: 2010-01-01>
35
+
36
+ date + duration # => #<ISO8601::Time: 2010-01-02>
37
+ ```
38
+
39
+ All together now:
40
+
41
+ ```ruby
42
+ duration = ISO8601::Duration.new 'P1DT1H' # => #<ISO8601::Duration: P1DT1H>
43
+ date_time = ISO8601::DateTime.new '2010-01-01T10:30' # => #<ISO8601::DateTime: 2010-01-01T10:30:00+00:00>
44
+
45
+ date_time + duration # => #<ISO8601::DateTime: 2010-01-02T11:30:00+00:00>
46
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
@@ -0,0 +1,16 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+ require 'iso8601/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'iso8601-basic'
6
+ s.version = ISO8601::VERSION
7
+ s.date = Time.now.strftime('%Y-%m-%d')
8
+ s.authors = ['Oliver Morgan']
9
+ s.email = 'olly@ticketinghub.com'
10
+ s.homepage = 'https://github.com/ticketinghub/iso8601-basic'
11
+ s.description = s.summary = "Very basic implementation of the ISO8601 spec - http://en.wikipedia.org/wiki/ISO_8601"
12
+ s.license = 'MIT'
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.require_paths = ['lib']
16
+ end
data/lib/iso8601.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'date'
2
+
3
+ require_relative 'iso8601/version'
4
+ require_relative 'iso8601/date'
5
+ require_relative 'iso8601/time'
6
+ require_relative 'iso8601/date_time'
7
+ require_relative 'iso8601/duration'
@@ -0,0 +1,27 @@
1
+ class ISO8601::Date < ::Date
2
+
3
+ def self.new(*args)
4
+ case value = args[0]
5
+ when Numeric then super
6
+ when String
7
+ dt = ::Date.iso8601 value
8
+ super dt.year, dt.month, dt.day
9
+ else super end
10
+ end
11
+
12
+ def +(other)
13
+ case other
14
+ when ISO8601::Time
15
+ ISO8601::DateTime.new year, month, day,
16
+ other.hour, other.minute, other.second, other.zone
17
+ when ISO8601::Duration
18
+ { years: [:>>, 12], months: [:>>, 1], weeks: [:+, 7], days: [:+, 1] }.reduce self do |date, (key, (action, factor))|
19
+ date.send action, other.to_h[key] * factor
20
+ end
21
+ else super end
22
+ end
23
+
24
+ def inspect
25
+ "#<ISO8601::Date: #{iso8601}>"
26
+ end
27
+ end
@@ -0,0 +1,33 @@
1
+ class ISO8601::DateTime < ::DateTime
2
+
3
+ def self.new(*args)
4
+ case value = args[0]
5
+ when Numeric then super
6
+ when String
7
+ dt = ::DateTime.iso8601 value
8
+ super dt.year, dt.month, dt.day,
9
+ dt.hour, dt.minute, dt.second, dt.zone
10
+ else super end
11
+ end
12
+
13
+ def to_iso8601_date
14
+ ISO8601::Date.new year, month, day
15
+ end
16
+
17
+ def to_iso8601_time
18
+ ISO8601::Time.new hour, minute, second, zone
19
+ end
20
+
21
+ def +(other)
22
+ case other
23
+ when ISO8601::Duration
24
+ date = to_iso8601_date + other
25
+ time = to_iso8601_time + other
26
+ date + time
27
+ else super end
28
+ end
29
+
30
+ def inspect
31
+ "#<ISO8601::DateTime: #{iso8601}>"
32
+ end
33
+ end
@@ -0,0 +1,119 @@
1
+ class ISO8601::Duration
2
+
3
+ attr_reader :years, :months, :weeks, :days, :hours, :minutes, :seconds, :sign
4
+
5
+ def initialize(value)
6
+ match = /^(\+|-)? # Sign
7
+ P(
8
+ (
9
+ (\d+(?:[,.]\d+)?Y)? # Years
10
+ (\d+(?:[.,]\d+)?M)? # Months
11
+ (\d+(?:[.,]\d+)?D)? # Days
12
+ (T
13
+ (\d+(?:[.,]\d+)?H)? # Hours
14
+ (\d+(?:[.,]\d+)?M)? # Minutes
15
+ (\d+(?:[.,]\d+)?S)? # Seconds
16
+ )? # Time
17
+ )
18
+ |(\d+(?:[.,]\d+)?W) # Weeks
19
+ ) # Duration
20
+ $/x.match(value)
21
+
22
+ raise ArgumentError, 'invalid duration' unless match
23
+
24
+ @sign = (match[1] == '-' ? -1 : 1)
25
+ { years: 4, months: 5, weeks: 11, days: 6, hours: 8, minutes: 9, seconds: 10 }.each do |key, index|
26
+ instance_variable_set "@#{key}", match[index] ? match[index].chop.to_f * @sign : 0
27
+ end
28
+ end
29
+
30
+ def present?
31
+ ! blank?
32
+ end
33
+
34
+ def blank?
35
+ empty?
36
+ end
37
+
38
+ def empty?
39
+ to_h.all? { |key, value| value.zero? }
40
+ end
41
+
42
+ def iso8601
43
+ return nil if blank?
44
+
45
+ day_parts = [:years, :months, :weeks, :days].collect do |key|
46
+ value = to_h[key].divmod(1)[1].zero?? to_h[key].to_i : to_h[key]
47
+ "#{value}#{key.to_s[0].upcase}" unless value.zero?
48
+ end.compact
49
+
50
+ time_parts = [:hours, :minutes, :seconds].collect do |key|
51
+ value = to_h[key].divmod(1)[1].zero?? to_h[key].to_i : to_h[key]
52
+ "#{value}#{key.to_s[0].upcase}" unless value.zero?
53
+ end.compact
54
+
55
+ "P#{day_parts.join}#{time_parts.any?? "T#{time_parts.join}" : ''}"
56
+ end
57
+
58
+ def eql?(other)
59
+ hash == other.hash
60
+ end
61
+
62
+ def ==(other)
63
+ eql? other
64
+ end
65
+
66
+ def hash
67
+ [self.class, to_h.to_a].hash
68
+ end
69
+
70
+ def inspect
71
+ "#<#{self.class.name}: #{iso8601}>"
72
+ end
73
+
74
+ def to_s
75
+ iso8601
76
+ end
77
+
78
+ def format
79
+ singular = {
80
+ years: 'year',
81
+ months: 'month',
82
+ weeks: 'week',
83
+ days: 'day',
84
+ hours: 'hour',
85
+ minutes: 'minute',
86
+ seconds: 'second' }
87
+
88
+ to_h.collect do |key, value|
89
+ value = value.divmod(1)[1].zero?? value.to_i : value
90
+ value.zero?? nil : key == 1.0 ? "#{value} #{singular[key]}" : "#{value} #{key}"
91
+ end.compact.join(', ')
92
+ end
93
+
94
+ def to_f
95
+ { years: 31536000,
96
+ months: 2628000,
97
+ weeks: 604800,
98
+ days: 86400,
99
+ hours: 3600,
100
+ minutes: 60,
101
+ secodns: 1 }.collect do |key, value|
102
+ to_h[key] * value
103
+ end.inject :+
104
+ end
105
+
106
+ def to_i
107
+ to_f.to_i
108
+ end
109
+
110
+ def to_h
111
+ { years: @years,
112
+ months: @months,
113
+ weeks: @weeks,
114
+ days: @days,
115
+ hours: @hours,
116
+ minutes: @minutes,
117
+ seconds: @seconds }
118
+ end
119
+ end
@@ -0,0 +1,38 @@
1
+ class ISO8601::Time < ::DateTime
2
+
3
+ def self.new(*args)
4
+ case value = args[0]
5
+ when Numeric
6
+ super 2000, 1, 1, *args
7
+ when String
8
+ value = "T#{value}" unless value[0] == 'T'
9
+ dt = ::DateTime.iso8601 "2000-01-01#{value}"
10
+ super 2000, 1, 1, dt.hour, dt.minute, dt.second, dt.zone
11
+ else super end
12
+ end
13
+
14
+ def +(other)
15
+ case other
16
+ when ISO8601::Date
17
+ ISO8601::DateTime.new other.year, other.month, other.day,
18
+ hour, minute, second, zone
19
+ when ISO8601::Duration
20
+ seconds = [:hours, :minutes, :seconds].collect do |key|
21
+ { seconds: 1, minutes: 60, hours: 3600 }[key] * other.to_h[key]
22
+ end.inject :+
23
+ super Rational(seconds.round, 86400)
24
+ else super end
25
+ end
26
+
27
+ def iso8601
28
+ super[10..-1]
29
+ end
30
+
31
+ def inspect
32
+ "#<ISO8601::Time: #{iso8601}>"
33
+ end
34
+
35
+ def to_s
36
+ iso8601
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module ISO8601
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iso8601-basic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Oliver Morgan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Very basic implementation of the ISO8601 spec - http://en.wikipedia.org/wiki/ISO_8601
14
+ email: olly@ticketinghub.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".gitignore"
20
+ - Gemfile
21
+ - LICENSE
22
+ - README.md
23
+ - Rakefile
24
+ - iso8601-basic.gemspec
25
+ - lib/iso8601.rb
26
+ - lib/iso8601/date.rb
27
+ - lib/iso8601/date_time.rb
28
+ - lib/iso8601/duration.rb
29
+ - lib/iso8601/time.rb
30
+ - lib/iso8601/version.rb
31
+ homepage: https://github.com/ticketinghub/iso8601-basic
32
+ licenses:
33
+ - MIT
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 2.2.1
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Very basic implementation of the ISO8601 spec - http://en.wikipedia.org/wiki/ISO_8601
55
+ test_files: []
56
+ has_rdoc: