473,654 Members | 3,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

date and time range checking


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:
a = xrange(1,5)
b = 3
if b in a:

.... print "OK"
thanks

--
Best regards,
Maksim Kasimov
mailto: ka*****@i.com.u a
Jul 19 '05 #1
2 6478
Maksim Kasimov wrote:
what is the "pythonic" way to check is the date/time value in the given
periods range?


Something like this, though I won't make strong claims of
"pythonicne ss". If you want to use the "in" keyword you'll want a
custom class and overriding of __contains__.

import time
from datetime import datetime

def make_datetime(s , fmt='%Y-%m-%d %H:%M'):
'''convert string to datetime'''
ts = time.mktime(tim e.strptime(s, fmt))
return datetime.fromti mestamp(ts)
def inRange(s, ranges):
dt = make_datetime(s )
for begin,end in ranges:
if begin <= dt <= end:
return True
else:
return False
ranges = [(make_datetime( b), make_datetime(e )) for (b,e) in [
('2005-06-08 12:30', '2005-06-10 15:30'),
('2005-06-12 12:30', '2005-06-14 15:30'),
]]

print inRange('2005-06-11 12:30', ranges)
Jul 19 '05 #2
Maksim Kasimov wrote:
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?


import datetime
t1 = datetime.dateti me(2005, 6, 8, 12, 30)
t2 = datetime.dateti me(2005, 6, 10, 15, 30)
t = datetime.dateti me(2005, 6, 9, 14, 00)
if t1 < t < t2: .... print "In range"
....
In range t = datetime.dateti me(2005, 6, 8, 14, 00)
if t1 < t < t2: .... print "In range"
....
In range t = datetime.dateti me(2005, 6, 7, 14, 00)

if t1 < t < t2: .... print "In range"
....
If you want to use the "in" syntax
class InRange: .... def __init__(self, low, high):
.... self.low = low
.... self.high = high
.... def __contains__(se lf, obj):
.... return self.low < obj < self.high
.... r = InRange(t1, t2)
datetime.dateti me(2005, 6, 7, 14, 00) in r False datetime.dateti me(2005, 6, 8, 14, 00) in r True datetime.dateti me(2005, 6, 9, 14, 00) in r True datetime.dateti me(2005, 6, 9, 18, 00) in r True datetime.dateti me(2005, 6, 10, 18, 00) in r False


Andrew
da***@dalkescie ntific.com

Jul 19 '05 #3

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

Similar topics

2
5210
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
4
2998
by: Lynn | last post by:
On a form I have Date_Start Date_End I have a new Date_Start1 Date_End1 which the use inputs. I need to validate that Date_Start1 and Date_End1 are a date range entirely separate from Date_Start and Date_End i.e. no overlapping
30
3669
by: Dr John Stockton | last post by:
It has appeared that ancient sources give a method for Numeric Date Validation that involves numerous tests to determine month length; versions are often posted by incomers here. That sort of code seems unnecessarily long. For some while, the following approach has been given here :- function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
2
2498
by: Noozer | last post by:
I have a textbox on one of my forms that is used to accept a time from the user. I need to write this value to a database to trigger an event later on. What I'd like to know is... Are there any functions in javascript that can convert different time formats to a known format (24 hour clock)... 5pm -> 17:00 or 5:00 to 05:00, etc. ??? Assuming that I get the input into a 24 hour format, how can I create a full
3
7455
by: manning_news | last post by:
Using A2K. I've been asked to modify a report currently requiring only one date parameter to now accept a date range. The main report has 2 subreports and is not bound to a table or query. The report prints dental and hygenist appointments for the date (one subreport for each). The user wants to enter a date range and have one page for each date in the date range. I'm wondering how to modify the report. The only way I see is to create...
18
38226
by: dfetrow410 | last post by:
Anyone have some code that will do this? Dave
26
7193
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,
62
2338
by: Generic Usenet Account | last post by:
A lot of research has been done to prove that the contention that C code is more efficient and more compact than equivalent C++ code is a myth. My posting pertains to a slightly different aspect of this debate. Here are my two questions: 1) Does anyone have any information on comparison of C and C++ software written for the ARM processor? 2) Are there any compiler and CPU dependencies that have to be factored in while debating this...
12
3265
smithj14
by: smithj14 | last post by:
I have a form to enter start and end dates then select a worker name to filter a report. This all works fine and when the report is open in preview mode it shows the date range in the txtboxes on the report header, but when I print the report the value for these txtboxes are #Name? Here is the code Date Range Form: Private Sub cmdViewErrorReport_Click() Dim stDocName As String Dim stDocName2 As String Dim stWhere As String...
0
8372
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
8285
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
8706
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
8475
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
8591
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
6160
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
5621
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1592
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.