473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Finding all time periods for a given interval within a date range

Hi everyone,

I was wondering if anyone has written some Python code which uses a start
date and end date and a given interval (day, month or year) and outputs all
the time periods for that range and interval.

For example you may wish to find all the months between the dates
'2004-02-14' and '2004-08-04'. You would maybe use a function where you pass
in those starting and ending dates and the interval 'month' and you'd get
back a list of those months. I guess you would need to year part of the date
too, so ['2004-02', '2004-03', '2004-04', '2004-05', '2004-06', '2004-07',
'2004-08']. If you had passed in 'day' as the interval then you would be
given back a list of all the days in those months.

This is pretty specific but I would appreciate anything similar or even a
good pointing in the right direction of which modules to use. I've taken a
look at 'time' and 'calendar' but all I could see what the
'calendar.calendar' function which would require quite a bit of string
processing after getting the calendar strings.

Thanks for any help :-)
Graeme

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.732 / Virus Database: 486 - Release Date: 29/07/2004
__________________________________________________ ______________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
__________________________________________________ ______________________
Jul 18 '05 #1
1 3193
Graeme Longman wrote:
Hi everyone,

I was wondering if anyone has written some Python code which uses a start
date and end date and a given interval (day, month or year) and outputs all
the time periods for that range and interval.

For example you may wish to find all the months between the dates
'2004-02-14' and '2004-08-04'. You would maybe use a function where you pass
in those starting and ending dates and the interval 'month' and you'd get
back a list of those months. I guess you would need to year part of the date
too, so ['2004-02', '2004-03', '2004-04', '2004-05', '2004-06', '2004-07',
'2004-08']. If you had passed in 'day' as the interval then you would be
given back a list of all the days in those months.


Try using dateutil module by Gustavo Niemeyer found at:
https://moin.conectiva.com.br/DateUtil

from datetime import date
from dateutil.rrule import *
d1 = date(2004,2,14)
d2 = date(2004,8,4)
for d in rrule(MONTHLY, d1, until=d2):
print d.year, d.month

2004 2
2004 3
2004 4
2004 5
2004 6
2004 7

Substitute MONTHLY with DAILY in the rrule and you will get list of days

Waldek
Jul 18 '05 #2

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

Similar topics

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: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
3
by: CrystalDBA | last post by:
I am using SQL Server 2000. I need to query my database for all the contracts that came in during a certain time frame (user is prompted for reportingperiodid). Table - Periods Fields -...
18
by: Max | last post by:
This is a follow-up on my previous thread concerning having the program wait for a certain date and time and then executing some code when it gets there. My question is; can I use the Sleep...
2
by: pruebauno | last post by:
I am currently working on a tricky problem at work. I googled around a bit, but "time intervals" did not come up with anything useful. Although I have some rough idea of how I could solve it, I...
26
by: Lionel B | last post by:
Hi, Anyone know if the Standard has anything to say about the time complexity of size() for std::set? I need to access a set's size (/not/ to know if it is empty!) heavily during an algorithm...
14
by: --CELKO-- | last post by:
Can I get a little help? Given a very simple table of events, with their durations, I want to consolidate time periods for an event that (1) touch each other or (2) overlap each other. CREATE...
10
by: kyosohma | last post by:
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...
7
by: phill86 | last post by:
Hi, I have an equipment booking form that a user is able to book a piece of equipment on a selected date and time. I have written code to prevent the user being able to book the piece of equipment...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.