472,980 Members | 2,022 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,980 software developers and data experts.

Finding specific date ranges

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

Sep 6 '07 #1
10 2341
ky******@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:
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?

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
Sep 6 '07 #2
On Sep 6, 10:57 am, Tim Golden <m...@timgolden.me.ukwrote:
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:
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?

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

Sep 6 '07 #3
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
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
Sep 6 '07 #4
On Sep 6, 12:41 pm, Tim Golden <t...@timgolden.me.ukwrote:
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

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
For completeness, here's my method for comparison's sake:

<code>

date_list = [('12/31/2006', '01/13/2007'),
('01/14/2007', '01/27/2007'),
('01/28/2007', '02/10/2007'),
('02/11/2007', '02/24/2007'),
('02/25/2007', '03/10/2007'),
('03/11/2007', '03/24/2007'),
('03/25/2007', '04/07/2007'),
('04/08/2007', '04/21/2007'),
('04/22/2007', '05/05/2007'),
('05/06/2007', '05/19/2007'),
('05/20/2007', '06/02/2007'),
('06/03/2007', '06/16/2007'),
('06/17/2007', '06/30/2007')
]
vac_periods = []
found = False
for d in date_list:
begin = d[0][0:2]
end = d[1][0:2]
if begin == end and found == False:
vac_periods.append(d)
found = True
else:
found = False
print vac_periods

</code>

Isn't it kind of late in the day over there, Tim? Anyway, your method
is probably clearer to read whereas mine doesn't require anything to
be imported. I didn't even realize there was a calendar module...or
maybe I forgot about it.

Mike

Sep 6 '07 #5
ky******@gmail.com wrote:
On Sep 6, 12:41 pm, Tim Golden <t...@timgolden.me.ukwrote:
>>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
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

