Class: Time

Inherits:
Object
  • Object
show all
Includes:
TimeExt::MethodChain
Defined in:
lib/time_ext/core_ext/time/iterations.rb,
lib/time_ext/core_ext/time/calculations.rb

Instance Method Summary (collapse)

Methods included from TimeExt::MethodChain

#add_to_chain, #call_chain

Instance Method Details

- (Object) beginning_of_each(unit, options = {}, &block)

Executes passed block for each “unit” of time specified, with a new time object set to the beginning of “unit” for each interval passed to the block.



34
35
36
# File 'lib/time_ext/core_ext/time/iterations.rb', line 34

def beginning_of_each(unit, options = {}, &block)
  iterate(unit, options.merge(:map_result => false, :beginning_of => true), &block)
end

- (Object) beginning_of_hour Also known as: at_beginning_of_hour

Returns a new Time representing the start of the hour (XX:00:00).



201
202
203
# File 'lib/time_ext/core_ext/time/calculations.rb', line 201

def beginning_of_hour
  change(:min => 0, :sec => 0, :usec => 0)
end

- (Object) beginning_of_minute Also known as: at_beginning_of_min, beginning_of_min, at_beginning_of_minute

Returns a new Time representing the start of the minute (XX:XX:00).



187
188
189
# File 'lib/time_ext/core_ext/time/calculations.rb', line 187

def beginning_of_minute
  change(:sec => 0, :usec => 0)
end

- (Object) beginning_of_second Also known as: at_beginning_of_sec, beginning_of_sec, at_beginning_of_second

Returns a new Time representing the start of the second, XX:XX:XX.000000 (.000000000 in ruby1.9).



173
174
175
# File 'lib/time_ext/core_ext/time/calculations.rb', line 173

def beginning_of_second
  change(:usec => 0)
end

- (Object) ceil(unit = :sec) Also known as: beginning_of_next, at_beginning_of_next

Returns a new Time representing the start of the next unit specified (second by default).



21
22
23
# File 'lib/time_ext/core_ext/time/calculations.rb', line 21

def ceil(unit = :sec)
  self.send("next_#{unit}").send("beginning_of_#{unit}")
end

- (Object) common_year_days_in_month

Helper method for backwards compatibility with ActiveSupport.



9
10
11
# File 'lib/time_ext/core_ext/time/calculations.rb', line 9

