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,13 @@
|
|
|
1
|
+
class Object
|
|
2
|
+
# Returns the object's singleton class.
|
|
3
|
+
def singleton_class
|
|
4
|
+
class << self
|
|
5
|
+
self
|
|
6
|
+
end
|
|
7
|
+
end unless respond_to?(:singleton_class)
|
|
8
|
+
|
|
9
|
+
# class_eval on an object acts like singleton_class_eval.
|
|
10
|
+
def class_eval(*args, &block)
|
|
11
|
+
singleton_class.class_eval(*args, &block)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'passive_support/core_ext/string/constantize'
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require 'date'
|
|
2
|
+
require 'time'
|
|
3
|
+
|
|
4
|
+
class Time
|
|
5
|
+
# Ruby 1.8-cvs and 1.9 define private Time#to_date
|
|
6
|
+
%w(to_date to_datetime).each do |method|
|
|
7
|
+
public method if private_instance_methods.include?(method)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# Pre-1.9 versions of Ruby have a bug with marshaling Time instances, where utc instances are
|
|
11
|
+
# unmarshaled in the local zone, instead of utc. We're layering behavior on the _dump and _load
|
|
12
|
+
# methods so that utc instances can be flagged on dump, and coerced back to utc on load.
|
|
13
|
+
if RUBY_VERSION < '1.9'
|
|
14
|
+
class << self
|
|
15
|
+
alias_method :_original_load, :_load
|
|
16
|
+
def _load(marshaled_time)
|
|
17
|
+
time = _original_load(marshaled_time)
|
|
18
|
+
time.instance_eval do
|
|
19
|
+
if defined?(@marshal_with_utc_coercion)
|
|
20
|
+
val = remove_instance_variable("@marshal_with_utc_coercion")
|
|
21
|
+
end
|
|
22
|
+
val ? utc : self
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
alias_method :_original_dump, :_dump
|
|
28
|
+
def _dump(*args)
|
|
29
|
+
obj = dup
|
|
30
|
+
obj.instance_variable_set('@marshal_with_utc_coercion', utc?)
|
|
31
|
+
obj._original_dump(*args)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
require 'passive_support/core_ext/time/behavior'
|
|
37
|
+
require 'passive_support/core_ext/time/calculations'
|
|
38
|
+
require 'passive_support/core_ext/time/conversions'
|
|
39
|
+
require 'passive_support/core_ext/time/zones'
|
|
40
|
+
|
|
41
|
+
class Time#:nodoc:
|
|
42
|
+
include PassiveSupport::CoreExtensions::Time::Behavior
|
|
43
|
+
include PassiveSupport::CoreExtensions::Time::Calculations
|
|
44
|
+
include PassiveSupport::CoreExtensions::Time::Conversions
|
|
45
|
+
include PassiveSupport::CoreExtensions::Time::Zones
|
|
46
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module PassiveSupport #:nodoc:
|
|
2
|
+
module CoreExtensions #:nodoc:
|
|
3
|
+
module Time #:nodoc:
|
|
4
|
+
module Behavior
|
|
5
|
+
# Enable more predictable duck-typing on Time-like classes. See
|
|
6
|
+
# Object#acts_like?.
|
|
7
|
+
def acts_like_time?
|
|
8
|
+
true
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
require 'passive_support/duration'
|
|
2
|
+
|
|
3
|
+
module PassiveSupport #:nodoc:
|
|
4
|
+
module CoreExtensions #:nodoc:
|
|
5
|
+
module Time #:nodoc:
|
|
6
|
+
# Enables the use of time calculations within Time itself
|
|
7
|
+
module Calculations
|
|
8
|
+
def self.included(base) #:nodoc:
|
|
9
|
+
base.extend ClassMethods
|
|
10
|
+
|
|
11
|
+
base.class_eval do
|
|
12
|
+
alias_method :plus_without_duration, :+
|
|
13
|
+
alias_method :+, :plus_with_duration
|
|
14
|
+
|
|
15
|
+
alias_method :minus_without_duration, :-
|
|
16
|
+
alias_method :-, :minus_with_duration
|
|
17
|
+
|
|
18
|
+
alias_method :minus_without_coercion, :-
|
|
19
|
+
alias_method :-, :minus_with_coercion
|
|
20
|
+
|
|
21
|
+
alias_method :compare_without_coercion, :<=>
|
|
22
|
+
alias_method :<=>, :compare_with_coercion
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
|
27
|
+
|
|
28
|
+
module ClassMethods
|
|
29
|
+
# Overriding case equality method so that it returns true for PassiveSupport::TimeWithZone instances
|
|
30
|
+
def ===(other)
|
|
31
|
+
other.is_a?(::Time)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Return the number of days in the given month.
|
|
35
|
+
# If no year is specified, it will use the current year.
|
|
36
|
+
def days_in_month(month, year = now.year)
|
|
37
|
+
return 29 if month == 2 && ::Date.gregorian_leap?(year)
|
|
38
|
+
COMMON_YEAR_DAYS_IN_MONTH[month]
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Returns a new Time if requested year can be accommodated by Ruby's Time class
|
|
42
|
+
# (i.e., if year is within either 1970..2038 or 1902..2038, depending on system architecture);
|
|
43
|
+
# otherwise returns a DateTime
|
|
44
|
+
def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0)
|
|
45
|
+
::Time.send(utc_or_local, year, month, day, hour, min, sec, usec)
|
|
46
|
+
rescue
|
|
47
|
+
offset = utc_or_local.to_sym == :local ? ::DateTime.local_offset : 0
|
|
48
|
+
::DateTime.civil(year, month, day, hour, min, sec, offset)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Wraps class method +time_with_datetime_fallback+ with +utc_or_local+ set to <tt>:utc</tt>.
|
|
52
|
+
def utc_time(*args)
|
|
53
|
+
time_with_datetime_fallback(:utc, *args)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Wraps class method +time_with_datetime_fallback+ with +utc_or_local+ set to <tt>:local</tt>.
|
|
57
|
+
def local_time(*args)
|
|
58
|
+
time_with_datetime_fallback(:local, *args)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Tells whether the Time object's time lies in the past
|
|
63
|
+
def past?
|
|
64
|
+
self < ::Time.current
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Tells whether the Time object's time is today
|
|
68
|
+
def today?
|
|
69
|
+
self.to_date == ::Date.current
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Tells whether the Time object's time lies in the future
|
|
73
|
+
def future?
|
|
74
|
+
self > ::Time.current
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Seconds since midnight: Time.now.seconds_since_midnight
|
|
78
|
+
def seconds_since_midnight
|
|
79
|
+
self.to_i - self.change(:hour => 0).to_i + (self.usec/1.0e+6)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Returns a new Time where one or more of the elements have been changed according to the +options+ parameter. The time options
|
|
83
|
+
# (hour, minute, sec, usec) reset cascadingly, so if only the hour is passed, then minute, sec, and usec is set to 0. If the hour and
|
|
84
|
+
# minute is passed, then sec and usec is set to 0.
|
|
85
|
+
def change(options)
|
|
86
|
+
::Time.send(
|
|
87
|
+
self.utc? ? :utc_time : :local_time,
|
|
88
|
+
options[:year] || self.year,
|
|
89
|
+
options[:month] || self.month,
|
|
90
|
+
options[:day] || self.day,
|
|
91
|
+
options[:hour] || self.hour,
|
|
92
|
+
options[:min] || (options[:hour] ? 0 : self.min),
|
|
93
|
+
options[:sec] || ((options[:hour] || options[:min]) ? 0 : self.sec),
|
|
94
|
+
options[:usec] || ((options[:hour] || options[:min] || options[:sec]) ? 0 : self.usec)
|
|
95
|
+
)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Uses Date to provide precise Time calculations for years, months, and days.
|
|
99
|
+
# The +options+ parameter takes a hash with any of these keys: <tt>:years</tt>,
|
|
100
|
+
# <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>, <tt>:hours</tt>,
|
|
101
|
+
# <tt>:minutes</tt>, <tt>:seconds</tt>.
|
|
102
|
+
def advance(options)
|
|
103
|
+
unless options[:weeks].nil?
|
|
104
|
+
options[:weeks], partial_weeks = options[:weeks].divmod(1)
|
|
105
|
+
options[:days] = (options[:days] || 0) + 7 * partial_weeks
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
unless options[:days].nil?
|
|
109
|
+
options[:days], partial_days = options[:days].divmod(1)
|
|
110
|
+
options[:hours] = (options[:hours] || 0) + 24 * partial_days
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
d = to_date.advance(options)
|
|
114
|
+
time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
|
|
115
|
+
seconds_to_advance = (options[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600
|
|
116
|
+
seconds_to_advance == 0 ? time_advanced_by_date : time_advanced_by_date.since(seconds_to_advance)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension
|
|
120
|
+
def ago(seconds)
|
|
121
|
+
self.since(-seconds)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Returns a new Time representing the time a number of seconds since the instance time, this is basically a wrapper around
|
|
125
|
+
# the Numeric extension.
|
|
126
|
+
def since(seconds)
|
|
127
|
+
f = seconds.since(self)
|
|
128
|
+
if PassiveSupport::Duration === seconds
|
|
129
|
+
f
|
|
130
|
+
else
|
|
131
|
+
initial_dst = self.dst? ? 1 : 0
|
|
132
|
+
final_dst = f.dst? ? 1 : 0
|
|
133
|
+
(seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f
|
|
134
|
+
end
|
|
135
|
+
rescue
|
|
136
|
+
self.to_datetime.since(seconds)
|
|
137
|
+
end
|
|
138
|
+
alias :in :since
|
|
139
|
+
|
|
140
|
+
# Returns a new Time representing the time a number of specified months ago
|
|
141
|
+
def months_ago(months)
|
|
142
|
+
advance(:months => -months)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Returns a new Time representing the time a number of specified months in the future
|
|
146
|
+
def months_since(months)
|
|
147
|
+
advance(:months => months)
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# Returns a new Time representing the time a number of specified years ago
|
|
151
|
+
def years_ago(years)
|
|
152
|
+
advance(:years => -years)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# Returns a new Time representing the time a number of specified years in the future
|
|
156
|
+
def years_since(years)
|
|
157
|
+
advance(:years => years)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def last_year # :nodoc:
|
|
161
|
+
PassiveSupport::Deprecation.warn("Time#last_year is deprecated and has been removed in Rails 3, please use Time#prev_year instead", caller)
|
|
162
|
+
prev_year
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Short-hand for years_ago(1)
|
|
166
|
+
def prev_year
|
|
167
|
+
years_ago(1)
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Short-hand for years_since(1)
|
|
171
|
+
def next_year
|
|
172
|
+
years_since(1)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def last_month # :nodoc:
|
|
176
|
+
PassiveSupport::Deprecation.warn("Time#last_month is deprecated and has been removed in Rails 3, please use Time#prev_month instead", caller)
|
|
177
|
+
prev_month
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Short-hand for months_ago(1)
|
|
181
|
+
def prev_month
|
|
182
|
+
months_ago(1)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# Short-hand for months_since(1)
|
|
186
|
+
def next_month
|
|
187
|
+
months_since(1)
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# Returns a new Time representing the "start" of this week (Monday, 0:00)
|
|
191
|
+
def beginning_of_week
|
|
192
|
+
days_to_monday = self.wday!=0 ? self.wday-1 : 6
|
|
193
|
+
(self - days_to_monday.days).midnight
|
|
194
|
+
end
|
|
195
|
+
alias :monday :beginning_of_week
|
|
196
|
+
alias :at_beginning_of_week :beginning_of_week
|
|
197
|
+
|
|
198
|
+
# Returns a new Time representing the end of this week (Sunday, 23:59:59)
|
|
199
|
+
def end_of_week
|
|
200
|
+
days_to_sunday = self.wday!=0 ? 7-self.wday : 0
|
|
201
|
+
(self + days_to_sunday.days).end_of_day
|
|
202
|
+
end
|
|
203
|
+
alias :at_end_of_week :end_of_week
|
|
204
|
+
|
|
205
|
+
# Returns a new Time representing the start of the given day in next week (default is Monday).
|
|
206
|
+
def next_week(day = :monday)
|
|
207
|
+
days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
|
|
208
|
+
since(1.week).beginning_of_week.since(days_into_week[day].day).change(:hour => 0)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# Returns a new Time representing the start of the day (0:00)
|
|
212
|
+
def beginning_of_day
|
|
213
|
+
#(self - seconds_since_midnight).change(:usec => 0)
|
|
214
|
+
change(:hour => 0, :min => 0, :sec => 0, :usec => 0)
|
|
215
|
+
end
|
|
216
|
+
alias :midnight :beginning_of_day
|
|
217
|
+
alias :at_midnight :beginning_of_day
|
|
218
|
+
alias :at_beginning_of_day :beginning_of_day
|
|
219
|
+
|
|
220
|
+
# Returns a new Time representing the end of the day, 23:59:59.999999 (.999999999 in ruby1.9)
|
|
221
|
+
def end_of_day
|
|
222
|
+
change(:hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# Returns a new Time representing the start of the month (1st of the month, 0:00)
|
|
226
|
+
def beginning_of_month
|
|
227
|
+
#self - ((self.mday-1).days + self.seconds_since_midnight)
|
|
228
|
+
change(:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
|
|
229
|
+
end
|
|
230
|
+
alias :at_beginning_of_month :beginning_of_month
|
|
231
|
+
|
|
232
|
+
# Returns a new Time representing the end of the month (end of the last day of the month)
|
|
233
|
+
def end_of_month
|
|
234
|
+
#self - ((self.mday-1).days + self.seconds_since_midnight)
|
|
235
|
+
last_day = ::Time.days_in_month( self.month, self.year )
|
|
236
|
+
change(:day => last_day, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
|
|
237
|
+
end
|
|
238
|
+
alias :at_end_of_month :end_of_month
|
|
239
|
+
|
|
240
|
+
# Returns a new Time representing the start of the quarter (1st of january, april, july, october, 0:00)
|
|
241
|
+
def beginning_of_quarter
|
|
242
|
+
beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
|
|
243
|
+
end
|
|
244
|
+
alias :at_beginning_of_quarter :beginning_of_quarter
|
|
245
|
+
|
|
246
|
+
# Returns a new Time representing the end of the quarter (end of the last day of march, june, september, december)
|
|
247
|
+
def end_of_quarter
|
|
248
|
+
beginning_of_month.change(:month => [3, 6, 9, 12].detect { |m| m >= self.month }).end_of_month
|
|
249
|
+
end
|
|
250
|
+
alias :at_end_of_quarter :end_of_quarter
|
|
251
|
+
|
|
252
|
+
# Returns a new Time representing the start of the year (1st of january, 0:00)
|
|
253
|
+
def beginning_of_year
|
|
254
|
+
change(:month => 1,:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
|
|
255
|
+
end
|
|
256
|
+
alias :at_beginning_of_year :beginning_of_year
|
|
257
|
+
|
|
258
|
+
# Returns a new Time representing the end of the year (end of the 31st of december)
|
|
259
|
+
def end_of_year
|
|
260
|
+
change(:month => 12, :day => 31, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
|
|
261
|
+
end
|
|
262
|
+
alias :at_end_of_year :end_of_year
|
|
263
|
+
|
|
264
|
+
# Convenience method which returns a new Time representing the time 1 day ago
|
|
265
|
+
def yesterday
|
|
266
|
+
advance(:days => -1)
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
# Convenience method which returns a new Time representing the time 1 day since the instance time
|
|
270
|
+
def tomorrow
|
|
271
|
+
advance(:days => 1)
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
def plus_with_duration(other) #:nodoc:
|
|
275
|
+
if PassiveSupport::Duration === other
|
|
276
|
+
other.since(self)
|
|
277
|
+
else
|
|
278
|
+
plus_without_duration(other)
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
def minus_with_duration(other) #:nodoc:
|
|
283
|
+
if PassiveSupport::Duration === other
|
|
284
|
+
other.until(self)
|
|
285
|
+
else
|
|
286
|
+
minus_without_duration(other)
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
# Time#- can also be used to determine the number of seconds between two Time instances.
|
|
291
|
+
# We're layering on additional behavior so that PassiveSupport::TimeWithZone instances
|
|
292
|
+
# are coerced into values that Time#- will recognize
|
|
293
|
+
def minus_with_coercion(other)
|
|
294
|
+
other = other.comparable_time if other.respond_to?(:comparable_time)
|
|
295
|
+
other.is_a?(::DateTime) ? to_f - other.to_f : minus_without_coercion(other)
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
# Layers additional behavior on Time#<=> so that DateTime and PassiveSupport::TimeWithZone instances
|
|
299
|
+
# can be chronologically compared with a Time
|
|
300
|
+
def compare_with_coercion(other)
|
|
301
|
+
# if other is an PassiveSupport::TimeWithZone, coerce a Time instance from it so we can do <=> comparison
|
|
302
|
+
other = other.comparable_time if other.respond_to?(:comparable_time)
|
|
303
|
+
if other.acts_like?(:date)
|
|
304
|
+
# other is a Date/DateTime, so coerce self #to_datetime and hand off to DateTime#<=>
|
|
305
|
+
to_datetime.compare_without_coercion(other)
|
|
306
|
+
else
|
|
307
|
+
compare_without_coercion(other)
|
|
308
|
+
end
|
|
309
|
+
end
|
|
310
|
+
end
|
|
311
|
+
end
|
|
312
|
+
end
|
|
313
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
module PassiveSupport #:nodoc:
|
|
2
|
+
module CoreExtensions #:nodoc:
|
|
3
|
+
module Time #:nodoc:
|
|
4
|
+
# Converting times to formatted strings, dates, and datetimes.
|
|
5
|
+
module Conversions
|
|
6
|
+
DATE_FORMATS = {
|
|
7
|
+
:db => "%Y-%m-%d %H:%M:%S",
|
|
8
|
+
:number => "%Y%m%d%H%M%S",
|
|
9
|
+
:time => "%H:%M",
|
|
10
|
+
:short => "%d %b %H:%M",
|
|
11
|
+
:long => "%B %d, %Y %H:%M",
|
|
12
|
+
:long_ordinal => lambda { |time| time.strftime("%B #{time.day.ordinalize}, %Y %H:%M") },
|
|
13
|
+
:rfc822 => lambda { |time| time.strftime("%a, %d %b %Y %H:%M:%S #{time.formatted_offset(false)}") }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
def self.included(base) #:nodoc:
|
|
17
|
+
base.class_eval do
|
|
18
|
+
alias_method :to_default_s, :to_s
|
|
19
|
+
alias_method :to_s, :to_formatted_s
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Converts to a formatted string. See DATE_FORMATS for builtin formats.
|
|
24
|
+
#
|
|
25
|
+
# This method is aliased to <tt>to_s</tt>.
|
|
26
|
+
#
|
|
27
|
+
# time = Time.now # => Thu Jan 18 06:10:17 CST 2007
|
|
28
|
+
#
|
|
29
|
+
# time.to_formatted_s(:time) # => "06:10:17"
|
|
30
|
+
# time.to_s(:time) # => "06:10:17"
|
|
31
|
+
#
|
|
32
|
+
# time.to_formatted_s(:db) # => "2007-01-18 06:10:17"
|
|
33
|
+
# time.to_formatted_s(:number) # => "20070118061017"
|
|
34
|
+
# time.to_formatted_s(:short) # => "18 Jan 06:10"
|
|
35
|
+
# time.to_formatted_s(:long) # => "January 18, 2007 06:10"
|
|
36
|
+
# time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10"
|
|
37
|
+
# time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
|
|
38
|
+
#
|
|
39
|
+
# == Adding your own time formats to +to_formatted_s+
|
|
40
|
+
# You can add your own formats to the Time::DATE_FORMATS hash.
|
|
41
|
+
# Use the format name as the hash key and either a strftime string
|
|
42
|
+
# or Proc instance that takes a time argument as the value.
|
|
43
|
+
#
|
|
44
|
+
# # config/initializers/time_formats.rb
|
|
45
|
+
# Time::DATE_FORMATS[:month_and_year] = "%B %Y"
|
|
46
|
+
# Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") }
|
|
47
|
+
def to_formatted_s(format = :default)
|
|
48
|
+
return to_default_s unless formatter = DATE_FORMATS[format]
|
|
49
|
+
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Returns the UTC offset as an +HH:MM formatted string.
|
|
53
|
+
#
|
|
54
|
+
# Time.local(2000).formatted_offset # => "-06:00"
|
|
55
|
+
# Time.local(2000).formatted_offset(false) # => "-0600"
|
|
56
|
+
def formatted_offset(colon = true, alternate_utc_string = nil)
|
|
57
|
+
utc? && alternate_utc_string || utc_offset.to_utc_offset_s(colon)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Converts a Time object to a Date, dropping hour, minute, and second precision.
|
|
61
|
+
#
|
|
62
|
+
# my_time = Time.now # => Mon Nov 12 22:59:51 -0500 2007
|
|
63
|
+
# my_time.to_date # => Mon, 12 Nov 2007
|
|
64
|
+
#
|
|
65
|
+
# your_time = Time.parse("1/13/2009 1:13:03 P.M.") # => Tue Jan 13 13:13:03 -0500 2009
|
|
66
|
+
# your_time.to_date # => Tue, 13 Jan 2009
|
|
67
|
+
def to_date
|
|
68
|
+
::Date.new(year, month, day)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# A method to keep Time, Date and DateTime instances interchangeable on conversions.
|
|
72
|
+
# In this case, it simply returns +self+.
|
|
73
|
+
def to_time
|
|
74
|
+
self
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Converts a Time instance to a Ruby DateTime instance, preserving UTC offset.
|
|
78
|
+
#
|
|
79
|
+
# my_time = Time.now # => Mon Nov 12 23:04:21 -0500 2007
|
|
80
|
+
# my_time.to_datetime # => Mon, 12 Nov 2007 23:04:21 -0500
|
|
81
|
+
#
|
|
82
|
+
# your_time = Time.parse("1/13/2009 1:13:03 P.M.") # => Tue Jan 13 13:13:03 -0500 2009
|
|
83
|
+
# your_time.to_datetime # => Tue, 13 Jan 2009 13:13:03 -0500
|
|
84
|
+
def to_datetime
|
|
85
|
+
::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400))
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|