Class: Time

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

Overview

Some codes extracted from Rails, but modified codes and additional codes

Constant Summary collapse

DATE_FORMATS =
{
  :default => "%Y-%m-%d %H:%M:%S"
}
DAYS_INTO_WEEK =
{
  :monday     => 0,
  :tuesday    => 1,
  :wednesday  => 2,
  :thursday   => 3,
  :friday     => 4,
  :saturday   => 5,
  :sunday     => 6 
}

Instance Method Summary collapse

Instance Method Details

#advance(options) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/small/time.rb', line 30

def advance(options)
  seconds = (options[:years] || 0).years +
    (options[:months] || 0).months +
    (options[:weeks] || 0).weeks + 
    (options[:days] || 0).days +
    (options[:hours] || 0).hours +
    (options[:minutes] || 0).minutes +
    (options[:seconds] || 0).seconds 
  since(seconds)
end

#ago(seconds) ⇒ Object



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

def ago(seconds)
  since(-seconds)
end

#beginning_of_business_week(start_day = :monday) ⇒ Object



111
112
113
# File 'lib/small/time.rb', line 111

def beginning_of_business_week(start_day = :monday)
  business_week(start_day).first
end

#beginning_of_dayObject Also known as: midnight



119
120
121
# File 'lib/small/time.rb', line 119

def beginning_of_day
  change(:hour => 0)
end

#beginning_of_monthObject



129
130
131
# File 'lib/small/time.rb', line 129

def beginning_of_month
  change(:day => 1, :hour => 0)
end

#beginning_of_week(start_day = :monday) ⇒ Object



124
125
126
127
# File 'lib/small/time.rb', line 124

def beginning_of_week(start_day = :monday)
  days_to_start = days_to_week_start(start_day)
  (self - days_to_start.days).midnight
end

#beginning_of_yearObject



133
134
135
# File 'lib/small/time.rb', line 133

def beginning_of_year
  change(:month => 1, :day => 1, :hour => 0)
end

#business_week(start_day = :monday) ⇒ Object



105
106
107
108
109
# File 'lib/small/time.rb', line 105

def business_week(start_day = :monday)
  days = []
  5.times {|i| days << beginning_of_week(start_day).since(i.days) }
  days
end

#change(options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/small/time.rb', line 41

def change(options)
  ::Time.send(
    utc? ? :utc : :local,
    options[:year]  || year,
    options[:month] || month,
    options[:day]   || day,
    options[:hour]  || hour,
    options[:min]   || (options[:hour] ? 0 : min),
    options[:sec]   || ((options[:hour] || options[:min]) ? 0 : sec),
    options[:usec]  || ((options[:hour] || options[:min] || options[:sec]) ? 0 : usec)
  )
end

#days_in_monthObject



90
91
92
# File 'lib/small/time.rb', line 90

def days_in_month
  ::Date.civil(year, month, -1).day
end

#days_to_week_start(start_day = :monday) ⇒ Object



94
95
96
97
98
99
# File 'lib/small/time.rb', line 94

def days_to_week_start(start_day = :monday)
  start_day_number = DAYS_INTO_WEEK[start_day]
  current_day_number = wday != 0 ? wday - 1 : 6
  days_span = current_day_number - start_day_number
  days_span >= 0 ? days_span : 7 + days_span
end

#end_of_business_week(start_day = :monday) ⇒ Object



115
116
117
# File 'lib/small/time.rb', line 115

def end_of_business_week(start_day = :monday)
  business_week(start_day).last
end

#end_of_dayObject



137
138
139
# File 'lib/small/time.rb', line 137

def end_of_day
  change(:hour => 23, :min => 59, :sec => 59, :usec => 999999.99)
end

#end_of_monthObject



146
147
148
# File 'lib/small/time.rb', line 146

def end_of_month
  change(:day => days_in_month, :hour => 23, :min => 59, :sec => 59, :usec => 999999.99)
end

#end_of_week(start_day = :monday) ⇒ Object



141
142
143
144
# File 'lib/small/time.rb', line 141

def end_of_week(start_day = :monday)
  days_to_end =  6 - days_to_week_start(start_day)
  (self + days_to_end.days).end_of_day
end

#end_of_yearObject



150
151
152
# File 'lib/small/time.rb', line 150

def end_of_year
  change(:month => 12, :day => 31, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
end

#future?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/small/time.rb', line 82

def future?
  self > ::Time.now
end

#last_monthObject



174
175
176
# File 'lib/small/time.rb', line 174

def last_month
  advance(:months => -1)
end

#last_weekObject



182
183
184
# File 'lib/small/time.rb', line 182

def last_week
  advance(:weeks => -1)
end

#last_yearObject



166
167
168
# File 'lib/small/time.rb', line 166

def last_year
  advance(:years => -1)
end

#leap_year?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/small/time.rb', line 70

def leap_year?
  to_date.leap?
end

#next_monthObject



170
171
172
# File 'lib/small/time.rb', line 170

def next_month
  advance(:months => 1)
end

#next_weekObject



178
179
180
# File 'lib/small/time.rb', line 178

def next_week
  advance(:weeks => 1)
end

#next_yearObject



162
163
164
# File 'lib/small/time.rb', line 162

def next_year
  advance(:years => 1)
end

#past?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/small/time.rb', line 86

def past?
  self < ::Time.now
end

#since(seconds) ⇒ Object



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

def since(seconds)
  self + seconds
end

#to_dateObject



54
55
56
# File 'lib/small/time.rb', line 54

def to_date
  ::Date.new(year, month, day)
end

#to_datetimeObject



58
59
60
# File 'lib/small/time.rb', line 58

def to_datetime
  ::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400))
end

#to_formatted_s(format = :default) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/small/time.rb', line 62

def to_formatted_s(format = :default)
  if formatter = DATE_FORMATS[format]
    strftime(formatter)
  else
    self.to_s
  end
end

#today?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/small/time.rb', line 78

def today?
  to_date == ::Time.now.to_date
end

#tomorrowObject



154
155
156
# File 'lib/small/time.rb', line 154

def tomorrow
  advance(:days => 1)
end

#weekend?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/small/time.rb', line 74

def weekend?
  weekends.include?(self)
end

#weekendsObject



101
102
103
# File 'lib/small/time.rb', line 101

def weekends
  [end_of_week.ago(1.day), end_of_week]
end

#yesterdayObject



158
159
160
# File 'lib/small/time.rb', line 158

def yesterday
  advance(:days => -1)
end