For completeness, here's my method for comparison's sake:
[... snipped ...]
Isn't it kind of late in the day over there, Tim?
(It's about 8.30pm as I write this, so still awake :)
Anyway, your method is probably clearer to read whereas mine
doesn't require anything to be imported.
I think I've been a SQL & Python programmer for so long that
I tend to do anything -- and sometimes too much -- to avoid
messy loop/exit-when structures. On reflection, my code
would be simplified by ignoring all the datetime.date stuff
which is irrelevant for the purpose, since tuples will
compare perfectly well. Also, I had to put the conditional
in the loop because I didn't have data for every month. If
you *knew* you had at least one period for each month and
weren't fussed about, say, date arithmetic later on, it
would be even simpler, just a min () function around
a generator expression.

Frankly, though, with code as small scale as this, it's
far more important what *you* the programmer and any
future maintainers will find easy to understand and use,
and far less important how clever the code is or how
fast it runs.
I didn't even realize there was a calendar module...or
maybe I forgot about it.
Even now, after some seven years of using Python, I'm
*still* surprised when I read someone's blog article or
mailing list posting referring to a module or application
I never knew about, or had completely forgotten.

TJG
Sep 6 '07 #6
On Sep 6, 2:39 pm, Tim Golden <t...@timgolden.me.ukwrote:
>
Anyway, your method is probably clearer to read whereas mine
doesn't require anything to be imported.

I think I've been a SQL & Python programmer for so long that
I tend to do anything -- and sometimes too much -- to avoid
messy loop/exit-when structures. On reflection, my code
would be simplified by ignoring all the datetime.date stuff
which is irrelevant for the purpose, since tuples will
compare perfectly well. Also, I had to put the conditional
in the loop because I didn't have data for every month. If
you *knew* you had at least one period for each month and
weren't fussed about, say, date arithmetic later on, it
would be even simpler, just a min () function around
a generator expression.

I'm getting pretty sick of for loops, flags and/or sentinel values. I
do *know* that there should be one (and only one) period per month
that applies. I just have to grab that period. I currently calculate
the date ranges on the fly based on the last date in the 2-value pair.
Ugh.

I've read an re-read about generators, but they seem to cause my mind
to go blank. It's like the related with statement or decorators. I
just don't understand them well enough to implement them.

Frankly, though, with code as small scale as this, it's
far more important what *you* the programmer and any
future maintainers will find easy to understand and use,
and far less important how clever the code is or how
fast it runs.
I agree. The simpler the code is to read and maintain, the better.
Clever, short code may be cool, but it can also be a pain to
troubleshoot.

Mike

Sep 6 '07 #7
On Sep 6, 7:56 am, kyoso...@gmail.com wrote:
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
Am I over simplifying if I say that since it appears to be a two week
pay period, the date has to be greater than the 11th unless the first,
or first and second, are on a weekend, in which case it would be 12
or 13? Or a reasonable facsimile thereof, depending on whether or
not the two week period is Saturday through Friday.

Sep 6 '07 #8
Zentrader wrote:
On Sep 6, 7:56 am, kyoso...@gmail.com wrote:
>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

Am I over simplifying if I say that since it appears to be a two week
pay period, the date has to be greater than the 11th unless the first,
or first and second, are on a weekend, in which case it would be 12
or 13? Or a reasonable facsimile thereof, depending on whether or
not the two week period is Saturday through Friday.
I think it's one of those things where the neatest answer
could well depend on the sort of heuristic you mention. As
a rule, when I come across this kind of requirement, I tend
to put the most general solution in place, unless a *real*
optimisation is clearly called for. In my experience, this
makes it much easier for the next person who looks at the
code, typically years later, even if that's me!

(This is has just happened to me this week, having to review
a date-related calculation to do the repost frequency of the
adverts my company deals with. I wrote the original code five
years ago, and commented it intelligently, but I *still* had
to work through the code twice when we had a problem with a
particular cycle!)

TJG
Sep 7 '07 #9
On Sep 6, 6:41 pm, Zentrader <zentrad...@gmail.comwrote:
On Sep 6, 7:56 am, kyoso...@gmail.com wrote:
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

Am I over simplifying if I say that since it appears to be a two week
pay period, the date has to be greater than the 11th unless the first,
or first and second, are on a weekend, in which case it would be 12
or 13? Or a reasonable facsimile thereof, depending on whether or
not the two week period is Saturday through Friday.
Yeah it is a 2-week period, but the starting day can change depending
on whether or not you're salaried. I haven't even looked at how we
handle unions. Ugh. But yes, you are correct.

Mike

Sep 7 '07 #10
Tim Golden wrote:
ky******@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:
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
What about (untested) :
import datetime

def itEarns(fromDate, toDate) :
return (fromDate.year == toDate.year and fromDate.month == toDate.month)

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))
]

candidatePeriods = [(frm, to) for frm, to in periods if itEarns(frm, to)]

??
>>
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?


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

Sep 7 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ajay | last post by:
hi! i am reading a file on the Web. How can i find out when it was last modified? thanks
2
by: Maksim Kasimov | last post by:
there are few of a time periods, for example: 2005-06-08 12:30 -> 2005-06-10 15:30, 2005-06-12 12:30 -> 2005-06-14 15:30 and there is some date and time value: 2005-06-11 12:30 what is the...
2
by: bobb | last post by:
Is there a way to select count(*) by grouping by date, and having multiple date ranges? combining... select field,count(*) from table where datefield > 2004/1/1 and datefield < 2004/1/31 and...
7
by: Thomas R. Hummel | last post by:
Hello all, I am trying to write a query that compares a member's enrollment period with the products that their group has had during that period (all members belong to a group and the products...
12
by: Steve Elliott | last post by:
I have a query set up to gather together data between two specified dates. Shown in the query column as: Between #24/09/2004# And #01/10/2004# Is it possible to enter several different date...
67
by: PC Datasheet | last post by:
Transaction data is given with date ranges: Beginning End 4/1/06 4/4/06 4/7/06 4/11/06 4/14/06 4/17/06 4/18/06 4/21/06 426/06 ...
4
by: norma.j.hildebrand | last post by:
I have a database that has a field (performance standard), every year the standard changed which was not a problem since we start out each year with a blank database and just change the standards...
16
by: Alex30093 | last post by:
OK This is my 1st post to any forum, but I rely on forums all the time. Short story is I find myself needing to become a Access DBA for work. Situation: I want to use one table of events that...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.