Re: calculate difference between two timestamps [newbie]
On 18 dec, 13:58, Lie Ryan wrote: > On 12/18/2011 10:43 PM, Peter Otten wrote: > > > > > > > > > > > nukeymusic wrote: > > >> On 17 dec, 12:20, "Günther Dietrich" wrote: > >>> nukeymusic wrote: > I'm trying to calculate the difference in seconds between two > > >>> [...] > > >> import datetime > >> date1 = datetime.datetime.strptime("Dec-13-09:47:12", > >> "%b-%d-%H:%M:%S") date2 = > >> datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") delta > >> = date2 - date1 delta_seconds = (delta.days * 60 * 60 * 24) + > >> delta.seconds + ((delta.microseconds + 50) / 100) > > >>> For very big time differences you should consider to use the Decimal > >>> arithmetics (standard module Decimal) instead of integer arithmetics > >>> for the last line. > >>> If you are sure, that you don't use fractional seconds, you can omit > >>> the part with 'delta.microseconds'. > > >>> Best regards, > > >>> Günther > >> That can very much Günther, this helped me a lot further, I'm only > >> struggling with one more problem to finish my first python-program. > >> Could you > >> tell me why I can't write to the outputfile as I do in the code > >> below:? > >> #!/usr/bin/python > >> #version 16/12/2011 > >> #Example of testfile > >> #Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 > >> #Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 > >> #Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 > >> import datetime > >> f = open('testfile','r') > >> g = open('outputfile','w') > >> #get line 1 from input file: > >> line1=f.readline() > >> #get first element in line 1: > >> date1=line1.rsplit()[0] > >> #convert first element tot structured date time > >> struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") > >> for line in f: > >> temp=line.rsplit() > >> delta=datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S")- > >> datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") > >> delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + > >> ((delta.microseconds + 50) / 100) > >> temp[0]=delta_seconds > >> #the following line is wrong, but I don't know how to fix it: > >> g.write(temp) > >> #Close files > >> f.close() > >> g.close() > > > The write() method only accepts strings; you have to convert the temp list > > to a string before passing it on. The minimal change: > > > for line in f: > > temp = line.rsplit() > > delta = (datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S") > > -datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S")) > > delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds > > + ((delta.microseconds + 50) / 100)) > > temp[0] = str(delta_seconds) > > g.write(" ".join(temp) + "\n") > > > Other observations: > > > - you are repeating calculations in the loop that you can do (and did) > > outside. > > > - use four-space indent for better readability > > > - there's no need to use rsplit(); use split() > > > After a few other modifications: > > > import datetime > > > def parse_line(line): > > date, rest = line.split(None, 1) > > date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") > > return date, rest > > > with open('testfile','r') as f: > > with open('outputfile','w') as g: > > first_date, first_rest = parse_line(next(f)) > > for line in f: > > cur_date, rest = parse_line(line) > > delta = cur_date - first_date > > delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds > > + ((delta.microseconds + 50) / 100)) > > g.write("%s %s" % (delta_seconds, rest)) > > minor improvement, you can do: > > with open('testfile','r') as f, open('outputfile','w') as g: > ... > > instead of the nested with-block. > > Also, you can use `delta.total_seconds()` instead of `delta_seconds = > ((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + > 50) / 100))` > > Therefore (untested): > > import datetime > > def parse_line(line): > date, rest = line.split(None, 1) > date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") > return date, rest > > with open('testfile','r') as f, open('outputfile','w') as g: > first_date, first_rest = parse_line(next(f)) > for line in f: > cur_date, rest = parse_line(line) > delta = cur_date - first_date > g.write("%s %s" % (int(round(delta.total_seconds())), rest)) thanks to you and all the others for helping me out with my first python-script. I tested your alternatives and they do work. The only minor inconvenience with the scripts is that the output-file misses the first line i.e. the first timestamp should be reset to zero (seconds), but as these log-files are very large, missing one measurement is not too bad best regards nukey -- http://mail.python.org/mailman/listinfo/python-list
Re: calculate difference between two timestamps [newbie]
On 18 dec, 16:01, Peter Otten <__pete...@web.de> wrote: > nukeymusic wrote: > > thanks and also thanks to all the others who were so kind to help me > > out with my first python-script. > > I tested your alternatives and they work, the only a minor > > inconvenience is that the first line of the inputfile gets lost i.e. > > the first timestamp should become zero (seconds) > > That should be easy to fix: > > >> with open('testfile','r') as f, open('outputfile','w') as g: > >> first_date, first_rest = parse_line(next(f)) > > g.write("0 %s" % first_rest) > > > > > > > > >> for line in f: > >> cur_date, rest = parse_line(line) > >> delta = cur_date - first_date > >> g.write("%s %s" % (int(round(delta.total_seconds())), rest)) thanks, that solves it, you're too kind nukey -- http://mail.python.org/mailman/listinfo/python-list
Re: calculate difference between two timestamps [newbie]
nukeymusic wrote: > thanks and also thanks to all the others who were so kind to help me > out with my first python-script. > I tested your alternatives and they work, the only a minor > inconvenience is that the first line of the inputfile gets lost i.e. > the first timestamp should become zero (seconds) That should be easy to fix: >> with open('testfile','r') as f, open('outputfile','w') as g: >> first_date, first_rest = parse_line(next(f)) g.write("0 %s" % first_rest) >> for line in f: >> cur_date, rest = parse_line(line) >> delta = cur_date - first_date >> g.write("%s %s" % (int(round(delta.total_seconds())), rest)) -- http://mail.python.org/mailman/listinfo/python-list
Re: calculate difference between two timestamps [newbie]
On 18 dec, 13:58, Lie Ryan wrote: > On 12/18/2011 10:43 PM, Peter Otten wrote: > > > > > > > > > > > nukeymusic wrote: > > >> On 17 dec, 12:20, "Günther Dietrich" wrote: > >>> nukeymusic wrote: > I'm trying to calculate the difference in seconds between two > > >>> [...] > > >> import datetime > >> date1 = datetime.datetime.strptime("Dec-13-09:47:12", > >> "%b-%d-%H:%M:%S") date2 = > >> datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") delta > >> = date2 - date1 delta_seconds = (delta.days * 60 * 60 * 24) + > >> delta.seconds + ((delta.microseconds + 50) / 100) > > >>> For very big time differences you should consider to use the Decimal > >>> arithmetics (standard module Decimal) instead of integer arithmetics > >>> for the last line. > >>> If you are sure, that you don't use fractional seconds, you can omit > >>> the part with 'delta.microseconds'. > > >>> Best regards, > > >>> Günther > >> That can very much Günther, this helped me a lot further, I'm only > >> struggling with one more problem to finish my first python-program. > >> Could you > >> tell me why I can't write to the outputfile as I do in the code > >> below:? > >> #!/usr/bin/python > >> #version 16/12/2011 > >> #Example of testfile > >> #Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 > >> #Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 > >> #Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 > >> import datetime > >> f = open('testfile','r') > >> g = open('outputfile','w') > >> #get line 1 from input file: > >> line1=f.readline() > >> #get first element in line 1: > >> date1=line1.rsplit()[0] > >> #convert first element tot structured date time > >> struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") > >> for line in f: > >> temp=line.rsplit() > >> delta=datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S")- > >> datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") > >> delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + > >> ((delta.microseconds + 50) / 100) > >> temp[0]=delta_seconds > >> #the following line is wrong, but I don't know how to fix it: > >> g.write(temp) > >> #Close files > >> f.close() > >> g.close() > > > The write() method only accepts strings; you have to convert the temp list > > to a string before passing it on. The minimal change: > > > for line in f: > > temp = line.rsplit() > > delta = (datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S") > > -datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S")) > > delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds > > + ((delta.microseconds + 50) / 100)) > > temp[0] = str(delta_seconds) > > g.write(" ".join(temp) + "\n") > > > Other observations: > > > - you are repeating calculations in the loop that you can do (and did) > > outside. > > > - use four-space indent for better readability > > > - there's no need to use rsplit(); use split() > > > After a few other modifications: > > > import datetime > > > def parse_line(line): > > date, rest = line.split(None, 1) > > date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") > > return date, rest > > > with open('testfile','r') as f: > > with open('outputfile','w') as g: > > first_date, first_rest = parse_line(next(f)) > > for line in f: > > cur_date, rest = parse_line(line) > > delta = cur_date - first_date > > delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds > > + ((delta.microseconds + 50) / 100)) > > g.write("%s %s" % (delta_seconds, rest)) > > minor improvement, you can do: > > with open('testfile','r') as f, open('outputfile','w') as g: > ... > > instead of the nested with-block. > > Also, you can use `delta.total_seconds()` instead of `delta_seconds = > ((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + > 50) / 100))` > > Therefore (untested): > > import datetime > > def parse_line(line): > date, rest = line.split(None, 1) > date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") > return date, rest > > with open('testfile','r') as f, open('outputfile','w') as g: > first_date, first_rest = parse_line(next(f)) > for line in f: > cur_date, rest = parse_line(line) > delta = cur_date - first_date > g.write("%s %s" % (int(round(delta.total_seconds())), rest)) thanks and also thanks to all the others who were so kind to help me out with my first python-script. I tested your alternatives and they work, the only a minor inconvenience is that the first line of the inputfile gets lost i.e. the first timestamp should become zero (seconds) best regards, nukey -- http://mail.python.org/mailman/listinfo/python-list
Re: calculate difference between two timestamps [newbie]
On 12/18/2011 10:43 PM, Peter Otten wrote: nukeymusic wrote: On 17 dec, 12:20, "Günther Dietrich" wrote: nukeymusic wrote: I'm trying to calculate the difference in seconds between two [...] import datetime date1 = datetime.datetime.strptime("Dec-13-09:47:12", "%b-%d-%H:%M:%S") date2 = datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") delta = date2 - date1 delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 50) / 100) For very big time differences you should consider to use the Decimal arithmetics (standard module Decimal) instead of integer arithmetics for the last line. If you are sure, that you don't use fractional seconds, you can omit the part with 'delta.microseconds'. Best regards, Günther That can very much Günther, this helped me a lot further, I'm only struggling with one more problem to finish my first python-program. Could you tell me why I can't write to the outputfile as I do in the code below:? #!/usr/bin/python #version 16/12/2011 #Example of testfile #Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 #Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 #Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 import datetime f = open('testfile','r') g = open('outputfile','w') #get line 1 from input file: line1=f.readline() #get first element in line 1: date1=line1.rsplit()[0] #convert first element tot structured date time struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") for line in f: temp=line.rsplit() delta=datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S")- datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 50) / 100) temp[0]=delta_seconds #the following line is wrong, but I don't know how to fix it: g.write(temp) #Close files f.close() g.close() The write() method only accepts strings; you have to convert the temp list to a string before passing it on. The minimal change: for line in f: temp = line.rsplit() delta = (datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S") -datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S")) delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 50) / 100)) temp[0] = str(delta_seconds) g.write(" ".join(temp) + "\n") Other observations: - you are repeating calculations in the loop that you can do (and did) outside. - use four-space indent for better readability - there's no need to use rsplit(); use split() After a few other modifications: import datetime def parse_line(line): date, rest = line.split(None, 1) date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") return date, rest with open('testfile','r') as f: with open('outputfile','w') as g: first_date, first_rest = parse_line(next(f)) for line in f: cur_date, rest = parse_line(line) delta = cur_date - first_date delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 50) / 100)) g.write("%s %s" % (delta_seconds, rest)) minor improvement, you can do: with open('testfile','r') as f, open('outputfile','w') as g: ... instead of the nested with-block. Also, you can use `delta.total_seconds()` instead of `delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 50) / 100))` Therefore (untested): import datetime def parse_line(line): date, rest = line.split(None, 1) date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") return date, rest with open('testfile','r') as f, open('outputfile','w') as g: first_date, first_rest = parse_line(next(f)) for line in f: cur_date, rest = parse_line(line) delta = cur_date - first_date g.write("%s %s" % (int(round(delta.total_seconds())), rest)) -- http://mail.python.org/mailman/listinfo/python-list
Re: calculate difference between two timestamps [newbie]
nukeymusic wrote: > On 17 dec, 12:20, "Günther Dietrich" wrote: >> nukeymusic wrote: >> >I'm trying to calculate the difference in seconds between two >> >> [...] >> >> >>> import datetime >> >>> date1 = datetime.datetime.strptime("Dec-13-09:47:12", >> >>> "%b-%d-%H:%M:%S") date2 = >> >>> datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") delta >> >>> = date2 - date1 delta_seconds = (delta.days * 60 * 60 * 24) + >> >>> delta.seconds + ((delta.microseconds + 50) / 100) >> >> For very big time differences you should consider to use the Decimal >> arithmetics (standard module Decimal) instead of integer arithmetics >> for the last line. >> If you are sure, that you don't use fractional seconds, you can omit >> the part with 'delta.microseconds'. >> >> Best regards, >> >> Günther > That can very much Günther, this helped me a lot further, I'm only > struggling with one more problem to finish my first python-program. > Could you > tell me why I can't write to the outputfile as I do in the code > below:? > #!/usr/bin/python > #version 16/12/2011 > #Example of testfile > #Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 > #Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 > #Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 > import datetime > f = open('testfile','r') > g = open('outputfile','w') > #get line 1 from input file: > line1=f.readline() > #get first element in line 1: > date1=line1.rsplit()[0] > #convert first element tot structured date time > struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") > for line in f: > temp=line.rsplit() > delta=datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S")- > datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") > delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + > ((delta.microseconds + 50) / 100) > temp[0]=delta_seconds > #the following line is wrong, but I don't know how to fix it: > g.write(temp) > #Close files > f.close() > g.close() The write() method only accepts strings; you have to convert the temp list to a string before passing it on. The minimal change: for line in f: temp = line.rsplit() delta = (datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S") -datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S")) delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 50) / 100)) temp[0] = str(delta_seconds) g.write(" ".join(temp) + "\n") Other observations: - you are repeating calculations in the loop that you can do (and did) outside. - use four-space indent for better readability - there's no need to use rsplit(); use split() After a few other modifications: import datetime def parse_line(line): date, rest = line.split(None, 1) date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") return date, rest with open('testfile','r') as f: with open('outputfile','w') as g: first_date, first_rest = parse_line(next(f)) for line in f: cur_date, rest = parse_line(line) delta = cur_date - first_date delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 50) / 100)) g.write("%s %s" % (delta_seconds, rest)) -- http://mail.python.org/mailman/listinfo/python-list
Re: calculate difference between two timestamps [newbie]
On 17 dec, 12:20, "Günther Dietrich" wrote: > nukeymusic wrote: > >I'm trying to calculate the difference in seconds between two > > [...] > > >>> import datetime > >>> date1 = datetime.datetime.strptime("Dec-13-09:47:12", "%b-%d-%H:%M:%S") > >>> date2 = datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") > >>> delta = date2 - date1 > >>> delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + > >>> ((delta.microseconds + 50) / 100) > > For very big time differences you should consider to use the Decimal > arithmetics (standard module Decimal) instead of integer arithmetics > for the last line. > If you are sure, that you don't use fractional seconds, you can omit > the part with 'delta.microseconds'. > > Best regards, > > Günther That can very much Günther, this helped me a lot further, I'm only struggling with one more problem to finish my first python-program. Could you tell me why I can't write to the outputfile as I do in the code below:? #!/usr/bin/python #version 16/12/2011 #Example of testfile #Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 #Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 #Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 import datetime f = open('testfile','r') g = open('outputfile','w') #get line 1 from input file: line1=f.readline() #get first element in line 1: date1=line1.rsplit()[0] #convert first element tot structured date time struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") for line in f: temp=line.rsplit() delta=datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S")- datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 50) / 100) temp[0]=delta_seconds #the following line is wrong, but I don't know how to fix it: g.write(temp) #Close files f.close() g.close() thanks in advance nukey -- http://mail.python.org/mailman/listinfo/python-list
Re: calculate difference between two timestamps [newbie]
nukeymusic wrote: >I'm trying to calculate the difference in seconds between two [...] >>> import datetime >>> date1 = datetime.datetime.strptime("Dec-13-09:47:12", "%b-%d-%H:%M:%S") >>> date2 = datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") >>> delta = date2 - date1 >>> delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + >>> ((delta.microseconds + 50) / 100) For very big time differences you should consider to use the Decimal arithmetics (standard module Decimal) instead of integer arithmetics for the last line. If you are sure, that you don't use fractional seconds, you can omit the part with 'delta.microseconds'. Best regards, Günther -- http://mail.python.org/mailman/listinfo/python-list
Re: calculate difference between two timestamps [newbie]
On Sat, Dec 17, 2011 at 02:19:44AM -0800, nukeymusic wrote: > I'm trying to calculate the difference in seconds between two > timestamps, but I'm totally stuck: > date1="Dec-13-09:47:12" > date2="Dec-13-09:47:39" > >>> diff=datetime.date(date2)-datetime.date(date1) > Traceback (most recent call last): > File "", line 1, in > TypeError: an integer is required > > struct_date1=time.strptime(date1, "%b-%d-%H:%M:%S") > struct_date2=time.strptime(date2, "%b-%d-%H:%M:%S") > >>> diff=datetime.date(struct_date2)-datetime.date(struct_date1) > Traceback (most recent call last): > File "", line 1, in > TypeError: an integer is required You're trying to compare two time.struct_time types when they need to be datetime.datetime types. This will do the conversion for you: import datetime,time date1="Dec-13-09:47:12" date2="Dec-13-09:47:39" struct_date1=datetime.datetime(*time.strptime(date1, "%b-%d-%H:%M:%S")[:6]) struct_date2=datetime.datetime(*time.strptime(date2, "%b-%d-%H:%M:%S")[:6]) print struct_date2 - struct_date1 -- http://mail.python.org/mailman/listinfo/python-list
Re: calculate difference between two timestamps [newbie]
Le 17/12/11 11:19, nukeymusic a écrit : I'm trying to calculate the difference in seconds between two timestamps, but I'm totally stuck: date1="Dec-13-09:47:12" date2="Dec-13-09:47:39" diff=datetime.date(date2)-datetime.date(date1) Traceback (most recent call last): File "", line 1, in TypeError: an integer is required struct_date1=time.strptime(date1, "%b-%d-%H:%M:%S") struct_date2=time.strptime(date2, "%b-%d-%H:%M:%S") diff=datetime.date(struct_date2)-datetime.date(struct_date1) Traceback (most recent call last): File "", line 1, in TypeError: an integer is required thanks in advance nukey That's work: import datetime begin = datetime.datetime(2011, 12, 13, 9, 47, 12) today = datetime.datetime(2011, 12, 13, 9, 47, 39) dif = str(today - begin) print dif >>> 0:00:27 Regards -- Vincent V.V. Oqapy . Qarte+7 . PaQager -- http://mail.python.org/mailman/listinfo/python-list
Re: calculate difference between two timestamps [newbie]
On 12/17/2011 05:19 AM, nukeymusic wrote: I'm trying to calculate the difference in seconds between two timestamps, but I'm totally stuck: date1="Dec-13-09:47:12" date2="Dec-13-09:47:39" diff=datetime.date(date2)-datetime.date(date1) Traceback (most recent call last): File "", line 1, in TypeError: an integer is required struct_date1=time.strptime(date1, "%b-%d-%H:%M:%S") struct_date2=time.strptime(date2, "%b-%d-%H:%M:%S") diff=datetime.date(struct_date2)-datetime.date(struct_date1) Traceback (most recent call last): File "", line 1, in TypeError: an integer is required thanks in advance nukey You should post the full code; you omitted your import line(s) That last approach was closer, try this one: import datetime struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") struct_date2=datetime.datetime.strptime(date2, "%b-%d-%H:%M:%S") diff = struct_date2 - struct_date1 print diff diff is of type datetime.timedelta -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
calculate difference between two timestamps [newbie]
I'm trying to calculate the difference in seconds between two timestamps, but I'm totally stuck: date1="Dec-13-09:47:12" date2="Dec-13-09:47:39" >>> diff=datetime.date(date2)-datetime.date(date1) Traceback (most recent call last): File "", line 1, in TypeError: an integer is required struct_date1=time.strptime(date1, "%b-%d-%H:%M:%S") struct_date2=time.strptime(date2, "%b-%d-%H:%M:%S") >>> diff=datetime.date(struct_date2)-datetime.date(struct_date1) Traceback (most recent call last): File "", line 1, in TypeError: an integer is required thanks in advance nukey -- http://mail.python.org/mailman/listinfo/python-list