Re: formatted 'time' data in calculations
Scott David Daniels wrote: Ross wrote: There seems to be no shortage of information around on how to use the time module, for example to use time.ctime() and push it into strftime and get something nice out the other side, but I haven't found anything helpful in going the other way. As to a paucity of conversion formatting, there is no magic way to take everyone's way of putting date and time information in text and convert it to unambiguous format, in part because there are too many different and contradictory formats. When I write dates, I know what I intended; when I read dates, I guess what the author intended. Have you read the entire time module document? If so, which functions in that module take strings as arguments? That is, given some formatted text describing times - is there something that makes it easy to calculate time differences, or do I have to index my way through the string pulling out characters, converting to integers etc... Data is formatted: t1 = 09:12:10 t2 = 11:22:14 I want to calculate tdiff = t2-t1 Do you do any work yourself? Show us your attempts. This looks like a trivial exercise. It seems that for less than four times the effort of asking your question you might have found the answer. Perhaps I am being too cranky this morning. --Scott David Daniels scott.dani...@acm.org Jeeze, you're quite an ass aren't you? -- http://mail.python.org/mailman/listinfo/python-list
Re: formatted 'time' data in calculations
Thanks Chris and Diez for the quick pointers... Very helpful Ross. -- http://mail.python.org/mailman/listinfo/python-list
Re: formatted 'time' data in calculations
Ross wrote: Scott David Daniels wrote: Ross wrote: There seems to be no shortage of information around on how to use the time module, for example to use time.ctime() and push it into strftime and get something nice out the other side, but I haven't found anything helpful in going the other way. As to a paucity of conversion formatting, there is no magic way to take everyone's way of putting date and time information in text and convert it to unambiguous format, in part because there are too many different and contradictory formats. When I write dates, I know what I intended; when I read dates, I guess what the author intended. Have you read the entire time module document? If so, which functions in that module take strings as arguments? That is, given some formatted text describing times - is there something that makes it easy to calculate time differences, or do I have to index my way through the string pulling out characters, converting to integers etc... Data is formatted: t1 = 09:12:10 t2 = 11:22:14 I want to calculate tdiff = t2-t1 Do you do any work yourself? Show us your attempts. This looks like a trivial exercise. It seems that for less than four times the effort of asking your question you might have found the answer. Perhaps I am being too cranky this morning. Jeeze, you're quite an ass aren't you? And how did sending this message improve things? Let's keep it civil, guys. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
formatted 'time' data in calculations
There seems to be no shortage of information around on how to use the time module, for example to use time.ctime() and push it into strftime and get something nice out the other side, but I haven't found anything helpful in going the other way. That is, given some formatted text describing times - is there something that makes it easy to calculate time differences, or do I have to index my way through the string pulling out characters, converting to integers etc... Data is formatted: t1 = 09:12:10 t2 = 11:22:14 I want to calculate tdiff = t2-t1 Any suggestions? (Thanks for anything you can offer) -Ross -- http://mail.python.org/mailman/listinfo/python-list
Re: formatted 'time' data in calculations
Ross wrote: There seems to be no shortage of information around on how to use the time module, for example to use time.ctime() and push it into strftime and get something nice out the other side, but I haven't found anything helpful in going the other way. That is, given some formatted text describing times - is there something that makes it easy to calculate time differences, or do I have to index my way through the string pulling out characters, converting to integers etc... Data is formatted: t1 = 09:12:10 t2 = 11:22:14 I want to calculate tdiff = t2-t1 Any suggestions? (Thanks for anything you can offer) The datetime module contains everything you need. Look at the strptime-function that will allow you to parse the above string to an actual datetime.time-object, and the you can subtract these to yield a datetime.timedelta-object. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: formatted 'time' data in calculations
On Wed, Jan 7, 2009 at 9:35 AM, Ross nos...@forme.thks wrote: There seems to be no shortage of information around on how to use the time module, for example to use time.ctime() and push it into strftime and get something nice out the other side, but I haven't found anything helpful in going the other way. That is, given some formatted text describing times - is there something that makes it easy to calculate time differences, or do I have to index my way through the string pulling out characters, converting to integers etc... Data is formatted: t1 = 09:12:10 t2 = 11:22:14 I want to calculate tdiff = t2-t1 Any suggestions? (Thanks for anything you can offer) Use the `datetime` class in the `datetime` module (http://docs.python.org/library/datetime.html). It has a class method .strptime() to parse a string into a `datetime` object. You can then subtract one `datetime` from another to produce a `timedelta` object representing the difference between them. Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: formatted 'time' data in calculations
Here is how I have done adjustments to time in the past. This is mostly Date related but it may help today = datetime.date.today() wkdiff = datetime.timedelta(weeks=1) daydiff = datetime.timedelta(days=1) startdate=(today-wkdiff)-daydiff this will subtract 1 week and 1 day from today. Stephen Ross wrote: There seems to be no shortage of information around on how to use the time module, for example to use time.ctime() and push it into strftime and get something nice out the other side, but I haven't found anything helpful in going the other way. That is, given some formatted text describing times - is there something that makes it easy to calculate time differences, or do I have to index my way through the string pulling out characters, converting to integers etc... Data is formatted: t1 = 09:12:10 t2 = 11:22:14 I want to calculate tdiff = t2-t1 Any suggestions? (Thanks for anything you can offer) -Ross -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: formatted 'time' data in calculations
Thanks Chris and Diez for the quick pointers... Very helpful Ross. -- http://mail.python.org/mailman/listinfo/python-list
Re: formatted 'time' data in calculations
Ross wrote: There seems to be no shortage of information around on how to use the time module, for example to use time.ctime() and push it into strftime and get something nice out the other side, but I haven't found anything helpful in going the other way. As to a paucity of conversion formatting, there is no magic way to take everyone's way of putting date and time information in text and convert it to unambiguous format, in part because there are too many different and contradictory formats. When I write dates, I know what I intended; when I read dates, I guess what the author intended. Have you read the entire time module document? If so, which functions in that module take strings as arguments? That is, given some formatted text describing times - is there something that makes it easy to calculate time differences, or do I have to index my way through the string pulling out characters, converting to integers etc... Data is formatted: t1 = 09:12:10 t2 = 11:22:14 I want to calculate tdiff = t2-t1 Do you do any work yourself? Show us your attempts. This looks like a trivial exercise. It seems that for less than four times the effort of asking your question you might have found the answer. Perhaps I am being too cranky this morning. --Scott David Daniels scott.dani...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Re: formatted 'time' data in calculations
I personally use epoch time since its absolute. I have a simple time clock app that uses this method, from that it is easy to convert into human date: [code] def OnButtonIn(self,evt): 'create time stamp with ID/action' 'print to the rightFrame text' if self.punchedIn: print Already punched in cannot punch again else: seconds = time.time() current = time.localtime(seconds) day = time.localtime() # time string can have characters 0..9, -, period, or space timeday = time.strftime('%H:%M-%m.%d.%y', day) formatday = time.strftime('%c', day) self.ClockIntext = seconds #self.text = Clock in: + self.text self.punchIn.SetValue(formatday) self.punchedIn = True self.punchedOut = False def OnButtonOut(self,evt): if self.punchedOut: print Already punched out! else: 'create time stamp with ID/action' 'print to the rightFrame text' 'write to time sheet' seconds = time.time() current = time.localtime(seconds) day = time.localtime() # time string can have characters 0..9, -, period, or space timeday = time.strftime('%H:%M-%m.%d.%y', day) formatday = time.strftime('%c', day) self.ClockOuttext = seconds self.punchOut.SetValue(formatday) self.punchedIn = False self.punchedOut = True 'create a file and write the table to it' file = open('timesheet.txt', 'a') file.write(str(self.ClockIntext)) file.write('\t') file.write(str(self.ClockOuttext)) file.write('\n') file.close() return None def OnButtonCalc(self,event): 'open the time sheet and calculate the total time' file = open('timesheet.txt','r') lines = file.readlines() time1 = '' time2 = '' self.hours = 0.000 for punch in lines: for x in punch: if(len(x) = 0): self.hours = self.hours else: if x != '\t' and x!= '\n': time1 = time1 + x elif x == '\t': time2 = time1 time1 = '' elif x == '\n': self.hours = self.hours + abs(((float(time2)-float(time1))/60)/60) time2 = '' time1 = '' self.total.SetValue('%2f' % self.hours) [/code] Oops wrong person, sorry about that. This time it should go to the mailing list Aaron Hill -- http://mail.python.org/mailman/listinfo/python-list
Re: formatted 'time' data in calculations
On Jan 8, 6:23 am, Scott David Daniels scott.dani...@acm.org wrote: Ross wrote: There seems to be no shortage of information around on how to use the time module, for example to use time.ctime() and push it into strftime and get something nice out the other side, but I haven't found anything helpful in going the other way. As to a paucity of conversion formatting, there is no magic way to take everyone's way of putting date and time information in text and convert it to unambiguous format, in part because there are too many different and contradictory formats. When I write dates, I know what I intended; when I read dates, I guess what the author intended. Have you read the entire time module document? If so, which functions in that module take strings as arguments? That is, given some formatted text describing times - is there something that makes it easy to calculate time differences, or do I have to index my way through the string pulling out characters, converting to integers etc... Data is formatted: t1 = 09:12:10 t2 = 11:22:14 I want to calculate tdiff = t2-t1 Do you do any work yourself? Show us your attempts. This looks like a trivial exercise. It seems that for less than four times the effort of asking your question you might have found the answer. Perhaps I am being too cranky this morning. Indeed. Be not cranky at clueless bludgers and cargo-cultists lest they rise high in the serried ranks of IT management and remember your name inclusive-or the net-nannies sally forth and wallop thee with a balloon on a stick. To the OP: Your Lordship did not specify whether output should be expressed in hours and a fraction, or in hours, minutes, and seconds. So that the eminent one may avoid having to make a decision in public, I humbly submit answers to both possibilities: t1 = 09:12:10 t2 = 11:22:14 sum((a - b) / c for (a, b, c) in zip(map(int, t2.split(:)), map(int,t1.split(:)), (1., 60., 3600.))) 2.1678 def tdiff(t1, t2): ...h, m, s = [a - b for (a, b) in zip(map(int, t2.split(:)), map (int,t1.split(:)))] ...if s 0: ... s += 60 ... m -= 1 ...if m 0: ... m += 60 ... h -= 1 ...return h, m, s ... tdiff(t1, t2) (2, 10, 4) tdiff('09:12:10', '10:11:09') (0, 58, 59) HTH, John -- http://mail.python.org/mailman/listinfo/python-list
Re: formatted 'time' data in calculations
John Machin wrote: On Jan 8, 6:23 am, Scott David Daniels scott.dani...@acm.org wrote: ...some stuff perhaps too cranky... Have you read the entire time module document? If so, which functions in that module take strings as arguments? then even more cranky stuff... Indeed. Be not cranky at clueless bludgers and cargo-cultists And since others are pointing you at datetime and othert places, I'll just point out that I _did_, in fact, answer your question: (1) The answer to the oblique clue is strptime. (2) Only mktime takes a time_struct and returns a non-string. import time struct_time1 = time.strptime('14:11-01.07.09', '%H:%M-%m.%d.%y') struct_time2 = time.strptime('14:18-01.07.09', '%H:%M-%m.%d.%y') since_epoch1 = time.mktime(struct_time1) since_epoch2 = time.mktime(struct_time2) print int((since_epoch2 - since_epoch1) / 10) H where have I seen that number before? A slightly less cranky kid because of the mollifying influence of the ever-diplomatic John Machin. --Scott David Daniels scott.dani...@acm.org -- http://mail.python.org/mailman/listinfo/python-list