Skip to content

Instantly share code, notes, and snippets.

@daicorrea-tw
Created February 14, 2022 21:05
Show Gist options
  • Save daicorrea-tw/e33ca4ec75d80e4968e0f19f19b835ce to your computer and use it in GitHub Desktop.
Save daicorrea-tw/e33ca4ec75d80e4968e0f19f19b835ce to your computer and use it in GitHub Desktop.
Ruby date
class UsageInterval
def initialize(date)
day_of_the_week = date.cwday
@end = date - day_of_the_week
@begin = @end - 7
end
def self.usageIntervalFromPreviousWeek date = Date.today
UsageInterval.new date
end
def begin
@begin.to_time.to_i
end
def end
@end.to_time.to_i
end
end
require 'rspec'
require 'date'
require_relative '../../lib/domain/usage_interval'
describe UsageInterval do
it 'should return the current week start as interval ending' do
date = Date.new 2021, 9, 25 # 2021-09-25
interval_end_date = UsageInterval::usageIntervalFromPreviousWeek(date).end
expect(interval_end_date).to eq(Date.new(2021, 9, 19).to_time.to_i) #2021-09-19
end
it 'should return the previous week start as interval start' do
date = Date.new 2021, 9, 25# 2021-09-25
interval_begin_date = UsageInterval::usageIntervalFromPreviousWeek(date).begin
expect(interval_begin_date).to eq(Date.new(2021, 9, 12).to_time.to_i) # 2021-09-12
end
it 'should return the previous week end even when the date is sunday' do
date = Date.new 2021, 9, 19# 2021-09-25
interval_end_date = UsageInterval::usageIntervalFromPreviousWeek(date).end
expect(interval_end_date).to eq(Date.new(2021, 9, 12).to_time.to_i) # 2021-09-12
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment