emipair-delayed_job 2.0.3.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 +2 -0
- data/MIT-LICENSE +20 -0
- data/README.textile +213 -0
- data/Rakefile +33 -0
- data/VERSION +1 -0
- data/benchmarks.rb +33 -0
- data/contrib/delayed_job.monitrc +14 -0
- data/contrib/delayed_job_multiple.monitrc +23 -0
- data/emipair-delayed_job.gemspec +25 -0
- data/generators/delayed_job/delayed_job_generator.rb +22 -0
- data/generators/delayed_job/templates/migration.rb +21 -0
- data/generators/delayed_job/templates/script +5 -0
- data/init.rb +1 -0
- data/lib/delayed/backend/active_record.rb +90 -0
- data/lib/delayed/backend/base.rb +111 -0
- data/lib/delayed/backend/data_mapper.rb +147 -0
- data/lib/delayed/backend/mongo_mapper.rb +110 -0
- data/lib/delayed/command.rb +109 -0
- data/lib/delayed/message_sending.rb +22 -0
- data/lib/delayed/performable_method.rb +62 -0
- data/lib/delayed/railtie.rb +10 -0
- data/lib/delayed/recipes.rb +31 -0
- data/lib/delayed/tasks.rb +15 -0
- data/lib/delayed/worker.rb +214 -0
- data/lib/delayed_job.rb +15 -0
- data/lib/passive_support.rb +4 -0
- data/lib/passive_support/basic_object.rb +16 -0
- data/lib/passive_support/core_ext.rb +8 -0
- data/lib/passive_support/core_ext/class.rb +1 -0
- data/lib/passive_support/core_ext/class/attribute_accessors.rb +57 -0
- data/lib/passive_support/core_ext/date.rb +10 -0
- data/lib/passive_support/core_ext/date/behavior.rb +42 -0
- data/lib/passive_support/core_ext/date/calculations.rb +241 -0
- data/lib/passive_support/core_ext/date/conversions.rb +107 -0
- data/lib/passive_support/core_ext/date_time.rb +12 -0
- data/lib/passive_support/core_ext/date_time/calculations.rb +126 -0
- data/lib/passive_support/core_ext/date_time/conversions.rb +107 -0
- data/lib/passive_support/core_ext/enumerable.rb +120 -0
- data/lib/passive_support/core_ext/kernel.rb +5 -0
- data/lib/passive_support/core_ext/kernel/agnostics.rb +11 -0
- data/lib/passive_support/core_ext/kernel/daemonizing.rb +7 -0
- data/lib/passive_support/core_ext/kernel/debugger.rb +16 -0
- data/lib/passive_support/core_ext/kernel/reporting.rb +59 -0
- data/lib/passive_support/core_ext/kernel/requires.rb +24 -0
- data/lib/passive_support/core_ext/module.rb +20 -0
- data/lib/passive_support/core_ext/module/aliasing.rb +74 -0
- data/lib/passive_support/core_ext/module/attr_accessor_with_default.rb +31 -0
- data/lib/passive_support/core_ext/module/attr_internal.rb +32 -0
- data/lib/passive_support/core_ext/module/delegation.rb +135 -0
- data/lib/passive_support/core_ext/module/inclusion.rb +30 -0
- data/lib/passive_support/core_ext/module/introspection.rb +90 -0
- data/lib/passive_support/core_ext/module/loading.rb +23 -0
- data/lib/passive_support/core_ext/module/model_naming.rb +25 -0
- data/lib/passive_support/core_ext/module/synchronization.rb +39 -0
- data/lib/passive_support/core_ext/numeric.rb +9 -0
- data/lib/passive_support/core_ext/numeric/bytes.rb +50 -0
- data/lib/passive_support/core_ext/numeric/conversions.rb +19 -0
- data/lib/passive_support/core_ext/numeric/time.rb +81 -0
- data/lib/passive_support/core_ext/object.rb +6 -0
- data/lib/passive_support/core_ext/object/blank.rb +76 -0
- data/lib/passive_support/core_ext/object/conversions.rb +15 -0
- data/lib/passive_support/core_ext/object/extending.rb +80 -0
- data/lib/passive_support/core_ext/object/instance_variables.rb +74 -0
- data/lib/passive_support/core_ext/object/misc.rb +90 -0
- data/lib/passive_support/core_ext/object/singleton_class.rb +13 -0
- data/lib/passive_support/core_ext/string.rb +1 -0
- data/lib/passive_support/core_ext/string/constantize.rb +7 -0
- data/lib/passive_support/core_ext/time.rb +46 -0
- data/lib/passive_support/core_ext/time/behavior.rb +13 -0
- data/lib/passive_support/core_ext/time/calculations.rb +313 -0
- data/lib/passive_support/core_ext/time/conversions.rb +90 -0
- data/lib/passive_support/core_ext/time/zones.rb +86 -0
- data/lib/passive_support/duration.rb +100 -0
- data/lib/passive_support/ordered_hash.rb +158 -0
- data/rails/init.rb +5 -0
- data/recipes/delayed_job.rb +1 -0
- data/spec/backend/active_record_job_spec.rb +46 -0
- data/spec/backend/data_mapper_job_spec.rb +16 -0
- data/spec/backend/mongo_mapper_job_spec.rb +94 -0
- data/spec/backend/shared_backend_spec.rb +268 -0
- data/spec/delayed_method_spec.rb +58 -0
- data/spec/performable_method_spec.rb +42 -0
- data/spec/sample_jobs.rb +25 -0
- data/spec/setup/active_record.rb +33 -0
- data/spec/setup/data_mapper.rb +24 -0
- data/spec/setup/mongo_mapper.rb +17 -0
- data/spec/spec_helper.rb +19 -0
- data/spec/story_spec.rb +17 -0
- data/spec/worker_spec.rb +225 -0
- data/tasks/jobs.rake +1 -0
- metadata +323 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
require 'date'
|
|
2
|
+
require 'passive_support/core_ext/date/behavior'
|
|
3
|
+
require 'passive_support/core_ext/date/calculations'
|
|
4
|
+
require 'passive_support/core_ext/date/conversions'
|
|
5
|
+
|
|
6
|
+
class Date#:nodoc:
|
|
7
|
+
include PassiveSupport::CoreExtensions::Date::Behavior
|
|
8
|
+
include PassiveSupport::CoreExtensions::Date::Calculations
|
|
9
|
+
include PassiveSupport::CoreExtensions::Date::Conversions
|
|
10
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
require 'date'
|
|
2
|
+
|
|
3
|
+
module PassiveSupport #:nodoc:
|
|
4
|
+
module CoreExtensions #:nodoc:
|
|
5
|
+
module Date #:nodoc:
|
|
6
|
+
module Behavior
|
|
7
|
+
# Enable more predictable duck-typing on Date-like classes. See
|
|
8
|
+
# Object#acts_like?.
|
|
9
|
+
def acts_like_date?
|
|
10
|
+
true
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Date memoizes some instance methods using metaprogramming to wrap
|
|
14
|
+
# the methods with one that caches the result in an instance variable.
|
|
15
|
+
#
|
|
16
|
+
# If a Date is frozen but the memoized method hasn't been called, the
|
|
17
|
+
# first call will result in a frozen object error since the memo
|
|
18
|
+
# instance variable is uninitialized.
|
|
19
|
+
#
|
|
20
|
+
# Work around by eagerly memoizing before freezing.
|
|
21
|
+
#
|
|
22
|
+
# Ruby 1.9 uses a preinitialized instance variable so it's unaffected.
|
|
23
|
+
# This hack is as close as we can get to feature detection:
|
|
24
|
+
begin
|
|
25
|
+
::Date.today.freeze.jd
|
|
26
|
+
rescue => frozen_object_error
|
|
27
|
+
if frozen_object_error.message =~ /frozen/
|
|
28
|
+
def freeze #:nodoc:
|
|
29
|
+
self.class.private_instance_methods(false).each do |m|
|
|
30
|
+
if m.to_s =~ /\A__\d+__\Z/
|
|
31
|
+
instance_variable_set(:"@#{m}", [send(m)])
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
super
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
module PassiveSupport #:nodoc:
|
|
2
|
+
module CoreExtensions #:nodoc:
|
|
3
|
+
module Date #:nodoc:
|
|
4
|
+
# Enables the use of time calculations within Date itself
|
|
5
|
+
module Calculations
|
|
6
|
+
def self.included(base) #:nodoc:
|
|
7
|
+
base.extend ClassMethods
|
|
8
|
+
|
|
9
|
+
base.instance_eval do
|
|
10
|
+
alias_method :plus_without_duration, :+
|
|
11
|
+
alias_method :+, :plus_with_duration
|
|
12
|
+
|
|
13
|
+
alias_method :minus_without_duration, :-
|
|
14
|
+
alias_method :-, :minus_with_duration
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
module ClassMethods
|
|
19
|
+
# Returns a new Date representing the date 1 day ago (i.e. yesterday's date).
|
|
20
|
+
def yesterday
|
|
21
|
+
::Date.today.yesterday
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Returns a new Date representing the date 1 day after today (i.e. tomorrow's date).
|
|
25
|
+
def tomorrow
|
|
26
|
+
::Date.today.tomorrow
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Returns Time.zone.today when config.time_zone is set, otherwise just returns Date.today.
|
|
30
|
+
def current
|
|
31
|
+
::Time.zone_default ? ::Time.zone.today : ::Date.today
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Tells whether the Date object's date lies in the past
|
|
36
|
+
def past?
|
|
37
|
+
self < ::Date.current
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Tells whether the Date object's date is today
|
|
41
|
+
def today?
|
|
42
|
+
self.to_date == ::Date.current # we need the to_date because of DateTime
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Tells whether the Date object's date lies in the future
|
|
46
|
+
def future?
|
|
47
|
+
self > ::Date.current
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
|
|
51
|
+
# and then subtracts the specified number of seconds
|
|
52
|
+
def ago(seconds)
|
|
53
|
+
to_time.since(-seconds)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
|
|
57
|
+
# and then adds the specified number of seconds
|
|
58
|
+
def since(seconds)
|
|
59
|
+
to_time.since(seconds)
|
|
60
|
+
end
|
|
61
|
+
alias :in :since
|
|
62
|
+
|
|
63
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
|
|
64
|
+
def beginning_of_day
|
|
65
|
+
to_time
|
|
66
|
+
end
|
|
67
|
+
alias :midnight :beginning_of_day
|
|
68
|
+
alias :at_midnight :beginning_of_day
|
|
69
|
+
alias :at_beginning_of_day :beginning_of_day
|
|
70
|
+
|
|
71
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59)
|
|
72
|
+
def end_of_day
|
|
73
|
+
to_time.end_of_day
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def plus_with_duration(other) #:nodoc:
|
|
77
|
+
if PassiveSupport::Duration === other
|
|
78
|
+
other.since(self)
|
|
79
|
+
else
|
|
80
|
+
plus_without_duration(other)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def minus_with_duration(other) #:nodoc:
|
|
85
|
+
if PassiveSupport::Duration === other
|
|
86
|
+
plus_with_duration(-other)
|
|
87
|
+
else
|
|
88
|
+
minus_without_duration(other)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with
|
|
93
|
+
# any of these keys: <tt>:years</tt>, <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>.
|
|
94
|
+
def advance(options)
|
|
95
|
+
options = options.dup
|
|
96
|
+
d = self
|
|
97
|
+
d = d >> options.delete(:years) * 12 if options[:years]
|
|
98
|
+
d = d >> options.delete(:months) if options[:months]
|
|
99
|
+
d = d + options.delete(:weeks) * 7 if options[:weeks]
|
|
100
|
+
d = d + options.delete(:days) if options[:days]
|
|
101
|
+
d
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Returns a new Date where one or more of the elements have been changed according to the +options+ parameter.
|
|
105
|
+
#
|
|
106
|
+
# Examples:
|
|
107
|
+
#
|
|
108
|
+
# Date.new(2007, 5, 12).change(:day => 1) # => Date.new(2007, 5, 1)
|
|
109
|
+
# Date.new(2007, 5, 12).change(:year => 2005, :month => 1) # => Date.new(2005, 1, 12)
|
|
110
|
+
def change(options)
|
|
111
|
+
::Date.new(
|
|
112
|
+
options[:year] || self.year,
|
|
113
|
+
options[:month] || self.month,
|
|
114
|
+
options[:day] || self.day
|
|
115
|
+
)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Returns a new Date/DateTime representing the time a number of specified months ago
|
|
119
|
+
def months_ago(months)
|
|
120
|
+
advance(:months => -months)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Returns a new Date/DateTime representing the time a number of specified months in the future
|
|
124
|
+
def months_since(months)
|
|
125
|
+
advance(:months => months)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Returns a new Date/DateTime representing the time a number of specified years ago
|
|
129
|
+
def years_ago(years)
|
|
130
|
+
advance(:years => -years)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Returns a new Date/DateTime representing the time a number of specified years in the future
|
|
134
|
+
def years_since(years)
|
|
135
|
+
advance(:years => years)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def last_year # :nodoc:
|
|
139
|
+
PassiveSupport::Deprecation.warn("Date#last_year is deprecated and has been removed in Rails 3, please use Date#prev_year instead", caller)
|
|
140
|
+
prev_year
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Short-hand for years_ago(1)
|
|
144
|
+
def prev_year
|
|
145
|
+
years_ago(1)
|
|
146
|
+
end unless method_defined?(:prev_year)
|
|
147
|
+
|
|
148
|
+
# Short-hand for years_since(1)
|
|
149
|
+
def next_year
|
|
150
|
+
years_since(1)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def last_month # :nodoc:
|
|
154
|
+
PassiveSupport::Deprecation.warn("Date#last_month is deprecated and has been removed in Rails 3, please use Date#prev_month instead", caller)
|
|
155
|
+
prev_month
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# Short-hand for months_ago(1)
|
|
159
|
+
def prev_month
|
|
160
|
+
months_ago(1)
|
|
161
|
+
end unless method_defined?(:prev_month)
|
|
162
|
+
|
|
163
|
+
# Short-hand for months_since(1)
|
|
164
|
+
def next_month
|
|
165
|
+
months_since(1)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# Returns a new Date/DateTime representing the "start" of this week (i.e, Monday; DateTime objects will have time set to 0:00)
|
|
169
|
+
def beginning_of_week
|
|
170
|
+
days_to_monday = self.wday!=0 ? self.wday-1 : 6
|
|
171
|
+
result = self - days_to_monday
|
|
172
|
+
self.acts_like?(:time) ? result.midnight : result
|
|
173
|
+
end
|
|
174
|
+
alias :monday :beginning_of_week
|
|
175
|
+
alias :at_beginning_of_week :beginning_of_week
|
|
176
|
+
|
|
177
|
+
# Returns a new Date/DateTime representing the end of this week (Sunday, DateTime objects will have time set to 23:59:59)
|
|
178
|
+
def end_of_week
|
|
179
|
+
days_to_sunday = self.wday!=0 ? 7-self.wday : 0
|
|
180
|
+
result = self + days_to_sunday.days
|
|
181
|
+
self.acts_like?(:time) ? result.end_of_day : result
|
|
182
|
+
end
|
|
183
|
+
alias :at_end_of_week :end_of_week
|
|
184
|
+
|
|
185
|
+
# Returns a new Date/DateTime representing the start of the given day in next week (default is Monday).
|
|
186
|
+
def next_week(day = :monday)
|
|
187
|
+
days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
|
|
188
|
+
result = (self + 7).beginning_of_week + days_into_week[day]
|
|
189
|
+
self.acts_like?(:time) ? result.change(:hour => 0) : result
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# Returns a new ; DateTime objects will have time set to 0:00DateTime representing the start of the month (1st of the month; DateTime objects will have time set to 0:00)
|
|
193
|
+
def beginning_of_month
|
|
194
|
+
self.acts_like?(:time) ? change(:day => 1,:hour => 0, :min => 0, :sec => 0) : change(:day => 1)
|
|
195
|
+
end
|
|
196
|
+
alias :at_beginning_of_month :beginning_of_month
|
|
197
|
+
|
|
198
|
+
# Returns a new Date/DateTime representing the end of the month (last day of the month; DateTime objects will have time set to 0:00)
|
|
199
|
+
def end_of_month
|
|
200
|
+
last_day = ::Time.days_in_month( self.month, self.year )
|
|
201
|
+
self.acts_like?(:time) ? change(:day => last_day, :hour => 23, :min => 59, :sec => 59) : change(:day => last_day)
|
|
202
|
+
end
|
|
203
|
+
alias :at_end_of_month :end_of_month
|
|
204
|
+
|
|
205
|
+
# Returns a new Date/DateTime representing the start of the quarter (1st of january, april, july, october; DateTime objects will have time set to 0:00)
|
|
206
|
+
def beginning_of_quarter
|
|
207
|
+
beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
|
|
208
|
+
end
|
|
209
|
+
alias :at_beginning_of_quarter :beginning_of_quarter
|
|
210
|
+
|
|
211
|
+
# Returns a new Date/DateTime representing the end of the quarter (last day of march, june, september, december; DateTime objects will have time set to 23:59:59)
|
|
212
|
+
def end_of_quarter
|
|
213
|
+
beginning_of_month.change(:month => [3, 6, 9, 12].detect { |m| m >= self.month }).end_of_month
|
|
214
|
+
end
|
|
215
|
+
alias :at_end_of_quarter :end_of_quarter
|
|
216
|
+
|
|
217
|
+
# Returns a new Date/DateTime representing the start of the year (1st of january; DateTime objects will have time set to 0:00)
|
|
218
|
+
def beginning_of_year
|
|
219
|
+
self.acts_like?(:time) ? change(:month => 1, :day => 1, :hour => 0, :min => 0, :sec => 0) : change(:month => 1, :day => 1)
|
|
220
|
+
end
|
|
221
|
+
alias :at_beginning_of_year :beginning_of_year
|
|
222
|
+
|
|
223
|
+
# Returns a new Time representing the end of the year (31st of december; DateTime objects will have time set to 23:59:59)
|
|
224
|
+
def end_of_year
|
|
225
|
+
self.acts_like?(:time) ? change(:month => 12,:day => 31,:hour => 23, :min => 59, :sec => 59) : change(:month => 12, :day => 31)
|
|
226
|
+
end
|
|
227
|
+
alias :at_end_of_year :end_of_year
|
|
228
|
+
|
|
229
|
+
# Convenience method which returns a new Date/DateTime representing the time 1 day ago
|
|
230
|
+
def yesterday
|
|
231
|
+
self - 1
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# Convenience method which returns a new Date/DateTime representing the time 1 day since the instance time
|
|
235
|
+
def tomorrow
|
|
236
|
+
self + 1
|
|
237
|
+
end
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
end
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
module PassiveSupport #:nodoc:
|
|
2
|
+
module CoreExtensions #:nodoc:
|
|
3
|
+
module Date #:nodoc:
|
|
4
|
+
# Converting dates to formatted strings, times, and datetimes.
|
|
5
|
+
module Conversions
|
|
6
|
+
DATE_FORMATS = {
|
|
7
|
+
:short => "%e %b",
|
|
8
|
+
:long => "%B %e, %Y",
|
|
9
|
+
:db => "%Y-%m-%d",
|
|
10
|
+
:number => "%Y%m%d",
|
|
11
|
+
:long_ordinal => lambda { |date| date.strftime("%B #{date.day.ordinalize}, %Y") }, # => "April 25th, 2007"
|
|
12
|
+
:rfc822 => "%e %b %Y"
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
def self.included(base) #:nodoc:
|
|
16
|
+
base.instance_eval do
|
|
17
|
+
alias_method :to_default_s, :to_s
|
|
18
|
+
alias_method :to_s, :to_formatted_s
|
|
19
|
+
alias_method :default_inspect, :inspect
|
|
20
|
+
alias_method :inspect, :readable_inspect
|
|
21
|
+
|
|
22
|
+
# Ruby 1.9 has Date#to_time which converts to localtime only.
|
|
23
|
+
remove_method :to_time if base.instance_methods.include?(:to_time)
|
|
24
|
+
|
|
25
|
+
# Ruby 1.9 has Date#xmlschema which converts to a string without the time component.
|
|
26
|
+
remove_method :xmlschema if base.instance_methods.include?(:xmlschema)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Convert to a formatted string. See DATE_FORMATS for predefined formats.
|
|
31
|
+
#
|
|
32
|
+
# This method is aliased to <tt>to_s</tt>.
|
|
33
|
+
#
|
|
34
|
+
# ==== Examples
|
|
35
|
+
# date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
|
|
36
|
+
#
|
|
37
|
+
# date.to_formatted_s(:db) # => "2007-11-10"
|
|
38
|
+
# date.to_s(:db) # => "2007-11-10"
|
|
39
|
+
#
|
|
40
|
+
# date.to_formatted_s(:short) # => "10 Nov"
|
|
41
|
+
# date.to_formatted_s(:long) # => "November 10, 2007"
|
|
42
|
+
# date.to_formatted_s(:long_ordinal) # => "November 10th, 2007"
|
|
43
|
+
# date.to_formatted_s(:rfc822) # => "10 Nov 2007"
|
|
44
|
+
#
|
|
45
|
+
# == Adding your own time formats to to_formatted_s
|
|
46
|
+
# You can add your own formats to the Date::DATE_FORMATS hash.
|
|
47
|
+
# Use the format name as the hash key and either a strftime string
|
|
48
|
+
# or Proc instance that takes a date argument as the value.
|
|
49
|
+
#
|
|
50
|
+
# # config/initializers/time_formats.rb
|
|
51
|
+
# Date::DATE_FORMATS[:month_and_year] = "%B %Y"
|
|
52
|
+
# Date::DATE_FORMATS[:short_ordinal] = lambda { |date| date.strftime("%B #{date.day.ordinalize}") }
|
|
53
|
+
def to_formatted_s(format = :default)
|
|
54
|
+
if formatter = DATE_FORMATS[format]
|
|
55
|
+
if formatter.respond_to?(:call)
|
|
56
|
+
formatter.call(self).to_s
|
|
57
|
+
else
|
|
58
|
+
strftime(formatter)
|
|
59
|
+
end
|
|
60
|
+
else
|
|
61
|
+
to_default_s
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005"
|
|
66
|
+
def readable_inspect
|
|
67
|
+
strftime("%a, %d %b %Y")
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# A method to keep Time, Date and DateTime instances interchangeable on conversions.
|
|
71
|
+
# In this case, it simply returns +self+.
|
|
72
|
+
def to_date
|
|
73
|
+
self
|
|
74
|
+
end if RUBY_VERSION < '1.9'
|
|
75
|
+
|
|
76
|
+
# Converts a Date instance to a Time, where the time is set to the beginning of the day.
|
|
77
|
+
# The timezone can be either :local or :utc (default :local).
|
|
78
|
+
#
|
|
79
|
+
# ==== Examples
|
|
80
|
+
# date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
|
|
81
|
+
#
|
|
82
|
+
# date.to_time # => Sat Nov 10 00:00:00 0800 2007
|
|
83
|
+
# date.to_time(:local) # => Sat Nov 10 00:00:00 0800 2007
|
|
84
|
+
#
|
|
85
|
+
# date.to_time(:utc) # => Sat Nov 10 00:00:00 UTC 2007
|
|
86
|
+
def to_time(form = :local)
|
|
87
|
+
::Time.send("#{form}_time", year, month, day)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Converts a Date instance to a DateTime, where the time is set to the beginning of the day
|
|
91
|
+
# and UTC offset is set to 0.
|
|
92
|
+
#
|
|
93
|
+
# ==== Examples
|
|
94
|
+
# date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
|
|
95
|
+
#
|
|
96
|
+
# date.to_datetime # => Sat, 10 Nov 2007 00:00:00 0000
|
|
97
|
+
def to_datetime
|
|
98
|
+
::DateTime.civil(year, month, day, 0, 0, 0, 0)
|
|
99
|
+
end if RUBY_VERSION < '1.9'
|
|
100
|
+
|
|
101
|
+
def xmlschema
|
|
102
|
+
to_time.xmlschema
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
require 'date'
|
|
2
|
+
require 'passive_support/core_ext/time/behavior'
|
|
3
|
+
require 'passive_support/core_ext/time/zones'
|
|
4
|
+
require 'passive_support/core_ext/date_time/calculations'
|
|
5
|
+
require 'passive_support/core_ext/date_time/conversions'
|
|
6
|
+
|
|
7
|
+
class DateTime
|
|
8
|
+
include PassiveSupport::CoreExtensions::Time::Behavior
|
|
9
|
+
include PassiveSupport::CoreExtensions::Time::Zones
|
|
10
|
+
include PassiveSupport::CoreExtensions::DateTime::Calculations
|
|
11
|
+
include PassiveSupport::CoreExtensions::DateTime::Conversions
|
|
12
|
+
end
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
require 'rational'
|
|
2
|
+
|
|
3
|
+
module PassiveSupport #:nodoc:
|
|
4
|
+
module CoreExtensions #:nodoc:
|
|
5
|
+
module DateTime #:nodoc:
|
|
6
|
+
# Enables the use of time calculations within DateTime itself
|
|
7
|
+
module Calculations
|
|
8
|
+
def self.included(base) #:nodoc:
|
|
9
|
+
base.extend ClassMethods
|
|
10
|
+
|
|
11
|
+
base.class_eval do
|
|
12
|
+
alias_method :compare_without_coercion, :<=>
|
|
13
|
+
alias_method :<=>, :compare_with_coercion
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
module ClassMethods
|
|
18
|
+
# DateTimes aren't aware of DST rules, so use a consistent non-DST offset when creating a DateTime with an offset in the local zone
|
|
19
|
+
def local_offset
|
|
20
|
+
::Time.local(2007).utc_offset.to_r / 86400
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def current
|
|
24
|
+
::Time.zone_default ? ::Time.zone.now.to_datetime : ::Time.now.to_datetime
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Tells whether the DateTime object's datetime lies in the past
|
|
29
|
+
def past?
|
|
30
|
+
self < ::DateTime.current
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Tells whether the DateTime object's datetime lies in the future
|
|
34
|
+
def future?
|
|
35
|
+
self > ::DateTime.current
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Seconds since midnight: DateTime.now.seconds_since_midnight
|
|
39
|
+
def seconds_since_midnight
|
|
40
|
+
self.sec + (self.min * 60) + (self.hour * 3600)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Returns a new DateTime where one or more of the elements have been changed according to the +options+ parameter. The time options
|
|
44
|
+
# (hour, minute, sec) reset cascadingly, so if only the hour is passed, then minute and sec is set to 0. If the hour and
|
|
45
|
+
# minute is passed, then sec is set to 0.
|
|
46
|
+
def change(options)
|
|
47
|
+
::DateTime.civil(
|
|
48
|
+
options[:year] || self.year,
|
|
49
|
+
options[:month] || self.month,
|
|
50
|
+
options[:day] || self.day,
|
|
51
|
+
options[:hour] || self.hour,
|
|
52
|
+
options[:min] || (options[:hour] ? 0 : self.min),
|
|
53
|
+
options[:sec] || ((options[:hour] || options[:min]) ? 0 : self.sec),
|
|
54
|
+
options[:offset] || self.offset,
|
|
55
|
+
options[:start] || self.start
|
|
56
|
+
)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Uses Date to provide precise Time calculations for years, months, and days.
|
|
60
|
+
# The +options+ parameter takes a hash with any of these keys: <tt>:years</tt>,
|
|
61
|
+
# <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>, <tt>:hours</tt>,
|
|
62
|
+
# <tt>:minutes</tt>, <tt>:seconds</tt>.
|
|
63
|
+
def advance(options)
|
|
64
|
+
d = to_date.advance(options)
|
|
65
|
+
datetime_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
|
|
66
|
+
seconds_to_advance = (options[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600
|
|
67
|
+
seconds_to_advance == 0 ? datetime_advanced_by_date : datetime_advanced_by_date.since(seconds_to_advance)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Returns a new DateTime representing the time a number of seconds ago
|
|
71
|
+
# Do not use this method in combination with x.months, use months_ago instead!
|
|
72
|
+
def ago(seconds)
|
|
73
|
+
self.since(-seconds)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Returns a new DateTime representing the time a number of seconds since the instance time
|
|
77
|
+
# Do not use this method in combination with x.months, use months_since instead!
|
|
78
|
+
def since(seconds)
|
|
79
|
+
self + Rational(seconds.round, 86400)
|
|
80
|
+
end
|
|
81
|
+
alias :in :since
|
|
82
|
+
|
|
83
|
+
# Returns a new DateTime representing the start of the day (0:00)
|
|
84
|
+
def beginning_of_day
|
|
85
|
+
change(:hour => 0)
|
|
86
|
+
end
|
|
87
|
+
alias :midnight :beginning_of_day
|
|
88
|
+
alias :at_midnight :beginning_of_day
|
|
89
|
+
alias :at_beginning_of_day :beginning_of_day
|
|
90
|
+
|
|
91
|
+
# Returns a new DateTime representing the end of the day (23:59:59)
|
|
92
|
+
def end_of_day
|
|
93
|
+
change(:hour => 23, :min => 59, :sec => 59)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Adjusts DateTime to UTC by adding its offset value; offset is set to 0
|
|
97
|
+
#
|
|
98
|
+
# Example:
|
|
99
|
+
#
|
|
100
|
+
# DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)) # => Mon, 21 Feb 2005 10:11:12 -0600
|
|
101
|
+
# DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)).utc # => Mon, 21 Feb 2005 16:11:12 +0000
|
|
102
|
+
def utc
|
|
103
|
+
new_offset(0)
|
|
104
|
+
end
|
|
105
|
+
alias_method :getutc, :utc
|
|
106
|
+
|
|
107
|
+
# Returns true if offset == 0
|
|
108
|
+
def utc?
|
|
109
|
+
offset == 0
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Returns the offset value in seconds
|
|
113
|
+
def utc_offset
|
|
114
|
+
(offset * 86400).to_i
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# Layers additional behavior on DateTime#<=> so that Time and PassiveSupport::TimeWithZone instances can be compared with a DateTime
|
|
118
|
+
def compare_with_coercion(other)
|
|
119
|
+
other = other.comparable_time if other.respond_to?(:comparable_time)
|
|
120
|
+
other = other.to_datetime unless other.acts_like?(:date)
|
|
121
|
+
compare_without_coercion(other)
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|