问题描述
我刚刚编写了一些执行日期/时间操作的代码,而我发现
发现Python 2.3 datetime模块没有提供一些
相当基本的功能。我理解推理(日期时间
模块提供了一组数据类型,并且不会试图进入日期算法的模糊水域)但是作为这些东西可能非常好b / b很难做到正确,我想知道是否有人已经实施了任何日期算法,或者如果我错过了一些简单的方法
做我想要的事。
我的具体要求是:
1.获取最后一天包含给定日期(时间)的月份。我确实很惊讶地发现这个丢失了,因为在我看来,日期时间模块必须知道每个月的最后一天是什么,所以
暴露它并不难。
2.添加一些月份到一个日期。这是混乱的,因为有
期权(1月31日之后的一个月)。麻烦的是,
计算虽然很简单,但是很难做到正确(月份是1-based,
因此很容易犯1个错误,而divmod并不像你想要的那样有用。
其他很高兴我不需要这个程序的功能,
但我过去觉得它很有用,是
- 一个日期(时间)到{年,月,日,季,周......}或者
截断。或者(偶尔)砍到下一个更高的边界
(上限)。
- 计算两个日期之间的{年,月,日......}的数量。
(或多或少相当于舍入timedelta)。
后两者目前还没有很好的定义,因为我有没有
即时用例。
在没有其他任何东西的情况下,我可能会写一个日期
公用事业"在某些方面我自己的模块。但是我不想要重新发明轮子。
保罗。
I was just writing some code which did date/time manipulations, and I
found that the Python 2.3 datetime module does not supply a number of
fairly basic functions. I understand the reasoning (the datetime
module provides a set of datatypes, and doesn''t attempt to get into
the murky waters of date algorithms) but as these things can be quite
tricky to get right, I was wondering if anyone has already implemented
any date algorithms, or alternatively if I''d missed some easy way of
doing what I''m after.
My specific requirements were:
1. Get the last day of the month contatining a given date(time). I
really was surprised to find this one missing, as it seems to me that
the datetime module must know what the last day for each month is, so
exposing it wouldn''t have been hard.
2. Add a number of months to a date. This is messy, as there are
options (what is one month after 31st Jan). The trouble is that the
calculation, while simple, is tricky to get right (month is 1-based,
so off-by-1 errors are easy to make, and divmod isn''t as useful as
you''d first think).
Other "nice to have" functions which I didn''t need for this program,
but which I have found useful in the past, are
- Round a date(time) to a {year,month,day,quarter,week,...} Or
truncate. Or (occasionally) chop to the next higher boundary
(ceiling).
- Calculate the number of {years,months,days,...} between two dates.
(Which is more or less the equivalent of rounding a timedelta).
These latter two aren''t very well defined right now, because I have no
immediate use case.
In the absence of anything else, I''ll probably write a "date
utilities" module for myself at some point. But I''d hate to be
reinventing the wheel.
Paul.
推荐答案
我很惊讶地发现它也不见了。
I''m kind of surprised to see that it''s missing, too.
我能找到的最佳解决方案是
def month_end(dt):
#获取下个月
y,m = dt。年,dt.month
如果m == 12:
y = = 1
m = 1
else:
m + = 1
#使用replace来满足日期时间和日期类型。这个
#使得日期时间的时间成分保持不变 - 它是
#争论这是否正确。
return dt.replace(year = y,month = m,day = 1) - datetime.timedelta(days = 1)
这并不难 - 但它'有点狡猾(我做了一些错误的开始和
一些愚蠢的一个一个错误)而且我宁愿从一个
库中获取它。我下次需要时会犯同样的错误。
The best solution I could find was
def month_end(dt):
# Get the next month
y, m = dt.year, dt.month
if m == 12:
y += 1
m = 1
else:
m += 1
# Use replace to cater for both datetime and date types. This
# leaves the time component of a datetime unchanged - it''s
# arguable whether this is the right thing.
return dt.replace(year=y, month=m, day=1) - datetime.timedelta(days=1)
It''s not hard - but it''s mildly tricky (I made a few false starts and
some silly off-by-one errors) and I''d much rather grab it from a
library than make the same mistakes next time I need it.
这取决于应用程序。
That''s application dependent.
True。但对于天真而言,使用,一个简单的定义。这与
符合日期时间模块的理念,即不试图迎合
" advanced"使用,但提供一些有用的东西直接使用
。在这个特殊的情况下,我认为明显的定义
(同一天数N个月),如果适用,还有一个记录良好的
合理的定义。边缘情况的答案(例如,1月31日加1个月)
有用。在实践中,我怀疑99%的案例涉及在一个月的第一个月或最后一个月增加一个月的b $ b。
如果是债券,例如,在月份30日支付利息,你可能想要30日,除了2月你想要的那个月的最后一天。但是,合同可能会指定其他内容。并且在任何情况下你都不希望日期突然变为第28页,因为你经历了2月。
True. But for "naive" use, a simple definition does. This is in line
with the datetime module''s philosophy of not trying to cater for
"advanced" uses, but to provide something useful for straightforward
use. In this particular case, I''d argue that the obvious definition
(same day number N months on) where applicable, plus a well-documented
"reasonable" answer for the edge cases (eg, Jan 31 plus 1 month) is
useful. In practice, I suspect that 99% of cases involve adding a
number of months to either the first or the last of a month.
If a bond, for example, has interest payable on the 30th of the
month, you probably want the 30th, except in February you want the
last day of the month. However, the contract may specify something
else. And in no case do you want the date to suddenly change to the
28th because you went through February.
但这不是一个添加一个月的情况,作为一个更复杂的概念,一个重复日期。不过,我明白你的观点。
But that''s not so much a case of adding a month, as a more complex
concept, a "repeating date". Nevertheless, I take your point.
在Parnassus的宝库中有一个备受推崇的日期模块。这个名字暂时让我感到惊讶,
但是通过Vaults的一些洞察将会出现几个可能做你想做的日期例程。
There''s a very well regarded date module out there in the
Vaults of Parnassus. The name escapes me at the moment,
but a bit of spelunking through the Vaults will turn up several
date routines that may do what you want.
我想你在考虑mxDateTime。我知道这一点,并同意
它非常全面。我真的不知道为什么我不喜欢使用它 - 部分只是一个减少依赖性的例子,而且
已经有这么多日期了我的代码中的类型(COM,cx_Oracle,
datetime),我不愿意添加另一个 - 已经有很多
太多代码用于转换表示...
感谢您的评论,
保罗。
-
此签名故意留空
I guess you''re thinking of mxDateTime. I''m aware of this, and agree
that it''s pretty comprehensive. I don''t really know why I prefer not
to use it - partly it''s just a case of reducing dependencies, also
there are already so many date types in my code (COM, cx_Oracle,
datetime) that I am reluctant to add another - there is already far
too much code devoted to converting representations...
Thanks for the comments,
Paul.
--
This signature intentionally left blank
我很惊讶地发现它也不见了。
I''m kind of surprised to see that it''s missing, too.
我能做的最好的解决方案找到了
def month_end(dt):
#获取下个月
y,m = dt.year,dt.month
如果m == 12:
y + = 1
m = 1
否则:
m + = 1
The best solution I could find was
def month_end(dt):
# Get the next month
y, m = dt.year, dt.month
if m == 12:
y += 1
m = 1
else:
m += 1
这看起来不完整。您可以使用它来获得下个月第一天的连续日期
,但是您仍需要减去一天
然后打印当天。
#使用replace来满足日期时间和日期类型。这个
#使得日期时间的时间成分保持不变 - 这是#争论这是否正确。
返回dt.replace(年= y,月= m,day = 1) - datetime.timedelta(days = 1)
这并不难 - 但它有点棘手(我做了一些错误的开始和
一些愚蠢的逐个错误)我宁愿从一个
库中获取它而不是在下次需要它时犯同样的错误。
正如我所说,如果你想提交它,我没有看到他们为什么不接受
a补丁的明显原因。 br>
This looks incomplete. You could use this to get the sequential date
for the first of the next month, but you still have to subtract one day
and then print out the day.
# Use replace to cater for both datetime and date types. This
# leaves the time component of a datetime unchanged - it''s
# arguable whether this is the right thing.
return dt.replace(year=y, month=m, day=1) - datetime.timedelta(days=1)
It''s not hard - but it''s mildly tricky (I made a few false starts and
some silly off-by-one errors) and I''d much rather grab it from a
library than make the same mistakes next time I need it.
As I said, I don''t see any obvious reason why they wouldn''t accept
a patch, if you want to submit it.
这取决于应用程序。
That''s application dependent.
是的。但对于天真而言,使用,一个简单的定义。这符合日期时间模块的理念,即不试图迎合
高级模式。使用,但提供一些有用的东西直接使用。在这个特殊情况下,我认为明显的定义是(适用于N个月的同一天)适用的情况,加上有充分证据的合理的定义。边缘情况的答案(例如,1月31日加1个月)是有用的。在实践中,我怀疑99%的案例涉及在一个月的第一个月或最后一个月增加一个月的数量。
True. But for "naive" use, a simple definition does. This is in line
with the datetime module''s philosophy of not trying to cater for
"advanced" uses, but to provide something useful for straightforward
use. In this particular case, I''d argue that the obvious definition
(same day number N months on) where applicable, plus a well-documented
"reasonable" answer for the edge cases (eg, Jan 31 plus 1 month) is
useful. In practice, I suspect that 99% of cases involve adding a
number of months to either the first or the last of a month.
除了边缘情况我在下面提到,这真的太简单了;
它只是在月份中添加一个并保持相同的日期。根本不值得
一种方法,特别是如果你有一个月的最后一天的话。方法。
Except for the edge case I mention below, this is really too simple;
it''s just add one to the month and keep the same date. Hardly worth
a method at all, especially if you have the "last day of month" method.
但这不是一个增加一个例子的情况。月,作为一个更复杂的概念,一个重复日期。不过,我明白你的观点。
But that''s not so much a case of adding a month, as a more complex
concept, a "repeating date". Nevertheless, I take your point.
是的。当你有一些依赖于应用程序的东西时,他们倾向于不把天真的东西放进去。定义。什么是'天真''对一个人的定义
的人
对另一个人来说是完全错误的。
John Roth
感谢您的评论,
Paul。
-
Yes. When you''ve got something application dependent, they tend
not to put in "naive" definitions. What''s a ''naive'' definition for one
person
is simply wrong for another.
John Roth
Thanks for the comments,
Paul.
--
这篇关于日期实用程序功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!