pendulum icon indicating copy to clipboard operation
pendulum copied to clipboard

Have `Day` enum returned by `day_of_week`

Open liaden opened this issue 3 years ago • 1 comments

  • [x] I have searched the issues of this repo and believe that this is not a duplicate.
  • [x] I have searched the documentation and believe that my question is not covered.

Feature Request

The Problem

There is this inconsistency that makes the idea of constructing a Day enum from an integer somewhat problematic, and it makes passing these integers confusing to pass around since it depends where it came from.

from datetime import datetime
import pendulum

# when ran on wednesday
datetime.now().weekday()  # 2
pendulum.now().weekday()  # 2
pendulum.now().day_of_week # 3

Proposed Solution

It would be nice if pendulum had a Day enum and it returned that enum for day_of_week. The underlying number could be left consistent with python's integer values this way, and allow for API usage such as:

from pendulum import Day

# using Day enum with a pendulum instance
pendulum.now() == Day.WEDNESDAY
pendulum.now() == Day.WED
pendulum.now().previous(Day.WED)

# constructing a Day
Day(datetime.now())
Day(datetime.now().weekday())
Day("wednesday")

# optionally add methods to Day instance
pendulum.now().day_of_week.is_wednesday()

liaden avatar Jul 20 '22 13:07 liaden

Somewhat related, day_of_week also doesn't take into account _WEEK_STARTS_AT and _WEEK_ENDS_AT.

day_of_week calls datetime.date.isoweekday() (which returns 1-7, with 1 being Monday) and then mods it by 7. Now, it's nice that there's a function for us Americans (and Canadians I guess) -- but I would expect the naming conventions to be clearer. Maybe us_weekday() or something?

It's just strange that Pendulum specifies that the default week is Monday to Sunday, but then the only pendulum-provided function returns the day starting with Sunday (and with no way to change that).

CrispinStichart avatar Sep 28 '22 20:09 CrispinStichart