查找特定日期范围 [英] Finding specific date ranges

查看:90
本文介绍了查找特定日期范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在制作一个时间表应用程序,我需要在一个月内找到完全包含的

第一个付款期在那个月

来计算休假时间。以下是一些示例日期范围:

2006年12月31日2007年1月13日#snnn赚不到

2007年1月14日2007年1月27日#确实赚了

2007年1月28日2007年2月10日#snnn不是/ b $ b 2007年2月11日2007年2月24日#确实

到目前为止,最好的我想出的方法是创建一个

元组列表,其中包含一年的付费期日期范围和

遍历元组寻找第一个发生了

个月的名字匹配。然后我将该日期范围添加到单独的列表中

并以某种方式忽略该月的任何其他匹配。这似乎是一个

hack。有没有人有更好的主意?


提前致谢!


Mike

Hi,

I am working on a timesheet application in which I need to to find the
first pay period in a month that is entirely contained in that month
to calculate vacation time. Below are some example date ranges:
December 31, 2006 January 13, 2007 # doesn''t earn
January 14, 2007 January 27, 2007 # does earn
January 28, 2007 February 10, 2007 # doesn''t
February 11, 2007 February 24, 2007 # does
So far, the best approach I''ve come up with is to create a list of
tuples that contain the pay period date ranges for the year and
iterate through the tuples looking for the first occurrence of the
month names matching. Then I''d add that date range to a separate list
and somehow ignore any other matches in that month. This seems like a
hack. Does anyone have a better idea?

Thanks in advance!

Mike

推荐答案

ky******@gmail.com 写道:




我正在制作一个时间表应用程序,我需要在其中找到

一个月内的第一个支付期完全包含在该月中

来计算休假时间。以下是一些示例日期范围:


2006年12月31日2007年1月13日#snnn赚不到

2007年1月14日2007年1月27日#确实赚了

2007年1月28日2007年2月10日#snn'n不用/ b $ b 2007年2月11日2007年2月24日#does


到目前为止,我提出的最好的方法是创建一个

元组列表,其中包含一年中的支付期日期范围和

迭代通过元组寻找第一次出现的

月份名称匹配。然后我将该日期范围添加到单独的列表中

并以某种方式忽略该月的任何其他匹配。这似乎是一个

hack。有没有人有更好的主意?
Hi,

I am working on a timesheet application in which I need to to find the
first pay period in a month that is entirely contained in that month
to calculate vacation time. Below are some example date ranges:
December 31, 2006 January 13, 2007 # doesn''t earn
January 14, 2007 January 27, 2007 # does earn
January 28, 2007 February 10, 2007 # doesn''t
February 11, 2007 February 24, 2007 # does
So far, the best approach I''ve come up with is to create a list of
tuples that contain the pay period date ranges for the year and
iterate through the tuples looking for the first occurrence of the
month names matching. Then I''d add that date range to a separate list
and somehow ignore any other matches in that month. This seems like a
hack. Does anyone have a better idea?



好​​吧,我可以想出一个解决方案,它基本上反映了我用SQL做的方式(因为这种方式)事情是我的面包和油脂

那里)但我不相信它真的比你的建议更好。

然而,为了这个目的插图:


< code>

导入日历

导入日期时间


年= 2007


个月= [

(datetime.date(年,n,1),datetime.date(年,n,calendar.monthrange) (年,n)[1]))

范围内的n(1,13)

]


periods = [

(datetime.date(2006,12,31),datetime.date(2007,1,13)),

(datetime.date(2007,1, 14),datetime.date(2007,1,27)),

(datetime.date(2007,1,28),datetime.date(2007,2,10)),
(datetime.date(2007,2,11),datetime.date(2007,2,24))

]


for m个月:

candidate_period如果m [0]< = p [0]而p [1] <= m [1]则s = [p表示p期间]如果candidate_periods:
$ b,则为
$ b print m [0]," =>",min(candidate_periods)

else:

print m [0]," =>" ;,没有期间匹配


< / code>


TJG


Well, I can come up with a solution which basically reflects the
way I''d do it in SQL (since this kind of thing is my bread-and-butter
there) but I''m not convinced it''s really any better than your proposal.
However, for the purposes of illustration:

<code>
import calendar
import datetime

YEAR = 2007

months = [
(datetime.date (YEAR, n, 1), datetime.date (YEAR, n, calendar.monthrange (YEAR, n)[1]))
for n in range (1, 13)
]

periods = [
(datetime.date(2006, 12, 31), datetime.date(2007, 1, 13)),
(datetime.date(2007, 1, 14), datetime.date(2007, 1, 27)),
(datetime.date(2007, 1, 28), datetime.date(2007, 2, 10)),
(datetime.date(2007, 2, 11), datetime.date(2007, 2, 24))
]

