Module: Time::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/time/utils.rb,
lib/time/utils/version.rb

Constant Summary collapse

COMMON_YEAR_DAYS_IN_MONTH =
[nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#advance(time, options) ⇒ Object



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

def advance(time, options)
  unless options[:weeks].nil?
    options[:weeks], partial_weeks = options[:weeks].divmod(1)
    options[:days] = (options[:days] || 0) + 7 * partial_weeks
  end

  unless options[:days].nil?
    options[:days], partial_days = options[:days].divmod(1)
    options[:hours] = (options[:hours] || 0) + 24 * partial_days
  end

  d = Date::Utils.advance(to_date(time), options)
  time_advanced_by_date = change(time, :year => d.year, :month => d.month, :day => d.day)
  seconds_to_advance = (options[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600
  seconds_to_advance == 0 ? time_advanced_by_date : since(time_advanced_by_date, seconds_to_advance)
end

#ago(time, seconds) ⇒ Object

Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension



62
63
64
# File 'lib/time/utils.rb', line 62

def ago(time, seconds)
  since(time, -seconds)
end

#beginning_of_day(time) ⇒ Object Also known as: midnight, at_midnight, at_beginning_of_day

Returns a new Time representing the start of the day (0:00)



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

def beginning_of_day(time)
  #(self - seconds_since_midnight).change(:usec => 0)
  change(time, :hour => 0)
end

#beginning_of_hour(time) ⇒ Object Also known as: at_beginning_of_hour

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



132
133
134
# File 'lib/time/utils.rb', line 132

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

#beginning_of_month(time) ⇒ Object Also known as: at_beginning_of_month

Returns a new Time representing the start of the month (1st of the month, 0:00)



148
149
150
151
# File 'lib/time/utils.rb', line 148

def beginning_of_month(time)
  #self - ((self.mday-1).days + self.seconds_since_midnight)
  change(time, :day => 1, :hour => 0)
end

#beginning_of_year(time) ⇒ Object Also known as: at_beginning_of_year

Returns a new Time representing the start of the year (1st of january, 0:00)



163
164
165
# File 'lib/time/utils.rb', line 163

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

#change(time, options) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/time/utils.rb', line 31

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

#days_in_month(month, year = now.year) ⇒ Object



9
10
11
12
# File 'lib/time/utils.rb', line 9

def days_in_month(month, year = now.year)
  return 29 if month == 2 && ::Date.gregorian_leap?(year)
  COMMON_YEAR_DAYS_IN_MONTH[month]
end

#end_of_day(time) ⇒ Object

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



127
128
129
# File 'lib/time/utils.rb', line 127

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

#end_of_hour(time) ⇒ Object

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



138
139
140
141
142
143
144
145
# File 'lib/time/utils.rb', line 138

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

#end_of_month(time) ⇒ Object Also known as: at_end_of_month

Returns a new Time representing the end of the month (end of the last day of the month)



155
156
157
158
159
# File 'lib/time/utils.rb', line 155

def end_of_month(time)
  #self - ((self.mday-1).days + self.seconds_since_midnight)
  last_day = days_in_month(time.month, time.year)
  change(time, :day => last_day, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
end

#end_of_year(time) ⇒ Object Also known as: at_end_of_year

Returns a new Time representing the end of the year (end of the 31st of december)



169
170
171
# File 'lib/time/utils.rb', line 169

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

#local_time(*args) ⇒ Object

Wraps class method time_with_datetime_fallback with utc_or_local set to :local.



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

def local_time(*args)
  time_with_datetime_fallback(:local, *args)
end

#months_ago(time, months) ⇒ Object

Returns a new Time representing the time a number of specified months ago



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

def months_ago(time, months)
  advance(time, :months => -months)
end

#months_since(time, months) ⇒ Object

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



83
84
85
# File 'lib/time/utils.rb', line 83

def months_since(time, months)
  advance(time, :months => months)
end

#next_month(time) ⇒ Object

Short-hand for months_since(1)



113
114
115
# File 'lib/time/utils.rb', line 113

def next_month(time)
  months_since(time, 1)
end

#next_year(time) ⇒ Object

Short-hand for years_since(1)



103
104
105
# File 'lib/time/utils.rb', line 103

def next_year(time)
  years_since(time, 1)
end

#prev_month(time) ⇒ Object

Short-hand for months_ago(1)



108
109
110
# File 'lib/time/utils.rb', line 108

def prev_month(time)
  months_ago(time, 1)
end

#prev_year(time) ⇒ Object

Short-hand for years_ago(1)



98
99
100
# File 'lib/time/utils.rb', line 98

def prev_year(time)
  years_ago(time, 1)
end

#since(time, seconds) ⇒ Object Also known as: in

Returns a new Time representing the time a number of seconds since the instance time



67
68
69
# File 'lib/time/utils.rb', line 67

def since(time, seconds)
  time + seconds
end

#time_with_datetime_fallback(utc_or_local, year, month = 1, day = 1, hour = 0, min = 0, sec = 0, usec = 0) ⇒ Object



14
15
16
17
18
19
# File 'lib/time/utils.rb', line 14

def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0)
  time = ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec)
  # This check is needed because Time.utc(y) returns a time object in the 2000s for 0 <= y <= 138.
  time.year == year ? time : ::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec)
  time
end

#to_date(time) ⇒ Object

Conversions



186
187
188
# File 'lib/time/utils.rb', line 186

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

#tomorrow(time) ⇒ Object

Convenience method which returns a new Time representing the time 1 day since the instance time



180
181
182
# File 'lib/time/utils.rb', line 180

def tomorrow(time)
  advance(time, :days => 1)
end

#utc_time(*args) ⇒ Object

Wraps class method time_with_datetime_fallback with utc_or_local set to :utc.



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

def utc_time(*args)
  time_with_datetime_fallback(:utc, *args)
end

#weeks_ago(time, weeks) ⇒ Object

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



73
74
75
# File 'lib/time/utils.rb', line 73

def weeks_ago(time, weeks)
  advance(time, :weeks => -weeks)
end

#years_ago(time, years) ⇒ Object

Returns a new Time representing the time a number of specified years ago



88
89
90
# File 'lib/time/utils.rb', line 88

def years_ago(time, years)
  advance(time, :years => -years)
end

#years_since(time, years) ⇒ Object

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



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

def years_since(time, years)
  advance(time, :years => years)
end

#yesterday(time) ⇒ Object

Convenience method which returns a new Time representing the time 1 day ago



175
176
177
# File 'lib/time/utils.rb', line 175

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