Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add start_day parameter to Time#at_beginning_of_week #13446

Merged
31 changes: 31 additions & 0 deletions spec/std/time/time_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,37 @@ describe Time do
Time.utc(2014, 11, i).at_beginning_of_week.should eq Time.utc(2014, 11, 3)
end

sunday_day_of_week = Time::DayOfWeek::Sunday
Time.utc(2014, 11, 1).at_beginning_of_week(sunday_day_of_week).should eq Time.utc(2014, 10, 26)
2.upto(8) do |i|
Time.utc(2014, 11, i).at_beginning_of_week(sunday_day_of_week).should eq Time.utc(2014, 11, 2)
end
Time.utc(2014, 11, 9).at_beginning_of_week(sunday_day_of_week).should eq Time.utc(2014, 11, 9)

Time.utc(2014, 11, 1).at_beginning_of_week(:sunday).should eq Time.utc(2014, 10, 26)
2.upto(8) do |i|
Time.utc(2014, 11, i).at_beginning_of_week(:sunday).should eq Time.utc(2014, 11, 2)
end
Time.utc(2014, 11, 9).at_beginning_of_week(:sunday).should eq Time.utc(2014, 11, 9)

Time.utc(2014, 11, 10).at_beginning_of_week(Time::DayOfWeek::Sunday).should eq Time.utc(2014, 11, 9)
Time.utc(2014, 11, 10).at_beginning_of_week(Time::DayOfWeek::Monday).should eq Time.utc(2014, 11, 10)
Time.utc(2014, 11, 10).at_beginning_of_week(Time::DayOfWeek::Tuesday).should eq Time.utc(2014, 11, 4)
Time.utc(2014, 11, 10).at_beginning_of_week(Time::DayOfWeek::Wednesday).should eq Time.utc(2014, 11, 5)
Time.utc(2014, 11, 10).at_beginning_of_week(Time::DayOfWeek::Thursday).should eq Time.utc(2014, 11, 6)
Time.utc(2014, 11, 10).at_beginning_of_week(Time::DayOfWeek::Friday).should eq Time.utc(2014, 11, 7)
Time.utc(2014, 11, 10).at_beginning_of_week(Time::DayOfWeek::Saturday).should eq Time.utc(2014, 11, 8)

at_beginning_of_week_default = Time.local.at_beginning_of_week
at_beginning_of_week_default.hour.should eq 0
at_beginning_of_week_default.minute.should eq 0
at_beginning_of_week_default.second.should eq 0

at_beginning_of_week_sunday = Time.local.at_beginning_of_week(:sunday)
at_beginning_of_week_sunday.hour.should eq 0
at_beginning_of_week_sunday.minute.should eq 0
at_beginning_of_week_sunday.second.should eq 0

t1.at_beginning_of_day.should eq Time.utc(2014, 11, 25)
t1.at_beginning_of_hour.should eq Time.utc(2014, 11, 25, 10)
t1.at_beginning_of_minute.should eq Time.utc(2014, 11, 25, 10, 11)
Expand Down
12 changes: 10 additions & 2 deletions src/time.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1359,9 +1359,17 @@ struct Time

# Returns a copy of this `Time` representing the beginning of the week.
#
# The week starts on Monday by default, but can be configured by passing a different `start_day` as a `Time::DayOfWeek`.
#
# ```
# now = Time.utc(2023, 5, 16, 17, 53, 22)
# now.at_beginning_of_week # => 2023-05-15 00:00:00 UTC
# now.at_beginning_of_week(:sunday) # => 2023-05-14 00:00:00 UTC
# now.at_beginning_of_week(:wednesday) # => 2023-05-10 00:00:00 UTC
# ```
# TODO: Ensure correctness in local time-line.
def at_beginning_of_week : Time
(self - (day_of_week.value - 1).days).at_beginning_of_day
def at_beginning_of_week(start_day : Time::DayOfWeek = :monday) : Time
(self - ((day_of_week.value - start_day.value) % 7).days).at_beginning_of_day
end

def_at_end(year) { Time.local(year, 12, 31, 23, 59, 59, nanosecond: 999_999_999, location: location) }
Expand Down