for m in months:
candidate_periods = [p for p in periods if m[0] <= p[0] and p[1] <= m[1]]
if candidate_periods:
print m[0], "=>", min (candidate_periods)
else:
print m[0], "=>", "no period matches"

</code>

TJG


9月6日上午10:57,Tim Golden< m ... @ timgolden.me.ukwrote:
On Sep 6, 10:57 am, Tim Golden <m...@timgolden.me.ukwrote:

kyoso ... @ gmail。 com写道:
kyoso...@gmail.com wrote:


Hi,


我正在研究一个时间表应用程序我需要找到一个月内完全包含的

第一个支付期间

来计算休假时间。以下是一些示例日期范围:
I am working on a timesheet application in which I need to to find the
first pay period in a month that is entirely contained in that month
to calculate vacation time. Below are some example date ranges:


2006年12月31日2007年1月13日#snnn赚不到

1月2007年1月27日2007年#确实赚了

2007年1月28日2007年2月10日#snn't不用/ b $ b 2007年2月11日2007年2月24日#do
December 31, 2006 January 13, 2007 # doesn''t earn
January 14, 2007 January 27, 2007 # does earn
January 28, 2007 February 10, 2007 # doesn''t
February 11, 2007 February 24, 2007 # does


到目前为止,我想出的最好的方法是创建一个包含以下内容的

元组列表支付年度的日期范围和

迭代通过元组寻找第一次出现的

月份名称匹配。然后我将该日期范围添加到单独的列表中

并以某种方式忽略该月的任何其他匹配。这似乎是一个

hack。有没有人有更好的主意?
So far, the best approach I''ve come up with is to create a list of
tuples that contain the pay period date ranges for the year and
iterate through the tuples looking for the first occurrence of the
month names matching. Then I''d add that date range to a separate list
and somehow ignore any other matches in that month. This seems like a
hack. Does anyone have a better idea?



好​​吧,我可以想出一个解决方案,它基本上反映了我用SQL做的方式(因为这种方式)事情是我的面包和油脂

那里)但我不相信它真的比你的建议更好。

然而,为了这个目的插图:


< code>

导入日历

导入日期时间


年= 2007


个月= [

(datetime.date(年,n,1),datetime.date(年,n,calendar.monthrange) (年,n)[1]))

范围内的n(1,13)

]


periods = [

(datetime.date(2006,12,31),datetime.date(2007,1,13)),

(datetime.date(2007,1, 14),datetime.date(2007,1,27)),

(datetime.date(2007,1,28),datetime.date(2007,2,10)),
(datetime.date(2007,2,11),datetime.date(2007,2,24))

]


for几个月:

candidate_periods = [p为句子中的p,如果m [0]< = p [0]和p [1]< = m [1]]

如果candidate_periods:

print m [0]," =>",min(candidate_periods)

else:

print m [0]," =>" ;,没有期间匹配


< / code>


TJG


Well, I can come up with a solution which basically reflects the
way I''d do it in SQL (since this kind of thing is my bread-and-butter
there) but I''m not convinced it''s really any better than your proposal.
However, for the purposes of illustration:

<code>
import calendar
import datetime

YEAR = 2007

months = [
(datetime.date (YEAR, n, 1), datetime.date (YEAR, n, calendar.monthrange (YEAR, n)[1]))
for n in range (1, 13)
]

periods = [
(datetime.date(2006, 12, 31), datetime.date(2007, 1, 13)),
(datetime.date(2007, 1, 14), datetime.date(2007, 1, 27)),
(datetime.date(2007, 1, 28), datetime.date(2007, 2, 10)),
(datetime.date(2007, 2, 11), datetime.date(2007, 2, 24))
]

for m in months:
candidate_periods = [p for p in periods if m[0] <= p[0] and p[1] <= m[1]]
if candidate_periods:
print m[0], "=>", min (candidate_periods)
else:
print m[0], "=>", "no period matches"

</code>

TJG



谢谢!我会尝试两种方式,看看是否有任何明显的速度差异,但因为它将被打包成一个

可执行文件,这可能不会无论如何都是一个问题。


迈克

Thanks! I''ll try it both ways and see if there''s any appreciable
difference in speed, although since it will be packaged into an
executable, that may not be an issue anyway.

Mike


谢谢!我会尝试两种方式,看看是否有任何明显的
Thanks! I''ll try it both ways and see if there''s any appreciable

的速度差异,尽管因为它将打包成一个

可执行文件,无论如何都可能不是问题。


Mike
difference in speed, although since it will be packaged into an
executable, that may not be an issue anyway.

Mike



我真的怀疑有没有对我的方法有利,当然不是速度方面的b $ b。它真的只是恰好适合你的想法更好,或者利用你可能已经拥有的结构,等等。


TJG

I honestly doubt there''s any advantage to my approach, certainly
not in terms of speed. It''s really only if it happens to suit
your mind better, or take advantage of structures you might
already have in place, etc.

TJG


这篇关于查找特定日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