Jason
Jason

Reputation: 482

Comparing time of day on day of week to time of day on day of week

Is there a straightforward way to test whether now is between Friday, 5pm and Sunday, 5pm, of the same week?

This attempt returns False because it does not compare now.time() relative to either now.isoweekday() >= 5 or now.isoweekday() <= 7 being True first.

[in]:
import datetime
now = datetime.datetime.now()
print(now)
(now.isoweekday() >= 5 and now.time() >= datetime.time(17, 0, 0, 0)) and (now.isoweekday() <= 7 and now.time() <= datetime.time(17, 0, 0, 0))

[out]:
2022-12-17 10:00:32.253489

False

Upvotes: 0

Views: 380

Answers (3)

nxht
nxht

Reputation: 477

Here is another approach by actually comparing the datetime

now = datetime.datetime.now()
weekday = now.isoweekday()
print(now, weekday)
upper_bound = (now + datetime.timedelta(days=7 - weekday)).replace(
    hour=17, minute=0, second=0, microsecond=0
)
lower_bound = (now - datetime.timedelta(days=weekday - 5)).replace(
    hour=17, minute=0, second=0, microsecond=0
)
print(upper_bound, upper_bound.isoweekday())
print(lower_bound, lower_bound.isoweekday())

lower_bound <= now < upper_bound
2022-12-18 00:37:56 7
2022-12-18 17:00:00 7
2022-12-16 17:00:00 5
True

It is actually better if you apply replace function before calculating the bound but this would be easier to understand (and more customizable)

EDIT:

Well, as I see some comment about readability, I've updated with slightly longer but more readable version

now = datetime.datetime.now()
print(now, now.isoweekday())

def weeekday_to_datetime(now, target_weekday, target_hour):
    target_date = now + datetime.timedelta(days=(target_weekday - now.isoweekday()))
    return target_date.replace(hour=target_hour, minute=0, second=0, microsecond=0)


lower_bound = weeekday_to_datetime(now, target_weekday=5, target_hour=17)
print(lower_bound, lower_bound.isoweekday())

upper_bound = weeekday_to_datetime(now, target_weekday=7, target_hour=17)
print(upper_bound, upper_bound.isoweekday())

lower_bound <= now < upper_bound
2022-12-18 02:08:47 7
2022-12-16 17:00:00 5
2022-12-18 17:00:00 7
True

Upvotes: 0

Samwise
Samwise

Reputation: 71562

Essentially the condition you're looking for is: after 5pm on Friday, any time Saturday, or before 5pm on Sunday. That's easy to express:

(now.isoweekday() == 5 and now.time() >= datetime.time(17, 0, 0, 0)
    or now.isoweekday() == 6
    or now.isoweekday() == 7 and now.time() <= datetime.time(17, 0, 0, 0)
)

The other option would be something like:

  • figure out what calendar week now falls within
  • generate the bounds of the weekend for that calendar week
  • test whether now is between those bounds

but I think that's actually more complicated than the above if you're just testing this one condition; an approach like that would make more sense if it was part of a repeated pattern.

Upvotes: 2

Jim G.
Jim G.

Reputation: 15382

import datetime

def is_weekend_time(my_datetime):
  if (my_datetime.isoweekday() == 5):
    return datetime.time(17, 0, 0, 0) <= my_datetime.time()
  if (my_datetime.isoweekday() == 6):
    return True
  if (my_datetime.isoweekday() == 7):
    return my_datetime.time() < datetime.time(17, 0, 0, 0)
  return False

now = datetime.datetime.now()
print(now)
print(is_weekend_time(now))

print()
friday_before = datetime.datetime(2022,12,16,16,59,59)
print('Friday Before')
print(friday_before)
print(is_weekend_time(friday_before))

print()
friday_after = datetime.datetime(2022,12,16,17,00,00)
print('Friday After')
print(friday_after)
print(is_weekend_time(friday_after))

print()
saturday = datetime.datetime(2022,12,17,16,59,59)
print('Saturday')
print(saturday)
print(is_weekend_time(saturday))

print()
sunday_before = datetime.datetime(2022,12,18,16,59,59)
print('Sunday Before')
print(sunday_before)
print(is_weekend_time(sunday_before))

print()
sunday_after = datetime.datetime(2022,12,18,18,00,00)
print('Sunday After')
print(sunday_after)
print(is_weekend_time(sunday_after))

2022-12-17 16:01:40.826755
True

Friday Before
2022-12-16 16:59:59
False

Friday After
2022-12-16 17:00:00
True

Saturday
2022-12-17 16:59:59
True

Sunday Before
2022-12-18 16:59:59
True

Sunday After
2022-12-18 18:00:00
False

Upvotes: 1

Related Questions