473,748 Members | 2,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.calen dar' 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 3212
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
6485
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 "pythonic" way to check is the date/time value in the given periods range? something like xrange:
2
5218
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 appreciated. TIA
3
17914
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 - Reporting Period id int Reporting Period desc varchar(30) Reporting Period Begin Date datetime Reporting Period End Date datetime
18
3269
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 function from kernel32 to accomplish this? My concern is that this function takes milliseconds where my program needs to be accurate to within 5 minuets, and delays may be as long as a number of days, months or whatever. Would I run into many problems...
2
1821
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 still would value some input. I have information of (It has only couple dozen entries.) ServiceNum, DollarCost and a input database table in the form (several GBytes): ClientNumber (CN), BeginDate(BD), EndDate(ED),
26
7205
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 and was thus wondering whether I'd be better off tracking the size "manually" or whether that'd be pointless. Thanks,
14
2030
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 TABLE Events (event_id VARCHAR(25) NOT NULL, start_date DATE NOT NULL, end_date DATE NOT NULL, CHECK (start_date <= end_date), PRIMARY KEY (event_id, start_date, end_date));
10
2385
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 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
7
2051
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 if it has already been booked by a different user on the same date/time. I want to display in a time grid format when that piece of equipment is booked. I currently have 16 green coloured boxes on my form each representing an hour's time...
0
8995
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9558
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9331
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9253
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6798
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2216
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.