Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/time.rb

Instance Method Summary collapse

Instance Method Details

#beginning_of(secs = 1.day) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/time.rb', line 43

def beginning_of(secs=1.day)
  case secs
  when 1.hour
    self.beginning_of_hour
  when 1.day
    self.beginning_of_day
  when 1.week
    self.beginning_of_week
  when 1.month
    self.beginning_of_month
  when 1.year
    self.beginning_of_year
  else
    if secs < 1.day
      self.beginning_of_period(secs)
    else
      return self
    end
  end
end

#beginning_of_dayObject



20
21
22
# File 'lib/time.rb', line 20

def beginning_of_day
  self-(self.hour).hours-(self.min).minutes-self.sec
end

#beginning_of_hourObject



26
27
28
29
# File 'lib/time.rb', line 26

def beginning_of_hour
  self-(self.min).minutes-self.sec
  # Time.at((self.to_i/1.hour).floor*1.hour)
end

#beginning_of_minuteObject

Time.at((self.to_i/1.hour).floor*1.hour+1.hour)



34
35
36
37
# File 'lib/time.rb', line 34

def beginning_of_minute
  self-self.sec
  # Time.at((self.to_i/1.minute).floor*1.minute)
end

#beginning_of_monthObject



8
9
10
# File 'lib/time.rb', line 8

def beginning_of_month
  self-(self.mday-1).days-(self.hour).hours-(self.min).minutes-self.sec
end

#beginning_of_period(secs = 5.minutes) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/time.rb', line 85

def beginning_of_period(secs=5.minutes)
  return self if secs >= 1.day/2
  secs_today = self - self.beginning_of_day
  periods_today = (secs_today/secs).floor
  self.beginning_of_day + periods_today * secs
  # Time.at((self.to_i/seconds).floor*seconds)
end

#beginning_of_weekObject



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

def beginning_of_week
  self-(self.wday).days-(self.hour).hours-(self.min).minutes-self.sec
end

#beginning_of_yearObject



2
3
4
# File 'lib/time.rb', line 2

def beginning_of_year
  self-(self.yday-1).days-(self.hour).hours-(self.min).minutes-self.sec
end

#end_of(secs = 1.day) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/time.rb', line 64

def end_of(secs=1.day)
  case secs
  when 1.hour
    self.end_of_hour
  when 1.day
    self.end_of_day
  when 1.week
    self.end_of_week
  when 1.month
    self.end_of_month
  when 1.year
    self.end_of_year
  else
    if secs < 1.day
      self.end_of_period(secs)
    else
      return self
    end
  end
end

#end_of_dayObject



23
24
25
# File 'lib/time.rb', line 23

def end_of_day
  self.beginning_of_day + 24.hours - 1.second
end

#end_of_hourObject

Time.at((self.to_i/1.hour).floor*1.hour)



30
31
32
33
# File 'lib/time.rb', line 30

def end_of_hour
  self.beginning_of_hour + 1.hour - 1.second
  # Time.at((self.to_i/1.hour).floor*1.hour+1.hour)
end

#end_of_minuteObject

Time.at((self.to_i/1.minute).floor*1.minute)



38
39
40
41
# File 'lib/time.rb', line 38

def end_of_minute
  self.beginning_of_minute + 1.minute - 1.second
  # Time.at((self.to_i/1.minute).floor*5.minute)
end

#end_of_monthObject



11
12
13
# File 'lib/time.rb', line 11

def end_of_month
  self.beginning_of_month + 1.month - 1.second
end

#end_of_period(secs = 5.minutes) ⇒ Object

Time.at((self.to_i/seconds).floor*seconds)



92
93
94
95
96
# File 'lib/time.rb', line 92

def end_of_period(secs=5.minutes)
  return self if secs >= 1.day/2
  beginning_of(secs) + secs - 1.second
  # Time.at((self.to_i/seconds).floor*seconds+seconds)
end

#end_of_weekObject



17
18
19
# File 'lib/time.rb', line 17

def end_of_week
  self.beginning_of_week + 1.week - 1.second
end

#end_of_yearObject



5
6
7
# File 'lib/time.rb', line 5

def end_of_year
  self.beginning_of_year + 365.days - 1.second
end

#upto(max, interval = 1.day, inclusive = true) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/time.rb', line 98

def upto(max, interval=1.day, inclusive=true)
  t = self
  while t < max || (inclusive && t <= max)
    yield t
    t += interval
  end
end