473,554 Members | 3,220 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 6464
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
5203
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
2993
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
3643
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...
2
2494
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. ??? ...
3
7450
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...
18
38218
by: dfetrow410 | last post by:
Anyone have some code that will do this? Dave
26
7170
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
2313
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?...
12
3254
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...
0
7589
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...
0
7787
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. ...
0
8029
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...
1
5428
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...
0
3550
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...
0
3539
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1121
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
831
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...

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.