-1

Oracle has a very useful function for finding the date of the next weekday. This function is called NEXT_DAY. It takes a start date, say 15-OCT-2009, and the weekday we're expecting, say TUESDAY. The result of NEXT_DAY('15-OCT-2009', 'TUESDAY') is 20-OCT-2009 because that's the next Tuesday after the 15th of October 2009.

Does Python have a function, be it Built-in or via the datetime library, that does this same thing?

I need it to deterministically return true/false for the question "if this day was in November, is it Thanksgiving?" without referencing any kind of lookup table for any year.

It's used by looking at the day number, the 15th part from above, and finding the number of days until the next Thursday in October then adding 21 to that. An alternative method of answering the question "is the day Thanksgiving?", deterministically, at runtime, without any lookup tables, restricted to plain Python or the datetime library would work for my situation but would not work to answer the question in the title.

UpTide
  • 307
  • 3
  • 13
  • Why is the question a bad question? – UpTide Feb 16 '23 at 21:47
  • The two close votes seem to be for, "Seeking recommendations for books, tools, software libraries, and more", but I am not seeking recommendation for anything. I asked a yes or no question; as Python or the datetime library would have such a function or would not have such a function. Various versions of this question have been asked for other languages, such as [https://stackoverflow.com/questions/1579010/get-next-date-from-weekday-in-javascript?rq=1] for Java. If it's an issue of, "there are several ways to skin a cat", then why is my link's question fine? – UpTide Feb 16 '23 at 21:58
  • Did you check the docs for datetime module? Did you find such function (obviously no), so your question is about Python in general, i.e. also third-party packages. In which case it is in fact asking for a software tool or library. By the way, one of the votes was mine, but I will retract it. Let the others decide – buran Feb 16 '23 at 22:04
  • I meant my question to be restricted to either built-in or datetime functions. I'm not extremely experienced with the history nor workings of either Python and the datetime library, so if you want to get the answer to the more philosophical, "why ask if you have a reasonable assumption the answer is no?" then I will admit it is an attempt to extract such wisdom from those that do have the experience. To ask if metaphorical nail is meant, in python, to be driven with the side of a wrench, or if this is a case where I need to build a hammer. – UpTide Feb 16 '23 at 22:14

2 Answers2

1

I think this should work.

from datetime import datetime,timedelta

def next_day(date, day_of_week):
    if date.weekday() == day_of_week:
        return (date + timedelta(days=7)).date()
    else:
        return (date + timedelta(days=(day_of_week - date.weekday() + 7) % 7)).date()

# Test
day_names = "mon tue wed thu fri sat sun".split()
now = datetime.now()
print(f"Today is {day_names[now.weekday()]}, {now.date()}")
for i in range(7):
    print(f"next {day_names[i]}: {next_day(now, i)}")

Ouput

Today is thu, 2023-02-16
next mon: 2023-02-20
next tue: 2023-02-21
next wed: 2023-02-22
next thu: 2023-02-23
next fri: 2023-02-17
next sat: 2023-02-18
next sun: 2023-02-19
001
  • 13,291
  • 5
  • 35
  • 66
  • I am assuming by your answer there is no such function in the reference datetime library nor built-in Python in general. I appreciate you writing a function that solves the issue. Edited to be more clear. – UpTide Feb 16 '23 at 22:19
0

Have a look at Delorean (need to install via pip) and it's natural language capabilities

e.g. finding Thanksgiving for given year

import delorean as dl
from datetime import datetime
year = int(input('Enter a year:'))
d = dl.Delorean(datetime(year,11,30), timezone='UTC')
print(f"In {year} Thanksgiving is {d.last_thursday().date}")

output

Enter a year:2023
In 2023 Thanksgiving is on 2023-11-23

Enter a year:2022
In 2022 Thanksgiving is 2022-11-24

or finding next Monday

print(dl.Delorean(datetime.now(), timezone='utc').next_monday().date)

output

2023-02-20
buran
  • 13,682
  • 10
  • 36
  • 61
  • I am unable to use any libraries outside of datetime. – UpTide Feb 16 '23 at 22:05
  • It is development for something to run in production on critical infrastructure. No, the budget for such infrastructure isn't always that great. – UpTide Feb 16 '23 at 22:16