473,385 Members | 1,907 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.

how to find a time between the two given time

hello ,
i need a help regarding python programming:

basically i need to find out times between the start and the stop time.
Here is what i have tried---


Expand|Select|Wrap|Line Numbers
  1. s = raw_input('enter first time (hh:mm:ss ):')
  2. z = raw_input('enter the second time (hh:mm:ss):')
  3. if ':' in s:
  4.  h, m, ss = s.split(':')
  5.  q = int(h)*3600 + float(m)* 60 + float(ss)
  6.  
  7. if ':' in z:
  8.  hh, mm, sss = z.split(':')
  9.  p = int(hh)*3600 + float(mm)* 60 + float(sss)


Now here i have taken out the value of q and p .
Now how do i take a variable "tm" and compare the time as

q <= tm <= p

and then use the variable tm in my other processing
Mar 24 '11 #1
3 3558
bvdet
2,851 Expert Mod 2GB
You can use the time module for date and time math. time.strptime returns a struct_time object by parsing a string representing a time according to a format. time.localtime() returns a struct_time object representing the current time. A struct_time object is iterable as a tuple, so a given time and current time can be combined by slicing. Then you can easily perform math and comparisons on the objects. Example:
Expand|Select|Wrap|Line Numbers
  1. import time
  2.  
  3. t1 = "12:24:33"
  4. t2 = "14:56:12"
  5.  
  6. tObj1 = time.mktime(time.localtime()[:3]+time.strptime(t1, "%H:%M:%S")[3:])
  7. tObj2 = time.mktime(time.localtime()[:3]+time.strptime(t2, "%H:%M:%S")[3:])
  8.  
  9. diff = tObj2-tObj1
  10. print tObj1 > tObj2
  11. print diff
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> False
  2. 9099.0
  3. >>> 
Mar 27 '11 #2
Hello bvdet !!

Actually my query about my program is that:
i have to make a program which takes the two times (start time and end time) from the user. Then, i take the third time interval (say in minutes)from the user.

Now in my program:
suppose the user gives, start time : 12:00:00 and end time: 13:00:00 and time interval : 5 minutes

so it will compare in my file XYZ.TXT the start time string "12:00:00" . If in the file it finds the time say, 12:30:20 then it starts from this time and after every 5 minutes (time interval) it reads from the file and prints the time , which should lie between 13:00:00 (end time)

please do help me with this !!
Mar 31 '11 #3
bvdet
2,851 Expert Mod 2GB
I have shown you how to create a struct_time object with the time module. To increment a given struct_time object by 5 minutes:
Expand|Select|Wrap|Line Numbers
  1. >>> time.ctime(tObj1+5*60)
  2. 'Thu Mar 31 12:29:33 2011'
  3. >>> 
The time can be extracted from the string using string methods:
Expand|Select|Wrap|Line Numbers
  1. >>> time.ctime(tObj1+5*60).split()[3]
  2. '12:29:33'
  3. >>> 
Perhaps you can study python documentation on the time module and the example code I have posted to develop the code for your application. Post back if you have any specific questions about your code.
Mar 31 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

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 -...
1
by: Lei Guangfu | last post by:
Hi, I have a program need to check the value of a application available, and then decide whether to execute codes followed. In order to achieve this purpose, I use DO WHILE loop to judge the...
1
by: Lei Guangfu | last post by:
Hi, I have a program need to delay for a given time, I use DO WHILE loop to achieve this goal, but this method consumes too much CPU resource. So I want to find a way to pause a process for a...
3
by: Russell Warren | last post by:
Does anyone know how long it takes for time.clock() to roll over under win32? I'm aware that it uses QueryPerformanceCounter under win32... when I've used this in the past (other languages) it...
9
by: Ron Adam | last post by:
I'm having some cross platform issues with timing loops. It seems time.time is better for some computers/platforms and time.clock others, but it's not always clear which, so I came up with the...
2
by: Mathias Weyel | last post by:
Hi there, I have time-of-day spans presented as strings and want to check, if, at a given time, I am within such a time span. Example: Timespan-string is given as follows: "09:00 , 16:00" A...
5
by: bobthebuilder | last post by:
i am a beginner in c# and i was given a time in-time out program assignment. can anybody help me start this project?
1
by: sasasasa | last post by:
How can I loop through each date in a given time period so that I can add those dates as a column name in a table. I want to create datatable with the person's name and hours he worked in a given...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.