什么是最优雅的一天结束的方式(日期时间)? 我目前正在编写一些报告代码,以允许用户选择指定日期范围。它的工作方式(简化)是: 用户(选项)指定一年。 用户(可选)指定...

问题描述 投票:0回答:6
用户(可选)指定一个月。

用户(可选)每天指定。

    这里是一个代码段,以及描述我要做什么的评论:
  • from datetime import datetime, timedelta # ... now = datetime.now() start_time = now.replace(hour=0, minute=0, second=0, microsecond=0) stop_time = now # If the user enters no year, month, or day--then we'll simply run a # report that only spans the current day (from the start of today to now). if options['year']: start_time = start_time.replace(year=options['year'], month=0, day=0) stop_time = stop_time.replace(year=options['year']) # If the user specifies a year value, we should set stop_time to the last # day / minute / hour / second / microsecond of the year, that way we'll # only generate reports from the start of the specified year, to the end # of the specified year. if options['month']: start_time = start_time.replace(month=options['month'], day=0) stop_time = stop_time.replace(month=options['month']) # If the user specifies a month value, then set stop_time to the last # day / minute / hour / second / microsecond of the specified month, that # way we'll only generate reports for the specified month. if options['day']: start_time = start_time.replace(day=options['day']) stop_time = stop_time.replace(day=options['day']) # If the user specifies a day value, then set stop_time to the last moment of # the current day, so that reports ONLY run on the current day.
  • 我正在试图找到上面写代码的最优雅的方法 - 我一直在尝试找到一种使用TimeDelta的方法,但似乎无法弄清楚。任何建议将不胜感激。
  • 为适当的一年,月或一天设置
stop_time

,然后减去一年,一个月或一天 start_time

使用

timedelta(microseconds=1)

可以简化您的代码。 它比使用
python python-datetime
6个回答
7
投票

timedelta

对象要干净一些。

这里可以让您入门:
if options['year']:
    start_time = start_time.replace(year=options['year'], month=1, day=1)
    stop_time = stop_time.replace(year=options['year']+1)-timedelta(microseconds=1)

elif options['month']:
    start_time = start_time.replace(month=options['month'], day=1)
    months=options['month']%12+1
    stop_time = stop_time.replace(month=months,day=1)-timedelta(microseconds=1)

else:
    start_time = start_time.replace(day=options['day'])
    stop_time = stop_time.replace(day=options['day'])+timedelta(days=1,microseconds=-1)
    

dict.get

5
投票

from datetime import datetime options = dict(month=5, day=20) now = datetime.now() start_time = datetime(year=options.get('year', now.year), month=options.get('month', 1), day=options.get('day', 1) hour=0, minute=0, second=0) stop_time = datetime(year=options.get('year', now.year), month=options.get('month', now.month), day=options.get('day', now.day), hour=now.hour, minute=now.minute, second=now.second)

    
today = datetime.date.today() begintime = today.strftime("%Y-%m-%d 00:00:00") endtime = today.strftime("%Y-%m-%d 23:59:59")

在这里查看一些答案,但并没有真正找到任何非常优雅的东西,我在标准库周围戳了一下,发现了我当前的解决方案(我非常喜欢):

from datetime import datetime, date, timedelta def get_current_timestamp(): return int(datetime.now().timestamp()) def get_end_today_timestamp(): # get 23:59:59 result = datetime.combine(date.today() + timedelta(days=1), datetime.min.time()) return int(result.timestamp()) - 1 def get_datetime_from_timestamp(timestamp): return datetime.fromtimestamp(timestamp) end_today = get_datetime_from_timestamp(get_end_today_timestamp())
.

5
投票
date = datetime.strftime('<input date str>')

date.replace(hour=0, minute=0, second=0, microsecond=0)  # now we get begin of the day
date += timedelta(days=1, microseconds=-1)  # now end of the day

0
投票
dateutil
在边缘案例上非常有效。它的天数/月/年正确。如果我有
from datetime import date from dateutil.relativedelta import relativedelta now = date.today() stop_time = now + relativedelta(days=1) start_time = date( # NOTE: I'm not doing dict.get() since in my implementation, these dict # keys are guaranteed to exist. year = options['year'] or now.year, month = options['month'] or now.month, day = options['day'] or now.day ) if options['year']: start_time = date(year=options['year'] or now.year, month=1, day=1) stop_time = start_time + relativedelta(years=1) if options['month']: start_time = date( year = options['year'] or now.year, month = options['month'] or now.month, day = 1 ) stop_time = start_time + relativedelta(months=1) if options['day']: start_time = date( year = options['year'] or now.year, month = options['month'] or now.month, day = options['day'] or now.day, ) stop_time = start_time + relativedelta(days=1) # ... do stuff with start_time and stop_time here ...

0
投票
dateutil.relativedata.relativedata
,它将增加一年,并将月份设置为1(效果很好)。
也:在上述实施中,如果用户指定没有可选的日期(年,月或每天) - 我们将退缩到一个不错的默认值(start_time = topay noft,stop_time = timorm),那样我们'默认情况下仅在当天做事。

0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.