Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/thin_out_backups/time_fixes.rb,
lib/thin_out_backups/time_fixes.rb

Overview

Until facets gets patched proper, we’ll monkey patch it here: TODO: Is this still needed? require ‘/home/tyler/dev/ruby/facets/lib/core/facets/time/hence’

Instance Method Summary collapse

Instance Method Details

#ago(number, units = :seconds) ⇒ Object

Returns a new Time representing the time a number of time-units ago.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/thin_out_backups/time_fixes.rb', line 16

def ago(number, units=:seconds)
  time =(
    case units.to_s.downcase.to_sym
    when :years
      set(:year => (year - number))
    when :months
      y = ((month - number) / 12).to_i
      #puts "(#{month} - #{number}) / 12 == #{y}"

      new_month = ((month - number - 1) % 12) + 1
      y += 1 if new_month > month
      #puts y

      set(:year => (year - y), :month => new_month)
    when :weeks
      self - (number * 604800)
    when :days
      self - (number * 86400)
    when :hours
      self - (number * 3600)
    when :minutes
      self - (number * 60)
    when :seconds, nil
      self - number
    else
      raise ArgumentError, "unrecognized time units -- #{units}"
    end
  )
  dst_adjustment(time)
end

#beginning_of_dayObject Also known as: midnight

/usr/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_support/core_ext/time/calculations.rb



118
119
120
# File 'lib/thin_out_backups/time_fixes.rb', line 118

def beginning_of_day
  change(:hour => 0)
end

#beginning_of_weekObject Also known as: sunday

Returns a new Time representing the “start” of this week (Sunday, 0:00)



124
125
126
127
# File 'lib/thin_out_backups/time_fixes.rb', line 124

def beginning_of_week
  days_to_sunday = self.wday
  self.ago(days_to_sunday, :days).midnight
end

#dst_adjustment(time) ⇒ Object

Adjust DST

TODO: Can’t seem to get this to pass ActiveSupport tests. Even though it is essentially identical to the ActiveSupport code (see Time#since in time/calculations.rb). It handles all but 4 tests.



85
86
87
88
89
90
91
92
93
94
# File 'lib/thin_out_backups/time_fixes.rb', line 85

def dst_adjustment(time)
  self_dst = self.dst? ? 1 : 0
  time_dst = time.dst? ? 1 : 0
  seconds  = (self - time).abs
  if (seconds >= 86400 && self_dst != time_dst)
    time + ((self_dst - time_dst) * 60 * 60)
  else
    time
  end
end

#to_sObject



108
109
110
111
# File 'lib/thin_out_backups/time_fixes.rb', line 108

def to_s
  #strftime("%Y%m%dT%H%M%S")
  strftime("%Y-%m-%d %H:%M")
end

#to_s_fullObject



112
113
114
# File 'lib/thin_out_backups/time_fixes.rb', line 112

def to_s_full
  strftime("%Y-%m-%d %H:%M:%S")
end