time_crisis 0.1.4
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/.document +5 -0
- data/.gitignore +7 -0
- data/LICENSE +20 -0
- data/README.markdown +60 -0
- data/Rakefile +82 -0
- data/VERSION.yml +4 -0
- data/lib/time_crisis/date_range.rb +110 -0
- data/lib/time_crisis/meteorological_seasons.rb +72 -0
- data/lib/time_crisis/named_months.rb +28 -0
- data/lib/time_crisis/support/acts_like.rb +20 -0
- data/lib/time_crisis/support/advance.rb +25 -0
- data/lib/time_crisis/support/change.rb +47 -0
- data/lib/time_crisis/support/conversions.rb +89 -0
- data/lib/time_crisis/support/current.rb +16 -0
- data/lib/time_crisis/support/readable_inspect.rb +30 -0
- data/lib/time_crisis/support.rb +10 -0
- data/lib/time_crisis.rb +13 -0
- data/test/test_helper.rb +10 -0
- data/test/time_crisis_test.rb +7 -0
- data/time_crisis.gemspec +66 -0
- metadata +95 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Travis Tilley
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
Arbitrary Date Ranges
|
2
|
+
=====================
|
3
|
+
|
4
|
+
You can create a Range with a beginning and end Date using the convenience methods 'for' and 'from'. 'for' will create a range into the future while 'from' creates a range into the past.
|
5
|
+
|
6
|
+
TimeCrisis::Date.today.for(2, 'years')
|
7
|
+
=> #<TimeCrisis::Date 2009-09-22>..#<TimeCrisis::Date 2011-09-21>
|
8
|
+
|
9
|
+
TimeCrisis::Date.today.from(2, 'years')
|
10
|
+
=> #<TimeCrisis::Date 2007-09-23>..#<TimeCrisis::Date 2009-09-22>
|
11
|
+
|
12
|
+
If you want to be more literal in your syntax, you can use the range method directly:
|
13
|
+
|
14
|
+
TimeCrisis::Date.today.range(2, 'months', 'future')
|
15
|
+
=> #<TimeCrisis::Date 2009-09-22>..#<TimeCrisis::Date 2009-11-21>
|
16
|
+
|
17
|
+
TimeCrisis::Date.today.range(2, 'months', 'past')
|
18
|
+
=> #<TimeCrisis::Date 2009-07-23>..#<TimeCrisis::Date 2009-09-22>
|
19
|
+
|
20
|
+
There is also a class method for defining ranges that allows you to specify a beginning and end for the range, or just one of beginning or end and then the scale (months, years) and unit (number of scale).
|
21
|
+
|
22
|
+
TimeCrisis::Date.range(:begin => Date.today, :scale => 'months', :unit => 3)
|
23
|
+
=> #<TimeCrisis::Date 2009-09-22>..#<TimeCrisis::Date 2009-12-21>
|
24
|
+
|
25
|
+
TimeCrisis::Date.range(:begin => Date.civil(2008, 3, 1), :end => Date.civil(2008, 12, 31))
|
26
|
+
=> #<TimeCrisis::Date 2008-03-01>..#<TimeCrisis::Date 2008-12-31>
|
27
|
+
|
28
|
+
Named Months
|
29
|
+
============
|
30
|
+
|
31
|
+
A range of dates for a month can be created using that month's name, optionally passing in a year.
|
32
|
+
|
33
|
+
TimeCrisis::Date.february # 28 days in feb 2009
|
34
|
+
=> #<TimeCrisis::Date 2009-02-01>..#<TimeCrisis::Date 2009-02-28>
|
35
|
+
|
36
|
+
TimeCrisis::Date.february(2008) # 29 days in feb 2008
|
37
|
+
=> #<TimeCrisis::Date 2008-02-01>..#<TimeCrisis::Date 2008-02-29>
|
38
|
+
|
39
|
+
|
40
|
+
Meteorological Season Calculations
|
41
|
+
==================================
|
42
|
+
|
43
|
+
TimeCrisis::Date.beginning_of_meteorological_spring
|
44
|
+
=> #<TimeCrisis::Date 2009-03-01>
|
45
|
+
|
46
|
+
TimeCrisis::Date.beginning_of_meteorological_spring(2008)
|
47
|
+
=> #<TimeCrisis::Date 2008-03-01>
|
48
|
+
|
49
|
+
TimeCrisis::Date.civil(2009, 4, 1).meteorological_spring?
|
50
|
+
=> true
|
51
|
+
|
52
|
+
TimeCrisis::Date.civil(2009, 4, 1).meteorological_winter?
|
53
|
+
=> false
|
54
|
+
|
55
|
+
TimeCrisis::Date.civil(2009, 10, 1).meteorological_season
|
56
|
+
=> "fall"
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
Copyright (c) 2009 Travis Tilley. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "time_crisis"
|
8
|
+
gem.summary = %Q{date and time related extensions}
|
9
|
+
gem.description = %Q{date and time related extensions}
|
10
|
+
gem.email = "ttilley@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/ttilley/time_crisis"
|
12
|
+
gem.authors = ["Travis Tilley"]
|
13
|
+
gem.add_dependency 'third_base'
|
14
|
+
gem.add_development_dependency "thoughtbot-shoulda"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/*_test.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/*_test.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
begin
|
44
|
+
require 'reek/rake_task'
|
45
|
+
Reek::RakeTask.new do |t|
|
46
|
+
t.fail_on_error = true
|
47
|
+
t.verbose = false
|
48
|
+
t.source_files = 'lib/**/*.rb'
|
49
|
+
end
|
50
|
+
rescue LoadError
|
51
|
+
task :reek do
|
52
|
+
abort "Reek is not available. In order to run reek, you must: sudo gem install reek"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
begin
|
57
|
+
require 'roodi'
|
58
|
+
require 'roodi_task'
|
59
|
+
RoodiTask.new do |t|
|
60
|
+
t.verbose = false
|
61
|
+
end
|
62
|
+
rescue LoadError
|
63
|
+
task :roodi do
|
64
|
+
abort "Roodi is not available. In order to run roodi, you must: sudo gem install roodi"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
task :default => :test
|
69
|
+
|
70
|
+
require 'rake/rdoctask'
|
71
|
+
Rake::RDocTask.new do |rdoc|
|
72
|
+
if File.exist?('VERSION')
|
73
|
+
version = File.read('VERSION')
|
74
|
+
else
|
75
|
+
version = ""
|
76
|
+
end
|
77
|
+
|
78
|
+
rdoc.rdoc_dir = 'rdoc'
|
79
|
+
rdoc.title = "time_crisis #{version}"
|
80
|
+
rdoc.rdoc_files.include('README*')
|
81
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
82
|
+
end
|
data/VERSION.yml
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
class TimeCrisis::DateRange < Range
|
2
|
+
def initialize(*args)
|
3
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
4
|
+
|
5
|
+
# make compatibile with Range's normal arguments
|
6
|
+
options[:begin] = args.shift unless args.empty?
|
7
|
+
options[:end] = args.shift unless args.empty?
|
8
|
+
options[:exclude_end] = args.shift unless args.empty?
|
9
|
+
|
10
|
+
start = options[:begin].nil? ? false : options[:begin].to_tc_date
|
11
|
+
stop = options[:end].nil? ? false : options[:end].to_tc_date
|
12
|
+
|
13
|
+
unit = options[:unit] || 1
|
14
|
+
scale = options[:scale] || 'years'
|
15
|
+
scale = scale.pluralize if scale.respond_to?(:pluralize)
|
16
|
+
|
17
|
+
if start && !stop
|
18
|
+
stop = start.advance({scale.to_sym => unit, :days => -1})
|
19
|
+
elsif !start && stop
|
20
|
+
start = stop.advance({scale.to_sym => -unit, :days => 1})
|
21
|
+
end
|
22
|
+
|
23
|
+
super(start, stop, options[:exclude_end] || false)
|
24
|
+
end
|
25
|
+
|
26
|
+
def include?(datelike)
|
27
|
+
super(datelike.to_tc_date)
|
28
|
+
end
|
29
|
+
|
30
|
+
def each_slice_by_date(*args, &block)
|
31
|
+
dates = args.map! do |datelike|
|
32
|
+
date = datelike.to_tc_date
|
33
|
+
raise ArgumentError, "Date not within range: #{date}" unless self.include?(date)
|
34
|
+
date
|
35
|
+
end
|
36
|
+
dates.sort!
|
37
|
+
|
38
|
+
start = self.begin
|
39
|
+
|
40
|
+
dates.each do |date|
|
41
|
+
yield self.class.new(start, date, true, self.subopt)
|
42
|
+
start = date
|
43
|
+
end
|
44
|
+
|
45
|
+
yield self.class.new(start, self.end, self.exclude_end?, self.subopt)
|
46
|
+
end
|
47
|
+
|
48
|
+
def each_slice_by_period(period, unit=1, &block)
|
49
|
+
start = self.begin
|
50
|
+
nstart = start.advance(period.to_sym => unit)
|
51
|
+
|
52
|
+
raise ArgumentError, "Period exceeds range" if nstart >= self.real_end
|
53
|
+
|
54
|
+
while nstart < self.real_end
|
55
|
+
yield self.class.new(start, nstart, true, self.subopt)
|
56
|
+
start = nstart
|
57
|
+
nstart = start.advance(period.to_sym => unit)
|
58
|
+
end
|
59
|
+
|
60
|
+
yield self.class.new(start, self.end, self.exclude_end?, self.subopt)
|
61
|
+
end
|
62
|
+
|
63
|
+
def each_month(&block)
|
64
|
+
each_slice_by_period(:months, 1, &block)
|
65
|
+
end
|
66
|
+
|
67
|
+
def each_year(&block)
|
68
|
+
each_slice_by_period(:years, 1, &block)
|
69
|
+
end
|
70
|
+
|
71
|
+
protected
|
72
|
+
|
73
|
+
def real_end
|
74
|
+
@real_end ||= self.exclude_end? ? self.end.advance({:days => -1}) : self.end
|
75
|
+
end
|
76
|
+
|
77
|
+
# make life easier when subclassing
|
78
|
+
def subopt
|
79
|
+
@subopt ||= {}
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
module TimeCrisis::DateRange::Date
|
84
|
+
module InstanceMethods
|
85
|
+
def from(unit, scale)
|
86
|
+
self.range(unit, scale, 'past')
|
87
|
+
end
|
88
|
+
|
89
|
+
def for(unit, scale)
|
90
|
+
self.range(unit, scale, 'future')
|
91
|
+
end
|
92
|
+
|
93
|
+
def range(unit=1, scale='years', direction='past')
|
94
|
+
selfkey = direction == 'past' ? :end : :begin
|
95
|
+
self.class.range({selfkey => self, :unit => unit, :scale => scale})
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
module ClassMethods
|
100
|
+
def range(*args)
|
101
|
+
TimeCrisis::DateRange.new(*args)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
TimeCrisis::Date.send(:include, TimeCrisis::DateRange::Date::InstanceMethods)
|
107
|
+
TimeCrisis::Date.extend(TimeCrisis::DateRange::Date::ClassMethods)
|
108
|
+
|
109
|
+
::Date.send(:include, TimeCrisis::DateRange::Date::InstanceMethods)
|
110
|
+
::Date.extend(TimeCrisis::DateRange::Date::ClassMethods)
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module TimeCrisis
|
2
|
+
# Meteorological seasons are reckoned by temperature, with summer being
|
3
|
+
# the hottest quarter of the year and winter the coldest quarter of the
|
4
|
+
# year. Using this reckoning, the Roman calendar began the year and the
|
5
|
+
# spring season on the first of March, with each season occupying three
|
6
|
+
# months. In 1780 the Societas Meteorologica Palatina, an early
|
7
|
+
# international organization for meteorology, defined seasons as groupings
|
8
|
+
# of three whole months. Ever since, professional meteorologists
|
9
|
+
# everywhere have used this definition. So, in meteorology for the
|
10
|
+
# Northern hemisphere: spring begins on 1 March, summer on 1 June, autumn
|
11
|
+
# on 1 September, and winter on 1 December.
|
12
|
+
# - http://en.wikipedia.org/wiki/Season
|
13
|
+
module MeteorologicalSeasons
|
14
|
+
module ClassMethods
|
15
|
+
def beginning_of_meteorological_spring(year=current.year)
|
16
|
+
TimeCrisis::Date.civil(year, 3, 1)
|
17
|
+
end
|
18
|
+
|
19
|
+
def beginning_of_meteorological_summer(year=current.year)
|
20
|
+
TimeCrisis::Date.civil(year, 6, 1)
|
21
|
+
end
|
22
|
+
|
23
|
+
def beginning_of_meteorological_autumn(year=current.year)
|
24
|
+
TimeCrisis::Date.civil(year, 9, 1)
|
25
|
+
end
|
26
|
+
|
27
|
+
alias beginning_of_meteorological_fall beginning_of_meteorological_autumn
|
28
|
+
|
29
|
+
def beginning_of_meteorological_winter(year=current.year)
|
30
|
+
TimeCrisis::Date.civil(year, 12, 1)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module InstanceMethods
|
35
|
+
def meteorological_spring?
|
36
|
+
[3, 4, 5].include?(self.month)
|
37
|
+
end
|
38
|
+
|
39
|
+
def meteorological_summer?
|
40
|
+
[6, 7, 8].include?(self.month)
|
41
|
+
end
|
42
|
+
|
43
|
+
def meteorological_autumn?
|
44
|
+
[9, 10, 11].include?(self.month)
|
45
|
+
end
|
46
|
+
|
47
|
+
alias meteorological_fall? meteorological_autumn?
|
48
|
+
|
49
|
+
def meteorological_winter?
|
50
|
+
[1, 2, 12].include?(self.month)
|
51
|
+
end
|
52
|
+
|
53
|
+
def meteorological_season
|
54
|
+
case self.month
|
55
|
+
when (1..2) then
|
56
|
+
'winter'
|
57
|
+
when (3..5) then
|
58
|
+
'spring'
|
59
|
+
when (6..8) then
|
60
|
+
'summer'
|
61
|
+
when (9..11) then
|
62
|
+
'fall'
|
63
|
+
when 12 then
|
64
|
+
'winter'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
TimeCrisis::Date.extend(TimeCrisis::MeteorologicalSeasons::ClassMethods)
|
72
|
+
TimeCrisis::Date.send(:include, TimeCrisis::MeteorologicalSeasons::InstanceMethods)
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module TimeCrisis
|
2
|
+
module NamedMonths
|
3
|
+
def january(year=nil); month_range(1, year); end
|
4
|
+
def february(year=nil); month_range(2, year); end
|
5
|
+
def march(year=nil); month_range(3, year); end
|
6
|
+
def april(year=nil); month_range(4, year); end
|
7
|
+
def may(year=nil); month_range(5, year); end
|
8
|
+
def june(year=nil); month_range(6, year); end
|
9
|
+
def july(year=nil); month_range(7, year); end
|
10
|
+
def august(year=nil); month_range(8, year); end
|
11
|
+
def september(year=nil); month_range(9, year); end
|
12
|
+
def october(year=nil); month_range(10, year); end
|
13
|
+
def november(year=nil); month_range(11, year); end
|
14
|
+
def december(year=nil); month_range(12, year); end
|
15
|
+
|
16
|
+
def month_range(month=nil, year=nil)
|
17
|
+
month ||= current.month
|
18
|
+
year ||= current.year
|
19
|
+
|
20
|
+
base = TimeCrisis::Date.civil(year, month, 1)
|
21
|
+
base.for(1, 'months')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
TimeCrisis::Date.extend(TimeCrisis::NamedMonths)
|
27
|
+
|
28
|
+
::Date.extend(TimeCrisis::NamedMonths)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module TimeCrisis::Support::ActsLike
|
2
|
+
module Date
|
3
|
+
def acts_like_date?
|
4
|
+
true
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
module DateTime
|
9
|
+
def acts_like_date?
|
10
|
+
true
|
11
|
+
end
|
12
|
+
|
13
|
+
def acts_like_time?
|
14
|
+
true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
TimeCrisis::Date.send(:include, TimeCrisis::Support::ActsLike::Date)
|
20
|
+
TimeCrisis::DateTime.send(:include, TimeCrisis::Support::ActsLike::DateTime)
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module TimeCrisis::Support::Advance
|
2
|
+
module Date
|
3
|
+
def advance(options)
|
4
|
+
options = options.dup
|
5
|
+
d = self
|
6
|
+
d = d >> options.delete(:years) * 12 if options[:years]
|
7
|
+
d = d >> options.delete(:months) if options[:months]
|
8
|
+
d = d + options.delete(:weeks) * 7 if options[:weeks]
|
9
|
+
d = d + options.delete(:days) if options[:days]
|
10
|
+
d
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module DateTime
|
15
|
+
def advance(options)
|
16
|
+
d = to_tc_date.advance(options)
|
17
|
+
advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
|
18
|
+
seconds_to_advance = (options[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600
|
19
|
+
seconds_to_advance == 0 ? advanced_by_date : (advanced_by_date + Rational(seconds_to_advance.round, 86400).to_f)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
TimeCrisis::Date.send(:include, TimeCrisis::Support::Advance::Date)
|
25
|
+
TimeCrisis::DateTime.send(:include, TimeCrisis::Support::Advance::DateTime)
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module TimeCrisis::Support::Change
|
2
|
+
module Date
|
3
|
+
def change(options={})
|
4
|
+
TimeCrisis::Date.civil(
|
5
|
+
options[:year] || self.year,
|
6
|
+
options[:month] || self.month,
|
7
|
+
options[:day] || self.day
|
8
|
+
)
|
9
|
+
end
|
10
|
+
|
11
|
+
def beginning_of_year
|
12
|
+
change(:month => 1, :day => 1)
|
13
|
+
end
|
14
|
+
|
15
|
+
def end_of_year
|
16
|
+
change(:month => 12, :day => 31)
|
17
|
+
end
|
18
|
+
|
19
|
+
def beginning_of_month
|
20
|
+
change(:day => 1)
|
21
|
+
end
|
22
|
+
|
23
|
+
def end_of_month
|
24
|
+
last_day = days_in_month
|
25
|
+
change(:day => last_day)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module DateTime
|
30
|
+
def change(options={})
|
31
|
+
TimeCrisis::DateTime.civil(
|
32
|
+
options[:year] || year,
|
33
|
+
options[:month] || month,
|
34
|
+
options[:day] || day,
|
35
|
+
options[:hour] || hour,
|
36
|
+
options[:min] || (options[:hour] ? 0 : min),
|
37
|
+
options[:sec] || ((options[:hour] || options[:min]) ? 0 : sec),
|
38
|
+
options[:usec] || ((options[:hour] || options[:min] || options[:sec]) ? 0 : usec),
|
39
|
+
options[:offset] || offset
|
40
|
+
)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
TimeCrisis::Date.send(:public, :days_in_month)
|
46
|
+
TimeCrisis::Date.send(:include, TimeCrisis::Support::Change::Date)
|
47
|
+
TimeCrisis::DateTime.send(:include, TimeCrisis::Support::Change::DateTime)
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module TimeCrisis::Support::Conversions
|
2
|
+
module Ruby
|
3
|
+
module Date
|
4
|
+
def to_tc_date
|
5
|
+
TimeCrisis::Date.civil(year, month, day)
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_tc_datetime
|
9
|
+
TimeCrisis::DateTime.civil(year, month, day, 0, 0, 0, 0)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module Time
|
14
|
+
def to_tc_date
|
15
|
+
TimeCrisis::Date.civil(year, month, day)
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_tc_datetime
|
19
|
+
TimeCrisis::DateTime.civil(year, month, day, hour, min, sec, 0, utc_offset)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module DateTime
|
24
|
+
def to_tc_date
|
25
|
+
TimeCrisis::Date.civil(year, month, day)
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_tc_datetime
|
29
|
+
TimeCrisis::DateTime.civil(year, month, day, hour, min, sec, 0, (offset * 86400).to_i)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module String
|
34
|
+
def to_tc_date(opts={})
|
35
|
+
TimeCrisis::Date.parse(self, opts)
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_tc_datetime(opts={})
|
39
|
+
TimeCrisis::DateTime.parse(self, opts)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module Internal
|
45
|
+
module Date
|
46
|
+
def to_tc_date
|
47
|
+
self
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_tc_datetime
|
51
|
+
TimeCrisis::DateTime.civil(year, month, day, 0, 0, 0, 0)
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_date
|
55
|
+
::Date.civil(year, month, day)
|
56
|
+
end
|
57
|
+
|
58
|
+
def to_datetime
|
59
|
+
::DateTime.civil(year, month, day, 0, 0, 0, 0)
|
60
|
+
end
|
61
|
+
|
62
|
+
def to_time(form = :local)
|
63
|
+
::Time.send(form, year, month, day)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
module DateTime
|
68
|
+
def to_tc_date
|
69
|
+
TimeCrisis::Date.civil(year, month, day)
|
70
|
+
end
|
71
|
+
|
72
|
+
def to_tc_datetime
|
73
|
+
self
|
74
|
+
end
|
75
|
+
|
76
|
+
def to_date
|
77
|
+
::Date.civil(year, month, day)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
TimeCrisis::Date.send(:include, TimeCrisis::Support::Conversions::Internal::Date)
|
84
|
+
TimeCrisis::DateTime.send(:include, TimeCrisis::Support::Conversions::Internal::DateTime)
|
85
|
+
|
86
|
+
::Date.send(:include, TimeCrisis::Support::Conversions::Ruby::Date)
|
87
|
+
::Time.send(:include, TimeCrisis::Support::Conversions::Ruby::Time)
|
88
|
+
::DateTime.send(:include, TimeCrisis::Support::Conversions::Ruby::DateTime)
|
89
|
+
::String.send(:include, TimeCrisis::Support::Conversions::Ruby::String)
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module TimeCrisis::Support::Current
|
2
|
+
module Date
|
3
|
+
def current
|
4
|
+
today
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
module DateTime
|
9
|
+
def current
|
10
|
+
now
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
TimeCrisis::Date.extend(TimeCrisis::Support::Current::Date)
|
16
|
+
TimeCrisis::DateTime.extend(TimeCrisis::Support::Current::DateTime)
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module TimeCrisis::Support::ReadableInspect
|
2
|
+
module Date
|
3
|
+
def readable_inspect
|
4
|
+
"#<TimeCrisis::Date #{strftime('%Y-%m-%d')}>"
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.class_eval do
|
9
|
+
alias_method :default_inspect, :inspect
|
10
|
+
alias_method :inspect, :readable_inspect
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module DateTime
|
16
|
+
def readable_inspect
|
17
|
+
"#<TimeCrisis::DateTime #{strftime('%Y-%m-%dT%H:%M:%S%Z')}>"
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.included(base)
|
21
|
+
base.class_eval do
|
22
|
+
alias_method :default_inspect, :inspect
|
23
|
+
alias_method :inspect, :readable_inspect
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
TimeCrisis::Date.send(:include, TimeCrisis::Support::ReadableInspect::Date)
|
30
|
+
TimeCrisis::DateTime.send(:include, TimeCrisis::Support::ReadableInspect::DateTime)
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module TimeCrisis::Support
|
2
|
+
end
|
3
|
+
|
4
|
+
require 'time_crisis/support/conversions'
|
5
|
+
require 'time_crisis/support/current'
|
6
|
+
require 'time_crisis/support/change'
|
7
|
+
require 'time_crisis/support/advance'
|
8
|
+
require 'time_crisis/support/readable_inspect'
|
9
|
+
require 'time_crisis/support/acts_like'
|
10
|
+
|
data/lib/time_crisis.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'third_base'
|
2
|
+
|
3
|
+
module TimeCrisis
|
4
|
+
include ThirdBase
|
5
|
+
end
|
6
|
+
|
7
|
+
# add needed bits of active support to time crisis
|
8
|
+
# necessary now that it uses ThirdBase
|
9
|
+
require 'time_crisis/support'
|
10
|
+
|
11
|
+
require 'time_crisis/meteorological_seasons'
|
12
|
+
require 'time_crisis/date_range'
|
13
|
+
require 'time_crisis/named_months'
|
data/test/test_helper.rb
ADDED
data/time_crisis.gemspec
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{time_crisis}
|
8
|
+
s.version = "0.1.4"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Travis Tilley"]
|
12
|
+
s.date = %q{2009-09-30}
|
13
|
+
s.description = %q{date and time related extensions}
|
14
|
+
s.email = %q{ttilley@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.markdown"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.markdown",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION.yml",
|
26
|
+
"lib/time_crisis.rb",
|
27
|
+
"lib/time_crisis/date_range.rb",
|
28
|
+
"lib/time_crisis/meteorological_seasons.rb",
|
29
|
+
"lib/time_crisis/named_months.rb",
|
30
|
+
"lib/time_crisis/support.rb",
|
31
|
+
"lib/time_crisis/support/acts_like.rb",
|
32
|
+
"lib/time_crisis/support/advance.rb",
|
33
|
+
"lib/time_crisis/support/change.rb",
|
34
|
+
"lib/time_crisis/support/conversions.rb",
|
35
|
+
"lib/time_crisis/support/current.rb",
|
36
|
+
"lib/time_crisis/support/readable_inspect.rb",
|
37
|
+
"test/test_helper.rb",
|
38
|
+
"test/time_crisis_test.rb",
|
39
|
+
"time_crisis.gemspec"
|
40
|
+
]
|
41
|
+
s.homepage = %q{http://github.com/ttilley/time_crisis}
|
42
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
43
|
+
s.require_paths = ["lib"]
|
44
|
+
s.rubygems_version = %q{1.3.5}
|
45
|
+
s.summary = %q{date and time related extensions}
|
46
|
+
s.test_files = [
|
47
|
+
"test/test_helper.rb",
|
48
|
+
"test/time_crisis_test.rb"
|
49
|
+
]
|
50
|
+
|
51
|
+
if s.respond_to? :specification_version then
|
52
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
53
|
+
s.specification_version = 3
|
54
|
+
|
55
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
56
|
+
s.add_runtime_dependency(%q<third_base>, [">= 0"])
|
57
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
58
|
+
else
|
59
|
+
s.add_dependency(%q<third_base>, [">= 0"])
|
60
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
61
|
+
end
|
62
|
+
else
|
63
|
+
s.add_dependency(%q<third_base>, [">= 0"])
|
64
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
65
|
+
end
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: time_crisis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Travis Tilley
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-30 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: third_base
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: thoughtbot-shoulda
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: date and time related extensions
|
36
|
+
email: ttilley@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.markdown
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.markdown
|
49
|
+
- Rakefile
|
50
|
+
- VERSION.yml
|
51
|
+
- lib/time_crisis.rb
|
52
|
+
- lib/time_crisis/date_range.rb
|
53
|
+
- lib/time_crisis/meteorological_seasons.rb
|
54
|
+
- lib/time_crisis/named_months.rb
|
55
|
+
- lib/time_crisis/support.rb
|
56
|
+
- lib/time_crisis/support/acts_like.rb
|
57
|
+
- lib/time_crisis/support/advance.rb
|
58
|
+
- lib/time_crisis/support/change.rb
|
59
|
+
- lib/time_crisis/support/conversions.rb
|
60
|
+
- lib/time_crisis/support/current.rb
|
61
|
+
- lib/time_crisis/support/readable_inspect.rb
|
62
|
+
- test/test_helper.rb
|
63
|
+
- test/time_crisis_test.rb
|
64
|
+
- time_crisis.gemspec
|
65
|
+
has_rdoc: true
|
66
|
+
homepage: http://github.com/ttilley/time_crisis
|
67
|
+
licenses: []
|
68
|
+
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options:
|
71
|
+
- --charset=UTF-8
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
version:
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: "0"
|
85
|
+
version:
|
86
|
+
requirements: []
|
87
|
+
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.3.5
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: date and time related extensions
|
93
|
+
test_files:
|
94
|
+
- test/test_helper.rb
|
95
|
+
- test/time_crisis_test.rb
|