提问人:Jason 提问时间:12/17/2022 最后编辑:Jim G.Jason 更新时间:12/18/2022 访问量:111
将一周中某一天的时间与一周中某一天的时间进行比较
Comparing time of day on day of week to time of day on day of week
问:
有没有一种简单的方法来测试是否在同一周的周五下午 5 点和周日下午 5 点之间?now
此尝试返回,因为它没有相对于任何一个或第一个进行比较。False
now.time()
now.isoweekday() >= 5
now.isoweekday() <= 7
True
[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
答:
2赞
Samwise
12/17/2022
#1
从本质上讲,您正在寻找的条件是:周五下午 5 点之后、周六的任何时间或周日下午 5 点之前。这很容易表达:
(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)
)
另一个选项是这样的:
- 弄清楚哪个日历周属于哪个日历周
now
- 生成该日历周的周末边界
- 测试是否在这些边界之间
now
但我认为,如果您只是测试这一个条件,这实际上比上述情况更复杂;如果这种方法是重复模式的一部分,那么这种方法会更有意义。
0赞
NoThlnG
12/17/2022
#2
这是另一种通过实际比较日期时间的方法
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
如果您在计算边界之前应用替换函数,实际上会更好,但这会更容易理解(并且更可定制)
编辑:
好吧,当我看到一些关于可读性的评论时,我已经更新了稍长但更具可读性的版本
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
评论
2赞
Jim G.
12/18/2022
@Jason:这是不可读的。我知道你只是一个初学者,但相信我:你应该总是更喜欢可读的代码,而不是像这样的聪明代码。
1赞
Jason
12/18/2022
公平点。基于有利于@JimG的统计数据和声誉。我会改变我接受的答案。
0赞
Jason
12/18/2022
实际上,@Samwise的统计数据表明它们也是值得信赖的,根据我的特定用例,我发现他们的答案更加简单易读,所以我将再次调整接受的答案。
0赞
NoThlnG
12/18/2022
@Jason 为了以防万一,我添加了更多可读版本。因为该函数本身可能对您有用。
0赞
Jim G.
12/18/2022
@NoThIng:没有不尊重......只是如果你把它签到你公司的存储库中......一年后,没有人会理解你对时间增量的用法。
1赞
Jim G.
12/18/2022
#3
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
评论