Class: Attio::Util::TimePeriod

Inherits:
Object
  • Object
show all
Defined in:
lib/attio/util/time_period.rb

Overview

Utility class for time period calculations

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_date, end_date) ⇒ TimePeriod

Returns a new instance of TimePeriod.



11
12
13
14
# File 'lib/attio/util/time_period.rb', line 11

def initialize(start_date, end_date)
  @start_date = parse_date(start_date)
  @end_date = parse_date(end_date)
end

Instance Attribute Details

#end_dateObject (readonly)

Returns the value of attribute end_date.



9
10
11
# File 'lib/attio/util/time_period.rb', line 9

def end_date
  @end_date
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



9
10
11
# File 'lib/attio/util/time_period.rb', line 9

def start_date
  @start_date
end

Class Method Details

.between(start_date, end_date) ⇒ Object

Custom range



144
145
146
# File 'lib/attio/util/time_period.rb', line 144

def self.between(start_date, end_date)
  new(start_date, end_date)
end

.current_monthObject

Current month (full month, not MTD)



87
88
89
90
# File 'lib/attio/util/time_period.rb', line 87

def self.current_month
  today = Date.today
  month(today.year, today.month)
end

.current_quarterObject

Current quarter (full quarter, not QTD)



62
63
64
65
# File 'lib/attio/util/time_period.rb', line 62

def self.current_quarter
  today = Date.today
  quarter(today.year, (today.month - 1) / 3 + 1)
end

.current_yearObject

Current year (full year, not YTD)



108
109
110
# File 'lib/attio/util/time_period.rb', line 108

def self.current_year
  year(Date.today.year)
end

.last_30_daysObject

Last 30 days



129
130
131
# File 'lib/attio/util/time_period.rb', line 129

def self.last_30_days
  last_days(30)
end

.last_90_daysObject

Last 90 days



134
135
136
# File 'lib/attio/util/time_period.rb', line 134

def self.last_90_days
  last_days(90)
end

.last_days(num_days) ⇒ Object

Last N days (including today)



118
119
120
121
# File 'lib/attio/util/time_period.rb', line 118

def self.last_days(num_days)
  today = Date.today
  new(today - num_days + 1, today)
end

.last_weekObject

Last 7 days



124
125
126
# File 'lib/attio/util/time_period.rb', line 124

def self.last_week
  last_days(7)
end

.last_year_rollingObject

Last 365 days



139
140
141
# File 'lib/attio/util/time_period.rb', line 139

def self.last_year_rolling
  last_days(365)
end

.month(year, month_num) ⇒ Object

Specific month

Raises:

  • (ArgumentError)


79
80
81
82
83
84
# File 'lib/attio/util/time_period.rb', line 79

def self.month(year, month_num)
  raise ArgumentError, "Month must be between 1 and 12" unless (1..12).cover?(month_num)
  month_start = Date.new(year, month_num, 1)
  month_end = (month_start >> 1) - 1
  new(month_start, month_end)
end

.month_to_dateObject

Current month to date



40
41
42
43
# File 'lib/attio/util/time_period.rb', line 40

def self.month_to_date
  today = Date.today
  new(Date.new(today.year, today.month, 1), today)
end

.previous_monthObject

Previous month



93
94
95
96
97
98
99
100
# File 'lib/attio/util/time_period.rb', line 93

def self.previous_month
  today = Date.today
  if today.month == 1
    month(today.year - 1, 12)
  else
    month(today.year, today.month - 1)
  end
end

.previous_quarterObject

Previous quarter



68
69
70
71
72
73
74
75
76
# File 'lib/attio/util/time_period.rb', line 68

def self.previous_quarter
  today = Date.today
  current_q = (today.month - 1) / 3 + 1
  if current_q == 1
    quarter(today.year - 1, 4)
  else
    quarter(today.year, current_q - 1)
  end
end

.previous_yearObject

Previous year



113
114
115
# File 'lib/attio/util/time_period.rb', line 113

def self.previous_year
  year(Date.today.year - 1)
end

.quarter(year, quarter_num) ⇒ Object

Specific quarter

Raises:

  • (ArgumentError)


54
55
56
57
58
59
# File 'lib/attio/util/time_period.rb', line 54

def self.quarter(year, quarter_num)
  raise ArgumentError, "Quarter must be between 1 and 4" unless (1..4).cover?(quarter_num)
  quarter_start = Date.new(year, (quarter_num - 1) * 3 + 1, 1)
  quarter_end = (quarter_start >> 3) - 1
  new(quarter_start, quarter_end)
end

.quarter_to_dateObject

Current quarter to date



46
47
48
49
50
51
# File 'lib/attio/util/time_period.rb', line 46

def self.quarter_to_date
  today = Date.today
  quarter = (today.month - 1) / 3 + 1
  quarter_start = Date.new(today.year, (quarter - 1) * 3 + 1, 1)
  new(quarter_start, today)
end

.year(year_num) ⇒ Object

Specific year



103
104
105
# File 'lib/attio/util/time_period.rb', line 103

def self.year(year_num)
  new(Date.new(year_num, 1, 1), Date.new(year_num, 12, 31))
end

.year_to_dateObject

Current year to date



34
35
36
37
# File 'lib/attio/util/time_period.rb', line 34

def self.year_to_date
  today = Date.today
  new(Date.new(today.year, 1, 1), today)
end

Instance Method Details

#daysObject

Number of days in the period



162
163
164
# File 'lib/attio/util/time_period.rb', line 162

def days
  (@end_date - @start_date).to_i + 1
end

#includes?(date) ⇒ Boolean

Check if a date falls within this period

Returns:

  • (Boolean)


151
152
153
154
# File 'lib/attio/util/time_period.rb', line 151

def includes?(date)
  date = date.to_date
  date.between?(@start_date, @end_date)
end

#labelObject

Human-readable label



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/attio/util/time_period.rb', line 176

def label
  today = Date.today

  # Check for common patterns
  if @start_date == Date.new(today.year, 1, 1) && @end_date == today
    "Year to Date"
  elsif @start_date == Date.new(today.year, today.month, 1) && @end_date == today
    "Month to Date"
  elsif @start_date == Date.new(today.year, 1, 1) && @end_date == Date.new(today.year, 12, 31)
    today.year.to_s
  elsif @start_date.day == 1 && @end_date == (@start_date >> 1) - 1
    @start_date.strftime("%B %Y")
  elsif days == 7 && @end_date == today
    "Last 7 Days"
  elsif days == 30 && @end_date == today
    "Last 30 Days"
  elsif days == 90 && @end_date == today
    "Last 90 Days"
  else
    # Check for quarters
    quarter = detect_quarter
    return quarter if quarter

    to_s
  end
end

#to_rangeObject

Get the date range



157
158
159
# File 'lib/attio/util/time_period.rb', line 157

def to_range
  @start_date..@end_date
end

#to_sObject

String representation



167
168
169
170
171
172
173
# File 'lib/attio/util/time_period.rb', line 167

def to_s
  if @start_date == @end_date
    @start_date.to_s
  else
    "#{@start_date} to #{@end_date}"
  end
end