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

Get week start and end days #654

Closed
sestrella opened this issue Mar 6, 2022 · 4 comments
Closed

Get week start and end days #654

sestrella opened this issue Mar 6, 2022 · 4 comments

Comments

@sestrella
Copy link
Contributor

sestrella commented Mar 6, 2022

Inspired by Rails DateAndTime::Calculations module, it would be useful to have a set of functions that return week boundaries based on a weekday. This proposal - if approved - intends to add the following functions to IsoWeek implementation:

fn start_day(&self, weekday: Weekday) -> NaiveDate {
  NaiveDate::from_isoywd(self.year(), self.week(), weekday)
}

Equivalent to Rails beginning_of_week.

fn end_day(&self, weekday: Weekday) -> NaiveDate {
  self.start_day(weekday) + Duration::days(6)
}

Equivalent to Rails end_of_week.

Additionally, a function that returns the start and end of the week at once could be added as well:

fn start_and_end_days(&self, weekday: Weekday) -> (NaiveDate, NaiveDate) {
  let start_day = self.start_day(weekday);
  (start_day, start_day + Duration::days(6))
}

Usage

A common use case would be getting the week boundaries based on a day:

let week = Local::today().iso_week();

let start_day = week.start_day(Weekday::Mon)
let end_day = week.end_day(Weekday::Mon)
// or
let (start_day, end_day) = week.start_and_end_days(Weekday::Mon)
@djc
Copy link
Contributor

djc commented Mar 23, 2022

Can you talk more about the use case, what you are trying to achieve that is harder with the current API than (you think) it should be? There are any number of ways we could go about this, but you're describing potential solutions more than the problem you started out with.

@sestrella
Copy link
Contributor Author

Sure let me provide more context, when it comes down to reports it is quite common to collect data within a date range such as weekly, monthly, quarterly, or yearly, therefore, getting week boundaries based on a specific date could be useful, in fact, that is the main reason why I came out with this proposal since right now I'm pulling data from an API that takes start_day and end_day as arguments. About

what you are trying to achieve that is harder with the current API than

I would say the current API is very polished and coming up with a solution for the use case described above was not difficult, but at the same time, it feels like a common use case which is why I made this proposal. I hope this extra context provides more insights about the problem. Thank you for reviewing this proposal.

@djc
Copy link
Contributor

djc commented Mar 23, 2022

Okay, it makes sense to me to add start(Weekday), end(Weekday) and bounds(Weekday) methods to IsoWeek. Maybe these should return NaiveDateTime, and then you can get the date() out there?

Can you submit a PR including some tests?

@sestrella
Copy link
Contributor Author

Sure, I'm planning to work on this next week

sestrella added a commit to stackbuilders/chrono that referenced this issue Mar 29, 2022
sestrella added a commit to stackbuilders/chrono that referenced this issue Apr 20, 2022
sestrella added a commit to stackbuilders/chrono that referenced this issue Apr 22, 2022
sestrella added a commit to stackbuilders/chrono that referenced this issue Apr 22, 2022
Co-authored-by: Jorge Guerra <jguerra@stackbuilders.com>
sestrella added a commit to stackbuilders/chrono that referenced this issue May 5, 2022
Co-authored-by: Jorge Guerra <jguerra@stackbuilders.com>
sestrella added a commit to stackbuilders/chrono that referenced this issue May 5, 2022
Co-authored-by: Jorge Guerra <jguerra@stackbuilders.com>
sestrella added a commit to stackbuilders/chrono that referenced this issue May 13, 2022
Co-authored-by: Jorge Guerra <jguerra@stackbuilders.com>
sestrella added a commit to stackbuilders/chrono that referenced this issue May 17, 2022
Co-authored-by: David Mazarro <dmunuera@stackbuilders.com>
Co-authored-by: Jorge Guerra <jguerra@stackbuilders.com>
sestrella added a commit to stackbuilders/chrono that referenced this issue May 26, 2022
Co-authored-by: David Mazarro <dmunuera@stackbuilders.com>
Co-authored-by: Jorge Guerra <jguerra@stackbuilders.com>
sestrella added a commit to stackbuilders/chrono that referenced this issue Jun 9, 2022
Co-authored-by: David Mazarro <dmunuera@stackbuilders.com>
Co-authored-by: Jorge Guerra <jguerra@stackbuilders.com>
sestrella added a commit to stackbuilders/chrono that referenced this issue Jun 9, 2022
Co-authored-by: David Mazarro <dmunuera@stackbuilders.com>
Co-authored-by: Jorge Guerra <jguerra@stackbuilders.com>
sestrella added a commit to stackbuilders/chrono that referenced this issue Jun 15, 2022
Co-authored-by: David Mazarro <dmunuera@stackbuilders.com>
Co-authored-by: Jorge Guerra <jguerra@stackbuilders.com>
djc pushed a commit that referenced this issue Jun 15, 2022
Co-authored-by: David Mazarro <dmunuera@stackbuilders.com>
Co-authored-by: Jorge Guerra <jguerra@stackbuilders.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants