更改datetime.date的日期或月份,不使用构造函数的子类或辅助函数

ely*_*ely 2 python datetime calendar date

我收到各种格式化的日期或部分日期,并在将它们datetime.date格式化后,我想调整月份字段或日期字段而不调用构造函数来获取整个新datetime.date对象.

数据可以是以下任何一种:

'2012.01.01'
'194311'
'1865/08/30'
'1701'
 ... etc
Run Code Online (Sandbox Code Playgroud)

对于未指定的日期,我希望将它们推送到1月1日,提供月份的第一个月,到提供的月份结束,或者到12月31日.必须提供一年且不会更改.

dateutil.parser.parse 处理所有这些很好,除了它填补在当前月份和/或日期缺少的月份和/或日期的约定:

In [11]: dateutil.parser.parse('2012/04')
Out[11]: datetime.datetime(2012, 4, 10, 0, 0)

In [12]: dateutil.parser.parse('1865')
Out[12]: datetime.datetime(1865, 2, 10, 0, 0)

In [13]: dateutil.parser.parse('1920.03.27')
Out[13]: datetime.datetime(1920, 3, 27, 0, 0)
Run Code Online (Sandbox Code Playgroud)

在进入datetime.datetime格式之后,我可以将datetime函数调用date为有效地转换为a datetime.date,因此该部分是微不足道的.

我想避免的是编写一个帮助函数,必须经历调用构造函数的痛苦,如下所示:

def adjust_to_jan1(date_str):
    temp = dateutil.parser.parse(date_str).date()
    return datetime.date(temp.year, 1, 1)
Run Code Online (Sandbox Code Playgroud)

要么

def adjust_to_month_end(date_str):
    import calendar
    temp = dateutil.parser.parse(date_str).date()
    last_day = calendar.monthrange(temp.year, temp.month)
    return datetime.date(temp.year, temp.month, last_day)
Run Code Online (Sandbox Code Playgroud)

要么

def adjust_to_last_weekdat(date_str):
    import calendar
    temp = dateutil.parser.parse(date_str).date()
    last_weekday = max([elem for x in calendar.monthcalendar(temp.year, 
                                                             temp.month) 
                        for elem in x[0:5]])
    return datetime.date(temp.year, temp.month, last_weekday)
Run Code Online (Sandbox Code Playgroud)

由于我经常以交互方式处理这些数据,所以始终定义这些类型的函数会很麻烦.它变得更加复杂:上周那天不是某个任意一组国家的假期...上个工作日,除非它可能是上周六,如果这一年在某一年之前...

我想做的只是声明我想要的更改,例如:

some_date = datetime.date(2012, 1, 20)
some_date.day = 1
Run Code Online (Sandbox Code Playgroud)

但这是不允许的.

我可以继承,datetime但后来我不得不担心我的子类没有做任何损坏,以及它是否可以使用来自交互式工作的一些脚本的其他人.

任何其他解决方案将不胜感激.

如果你认为没有比子类化或编写大量这些辅助函数更好的方法,那很好,但我更愿意为任何不同的提案留下评论和答案- 而不是得到很多回答说"这看起来很好"或类似的东西.

Mar*_*ers 6

datetime.date对象是不可变的,但它们确实有一种.replace()方法可以做你想要的:

somedate = somedate.replace(day=1)
Run Code Online (Sandbox Code Playgroud)

该方法返回一个对象,其中交换了所需的值.

为了完整起见,也有datetime.datetime.replace()datetime.time.replace()方法.