def common_year_days_in_month
  defined?(COMMON_YEAR_DAYS_IN_MONTH) ? COMMON_YEAR_DAYS_IN_MONTH : [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
end

- (Object) days_ago(days)

Returns a new Time representing the time a number of specified days ago.



133
134
135
# File 'lib/time_ext/core_ext/time/calculations.rb', line 133

def days_ago(days)
  ago(days.days)
end

- (Object) days_into_week

Helper method for backwards compatibility with ActiveSupport.



4
5
6
# File 'lib/time_ext/core_ext/time/calculations.rb', line 4

def days_into_week
  defined?(DAYS_INTO_WEEK) ? DAYS_INTO_WEEK : { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6 }
end

- (Object) days_since(days)

Returns a new Time representing the time a number of specified days in the future.



138
139
140
# File 'lib/time_ext/core_ext/time/calculations.rb', line 138

def days_since(days)
  since(days.days)
end

- (Object) each(unit, options = {}, &block)

Executes passed block for each “unit” of time specified, with a new time object for each interval passed to the block.



29
30
31
# File 'lib/time_ext/core_ext/time/iterations.rb', line 29

def each(unit, options = {}, &block)
  iterate(unit, options.merge(:map_result => false), &block)
end

- (Object) end_of(unit = :sec)

Returns a new Time representing the end of the unit specified (defaults to second).



168
169
170
# File 'lib/time_ext/core_ext/time/calculations.rb', line 168

def end_of(unit = :sec)
  send("end_of_#{unit}")
end

- (Object) end_of_hour

Returns a new Time representing the end of the hour, XX:59:59.999999 (.999999999 in ruby1.9).



207
208
209
# File 'lib/time_ext/core_ext/time/calculations.rb', line 207

def end_of_hour
  change(:min => 59, :sec => 59, :usec => 999999.999)
end

- (Object) end_of_minute Also known as: end_of_min

Returns a new Time representing the end of the hour, XX:XX:59.999999 (.999999999 in ruby1.9).



195
196
197
# File 'lib/time_ext/core_ext/time/calculations.rb', line 195

def end_of_minute
  change(:sec => 59, :usec => 999999.999)
end

- (Object) end_of_second Also known as: end_of_sec

Returns a new Time representing the end of the hour, XX:XX:XX.999999 (.999999999 in ruby1.9).



181
182
183
# File 'lib/time_ext/core_ext/time/calculations.rb', line 181

def end_of_second
  change(:usec => 999999.999)
end

- (Object) floor(unit = :sec) Also known as: at_beginning_of, beginning_of

Returns a new Time representing the start of the unit specified (second by default).



14
15
16
# File 'lib/time_ext/core_ext/time/calculations.rb', line 14

def floor(unit = :sec)
  self.send("beginning_of_#{unit}")
end

- (Object) hours_ago(hours)

Returns a new Time representing the time a number of specified hours ago.



123
124
125
# File 'lib/time_ext/core_ext/time/calculations.rb', line 123

def hours_ago(hours)
  ago(hours.hours)
end

- (Object) hours_since(hours)

Returns a new Time representing the time a number of specified hours in the future.



128
129
130
# File 'lib/time_ext/core_ext/time/calculations.rb', line 128

def hours_since(hours)
  since(hours.hours)
end

- (Object) iterate(unit, options = {}, &block)

Used by #each, #map_each and similar methods to iterate over ranges of time.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/time_ext/core_ext/time/iterations.rb', line 5

def iterate(unit, options = {}, &block)
  options.reverse_merge!(:map_result => false, :beginning_of => false, :include_start => false)
  if block_given?
    units = [:year, :month, :day, :hour, :min, :sec, :usec]
    parent_unit = units[units.index(unit)-1]
    @until ||= (!parent_unit.nil?) ? self.send("#{parent_unit}s_since", 1) : self.send("#{unit}s_since", 1)
    time = self.clone
    direction = (self < @until) ? :f : :b
    succ_method = (direction == :f) ? "next_#{unit}" : "prev_#{unit}"
    time = time.beginning_of(unit) if options[:beginning_of]
    time = time.send(succ_method) if !options[:include_start]
    results = []
    while (direction == :f && time <= @until) || (direction == :b && time >= @until)
      options[:map_result] ? results << yield(time) : yield(time)
      time = time.send(succ_method)
    end
    options[:map_result] ? results : self
  else
    add_to_chain(:iterate, unit, options)
    self
  end
end

- (Object) map_beginning_of_each(unit, options = {}, &block)

Executes passed block for each “unit” of time specified, returning an array with the return values from passed block. Additionally the time object passed into the block is set to the beginning of specified “unit”.



44
45
46
# File 'lib/time_ext/core_ext/time/iterations.rb', line 44

def map_beginning_of_each(unit, options = {}, &block)
  iterate(unit, options.merge(:map_result => true, :beginning_of => true), &block)
end

- (Object) map_each(unit, options = {}, &block)

Executes passed block for each “unit” of time specified, returning an array with the return values from the passed block.



39
40
41
# File 'lib/time_ext/core_ext/time/iterations.rb', line 39

def map_each(unit, options = {}, &block)
  iterate(unit, options.merge(:map_result => true), &block)
end

- (Object) minutes_ago(minutes) Also known as: mins_ago

Returns a new Time representing the time a number of specified minutes ago.



111
112
113
# File 'lib/time_ext/core_ext/time/calculations.rb', line 111

def minutes_ago(minutes)
  ago(minutes.minutes)
end

- (Object) minutes_since(minutes) Also known as: mins_since

Returns a new Time representing the time a number of specified minutes in the future.



117
118
119
# File 'lib/time_ext/core_ext/time/calculations.rb', line 117

def minutes_since(minutes)
  since(minutes.minutes)
end

- (Object) next(unit = :sec)

Returns a new Time representing the next unit specified (defaults to second).



41
42
43
# File 'lib/time_ext/core_ext/time/calculations.rb', line 41

def next(unit = :sec)
  send("next_#{unit}")
end

- (Object) next_day

Short-hand for days_since(1).



85
86
87
# File 'lib/time_ext/core_ext/time/calculations.rb', line 85

def next_day
  days_since(1)
end

- (Object) next_hour

Short-hand for hours_since(1).



75
76
77
# File 'lib/time_ext/core_ext/time/calculations.rb', line 75

def next_hour
  hours_since(1)
end

- (Object) next_minute Also known as: next_min

Short-hand for minutes_since(1).



64
65
66
# File 'lib/time_ext/core_ext/time/calculations.rb', line 64

def next_minute
  minutes_since(1)
end

- (Object) next_quarter

Short-hand for quarters_since(1).beginning_of_quarter.



95
96
97
# File 'lib/time_ext/core_ext/time/calculations.rb', line 95

def next_quarter
  quarters_since(1).beginning_of_quarter
end

- (Object) next_second Also known as: next_sec

Short-hand for seconds_since(1).



52
53
54
# File 'lib/time_ext/core_ext/time/calculations.rb', line 52

def next_second
  since(1)
end

- (Object) prev(unit = :sec)

Returns a new Time representing the previoius unit specified (defaults to second).



36
37
38
# File 'lib/time_ext/core_ext/time/calculations.rb', line 36

def prev(unit = :sec)
  send("prev_#{unit}")
end

- (Object) prev_day

Short-hand for days_ago(1).



80
81
82
# File 'lib/time_ext/core_ext/time/calculations.rb', line 80

def prev_day
  days_ago(1)
end

- (Object) prev_hour

Short-hand for hours_ago(1).



70
71
72
# File 'lib/time_ext/core_ext/time/calculations.rb', line 70

def prev_hour
  hours_ago(1)
end

- (Object) prev_minute Also known as: prev_min

Short-hand for minutes_ago(1).



58
59
60
# File 'lib/time_ext/core_ext/time/calculations.rb', line 58

def prev_minute
  minutes_ago(1)
end

- (Object) prev_quarter

Short-hand for quarters_ago(1).beginning_of_quarter.



100
101
102
# File 'lib/time_ext/core_ext/time/calculations.rb', line 100

def prev_quarter
  quarters_ago(1).beginning_of_quarter
end

- (Object) prev_second Also known as: prev_sec

Short-hand for seconds_ago(1).



46
47
48
# File 'lib/time_ext/core_ext/time/calculations.rb', line 46

def prev_second
  ago(1)
end

- (Object) prev_week(day = :monday)

Returns a new Time representing the start of the given day in the previous week (default is Monday).



90
91
92
# File 'lib/time_ext/core_ext/time/calculations.rb', line 90

def prev_week(day = :monday)
  weeks_ago(1).beginning_of_week.since(days_into_week[day].day).change(:hour => 0)
end

- (Object) quarters_ago(quarters)

Returns a new Time representing the time a number of specified quarters (3 months) ago.



153
154
155
# File 'lib/time_ext/core_ext/time/calculations.rb', line 153

def quarters_ago(quarters)
  ago((quarters * 3).months)
end

- (Object) quarters_since(quarters)

Returns a new Time representing the time a number of specified quarters (3 months) in the future.



158
159
160
# File 'lib/time_ext/core_ext/time/calculations.rb', line 158

def quarters_since(quarters)
  since((quarters * 3).months)
end

- (Object) round(unit = :sec) Also known as: beginning_of_closest

Returns a new Time representing the start of the current or next unit specified (second by default) depending which is closest



28
29
30
31
32
# File 'lib/time_ext/core_ext/time/calculations.rb', line 28

def round(unit = :sec)
  next_unit = self.ceil(unit)
  this_unit = self.floor(unit)
  (self - this_unit) < (next_unit - self) ? this_unit : next_unit
end

- (Object) until(time, &block) Also known as: till

Used togeter with #each to specify end of interation.



49
50
51
52
53
54
55
56
57
58
# File 'lib/time_ext/core_ext/time/iterations.rb', line 49

def until(time, &block)
  time = time.to_time if time.is_a?(::Date)
  @until = time
  if block_given?
    call_chain(block)
  else
    add_to_chain(:until, time)
    self
  end
end

- (Object) weeks_ago(weeks)

Returns a new Time representing the time a number of specified weeks ago.



143
144
145
# File 'lib/time_ext/core_ext/time/calculations.rb', line 143

def weeks_ago(weeks)
  ago(weeks.weeks)
end

- (Object) weeks_since(weeks)

Returns a new Time representing the time a number of specified weeks in the future.



148
149
150
# File 'lib/time_ext/core_ext/time/calculations.rb', line 148

def weeks_since(weeks)
  since(weeks.weeks)
end