Re: Subtracting dates to get hours and minutes
On 12/15/2022 11:34 PM, MRAB wrote: On 2022-12-15 22:49, Gronicus@SGA.Ninja wrote: Yes, it works like a charm. On the tupility of it all. Special thanks for the explanation too….. (Originally asked but I found the errors. All is working) Now that the code no longer produces the errors, I see that the year and month not included in the calculation? How do I fix this? First you should read about the function you are using. In this case, it is at https://docs.python.org/3.10/library/datetime.html I found this page using an internet search for "python datetime.datetime". All Python functions, classes, and methods will normally be found in the documentation for the standard library, as this one is. You should also look at datetime.timedelta, as various people have posted here. Try to understand what is being described. Then try to make simple examples that will show if you can get what you want, or if not, what more information you need. Look through the rest of the documentation of (in this case) the datetime module and see if any of the other functionality looks like it will produce what you want. It is important to understand clearly what your input data is like, and what results you need to achieve. If you are not clear, or do not express yourself clearly, it will be hard and frustrating for others on the list to assist you. For example, you wrote "I see that the year and month not included in the calculation". I don't know what you mean. You already include the month in the tuple that you feed to datetime.datetime(). That is, in the input string you showed, "(2022, 12, 13, 5, 3, 30)", it seems that "2022" represents a year, "12" represents a month, "13" represents a day of the month, and so forth. So I don't understand how year and month not included. I see that you do think that something is not coming out right, but you need to be more precise about what you mean so that others can understand too. To succeed at programming, you need to be very precise about attending to details because the computer cannot know what you have in your mind. To succeed at getting help, you have to be precise and accurate about what you want to achieve and what is not coming out right, and describe those things simply and clearly to other people. In other words, please help us to help you. -- https://mail.python.org/mailman/listinfo/python-list
Re: Subtracting dates to get hours and minutes
On 2022-12-15 22:49, Gronicus@SGA.Ninja wrote: Yes, it works like a charm. On the tupility of it all. Special thanks for the explanation too….. (Originally asked but I found the errors. All is working) Now that the code no longer produces the errors, I see that the year and month not included in the calculation? How do I fix this? How long is a month? How many months are there until January 1? On December 25, many months will there be until January 1? And how many years are there until January 1? [snip] -- https://mail.python.org/mailman/listinfo/python-list
Re: Subtracting dates to get hours and minutes
Oops, "items = dstr[1:-2].split(',')" should have read "items = dstr[1:-1].split(',')". On 12/15/2022 1:56 PM, Thomas Passin wrote: It's hard to be sure from what you have offered, but I suspect that you are taking the string "(2022, 12, 13, 5, 3, 30)" from the file and using it as is. When you feed that in as a starred argument, the string gets treated as a sequence where each item is a character in the string. Your example contains 26 characters, which matches the error message, so that's probably what is going on. You need to convert the string into the correct integers, because is the datetime function expects to get integers, not strings. It isn't going to work with a string that looks like a tuple when it is printed. Here is one way you could do this. From the input file, extract the string. Call it dstr. Then you have to get rid of the parentheses and separate out each item so you can convert it into an integer. So: items = dstr[1:-2].split(',') # This creates a list of strings. # print(items) --> ['2022', ' 12', ' 13', ' 5', ' 3', ' 3'] # Create a tuple of integers from the string items seq = (int(n) for n in items) # or make it a list instead: seq = [int(n) for n in items] # And here is the datetime object you wanted d1 = datetime.datetime(*seq) On 12/15/2022 1:14 PM, Gronicus@SGA.Ninja wrote: So far so good , I can now use a variable in datetime.datetime but it only works if I hard-code the time/date information. Now I want to have the code read from a file but I get: TypeError: function takes at most 9 arguments (26 given) I figure that the structure in the file is incorrect. What should it be? The entry in the file is (2022, 12, 13, 5, 3, 30) but when my program tries to use it I get the error. The program is a bit more sophisticated now but here is the update with a sample of the SPECIFICATIONS.txt file: = # This program compares two Timedate values, subtracts the two and # converts the difference to seconds and hours. # # %A Monday # %a Mon # %B January # %b Jan # %d 05 day # %m month as 01 # %Y 2020 # %y 20 # %H 24 # %I 12 # %M 30 min # %S Seconds import time import datetime from time import gmtime, strftime ##define strftime as time/date right now # == def GetSpecByItem(GetThisOne): #get line by item in column 4 - 7 ItemValue = "--" with open("SPECIFICATIONS.txt" , 'r') as infile: for lineEQN in infile: # loop to find each line in the file for that dose if ((lineEQN[4:7]== GetThisOne)): ItemValue = lineEQN[30:60].strip() # Just the Data return(ItemValue) """ SPECIFICATIONS.txt IYf HRB Humalog R Date (2018, 12, 4, 10, 7, 00) ## IYf HRG Humulin R Date (2022, 12, 13, 5, 3, 30) ## """ # == Main() == print() Startt = "404" Stopp = "404" Answer = "Y" Answer = input("Run test A? (" + Answer + ")" ) if Answer == "Y" or Answer == "y" or Answer == "": print() print(" Running Test A:") # Year Mth Day Hour Min Sec Startt = 2018, 12, 4, 10, 7, 00 Stopp = 2022, 12, 12, 1, 15, 30 NowTime = 2022, 12, 14, 21, 15, 30 else: print(" Running Test B:") Startt = GetSpecByItem("HRG") Stopp = GetSpecByItem("HRB") NowTime = strftime("(%Y, %m, %d, %H, %M, %S)") print() print("55 NowTime = " + str(NowTime)) print("56 Startt = " + str(Startt)) print("57 Stopp = " + str(Stopp)) print() NowTime = datetime.datetime(*NowTime) Startt = datetime.datetime(*Startt) Stopp = datetime.datetime(*Stopp) #Start == Startt # True" #print("Startt test = " + Start) # = print() c = NowTime - Stopp minutes = c.total_seconds() / 60 minutes = c.seconds / 60 hours = 0 while (minutes > 59): minutes = minutes - 60 hours += 1 minutes = round(minutes) print ("77 Hours = <" + str(hours) + ">") print ("78 Minutes = <" + str(minutes) + ">") if hours > 7: print(" Time to inject Humulin R u500.") pause = input("Pause") # == -Original Message- From: Python-list On Behalf Of Thomas Passin Sent: Tuesday, December 13, 2022 11:20 PM To: python-list@python.org Subject: Re: Subtracting dates to get hou
RE: Subtracting dates to get hours and minutes
Yes, it works like a charm. On the tupility of it all. Special thanks for the explanation too….. (Originally asked but I found the errors. All is working) Now that the code no longer produces the errors, I see that the year and month not included in the calculation? How do I fix this? From: anthony.flury Sent: Thursday, December 15, 2022 1:47 PM To: Gronicus@SGA.Ninja Subject: RE: Subtracting dates to get hours and minutes What is likely happening is that when you read the data from the file you are not reading a tuple, you are reading a 26 charcter string. You have to convert that string into a tuple - the easiest way will be somthing like this : timet = tuple(int(x.strip()) for x in timestring[1:-1].split(',')) where timestring is the data you get from the file The [1:-1] removes the () from the data The .split(",") method creates a temporary list from the remaining string breaking the string where there are commas The x.strip() removes spaces from each item in the temporary list. Note that the * unpack operator doesn't just unpack tuples, it works on an iterable, so when you read the data from the file currently, and then use * on it, it will pass 26 separate characters to the function. -- Original Message -- From: Gronicus@SGA.Ninja <mailto:Gronicus@SGA.Ninja> To: "'Thomas Passin'" mailto:li...@tompassin.net> >; python-list@python.org <mailto:python-list@python.org> Sent: Thursday, 15 Dec, 22 At 18:14 Subject: RE: Subtracting dates to get hours and minutes So far so good , I can now use a variable in datetime.datetime but it only works if I hard-code the time/date information. Now I want to have the code read from a file but I get: TypeError: function takes at most 9 arguments (26 given) I figure that the structure in the file is incorrect. What should it be? The entry in the file is (2022, 12, 13, 5, 3, 30) but when my program tries to use it I get the error. The program is a bit more sophisticated now but here is the update with a sample of the SPECIFICATIONS.txt file: = # This program compares two Timedate values, subtracts the two and # converts the difference to seconds and hours. # # %A Monday # %a Mon # %B January # %b Jan # %d 05 day # %m month as 01 # %Y 2020 # %y 20 # %H 24 # %I 12 # %M 30 min # %S Seconds import time import datetime from time import gmtime, strftime ##define strftime as time/date right now # == def GetSpecByItem(GetThisOne): #get line by item in column 4 - 7 ItemValue = "--" with open("SPECIFICATIONS.txt" , 'r') as infile: for lineEQN in infile: # loop to find each line in the file for that dose if ((lineEQN[4:7]== GetThisOne)): ItemValue = lineEQN[30:60].strip() # Just the Data return(ItemValue) """ SPECIFICATIONS.txt IYf HRB Humalog R Date (2018, 12, 4, 10, 7, 00) ## IYf HRG Humulin R Date (2022, 12, 13, 5, 3, 30) ## """ # == Main() == print() Startt = "404" Stopp = "404" Answer = "Y" Answer = input("Run test A? (" + Answer + ")" ) if Answer == "Y" or Answer == "y" or Answer == "": print() print(" Running Test A:") # Year Mth Day Hour Min Sec Startt = 2018, 12, 4, 10, 7, 00 Stopp = 2022, 12, 12, 1, 15, 30 NowTime = 2022, 12, 14, 21, 15, 30 else: print(" Running Test B:") Startt = GetSpecByItem("HRG") Stopp = GetSpecByItem("HRB") NowTime = strftime("(%Y, %m, %d, %H, %M, %S)") print() print("55 NowTime = " + str(NowTime)) print("56 Startt = " + str(Startt)) print("57 Stopp = " + str(Stopp)) print() NowTime = datetime.datetime(*NowTime) Startt = datetime.datetime(*Startt) Stopp = datetime.datetime(*Stopp) #Start == Startt # True" #print("Startt test = " + Start) # = print() c = NowTime - Stopp minutes = c.total_seconds() / 60 minutes = c.seconds / 60 hours = 0 while (minutes > 59): minutes = minutes - 60 hours += 1 minutes = round(minutes) print ("77 Hours = <" + str(hours) + ">") print ("78 Minutes = <" + str(minutes) + ">") if hours > 7: print(" Time to inject Humulin R u500.") pause = input("Pause") # == -Original Message- From: Python-list mailto:python-list-bounces+gronicus=sga.ni...@python.org> > On Behalf Of Thomas Passin Sent: Tuesday, December 13, 2022 11:20 PM To: python-list@python.org <mailto:python-list@python.org> Subject: Re: Subtracting dates to get hours and minutes Your problem is that datetime.datetime d
Re: Subtracting dates to get hours and minutes
Not sure what you mean: x = datetime.datetime(year=2018,month=12,day=4,hour=10) y = datetime.datetime(year=2022,month=12,day=13,hour=5) print(x -y) -1470 days, 5:00:00 If you want to display years, (x-y).days /365 From: Python-list on behalf of Gronicus@SGA.Ninja Date: Thursday, December 15, 2022 at 5:02 PM To: 'anthony.flury' , python-list@python.org Subject: RE: Subtracting dates to get hours and minutes *** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. *** Yes, it works like a charm. On the tupility of it all. Special thanks for the explanation too….. Now that the code no longer produces the errors, I see that the year and month not included in the calculation? How do I fix this? From: anthony.flury Sent: Thursday, December 15, 2022 1:47 PM To: Gronicus@SGA.Ninja Subject: RE: Subtracting dates to get hours and minutes What is likely happening is that when you read the data from the file you are not reading a tuple, you are reading a 26 charcter string. You have to convert that string into a tuple - the easiest way will be somthing like this : timet = tuple(int(x.strip()) for x in timestring[1:-1].split(',')) where timestring is the data you get from the file The [1:-1] removes the () from the data The .split(",") method creates a temporary list from the remaining string breaking the string where there are commas The x.strip() removes spaces from each item in the temporary list. Note that the * unpack operator doesn't just unpack tuples, it works on an iterable, so when you read the data from the file currently, and then use * on it, it will pass 26 separate characters to the function. -- Original Message -- From: Gronicus@SGA.Ninja <mailto:Gronicus@SGA.Ninja> To: "'Thomas Passin'" mailto:li...@tompassin.net> >; python-list@python.org <mailto:python-list@python.org> Sent: Thursday, 15 Dec, 22 At 18:14 Subject: RE: Subtracting dates to get hours and minutes So far so good , I can now use a variable in datetime.datetime but it only works if I hard-code the time/date information. Now I want to have the code read from a file but I get: TypeError: function takes at most 9 arguments (26 given) I figure that the structure in the file is incorrect. What should it be? The entry in the file is (2022, 12, 13, 5, 3, 30) but when my program tries to use it I get the error. The program is a bit more sophisticated now but here is the update with a sample of the SPECIFICATIONS.txt file: = # This program compares two Timedate values, subtracts the two and # converts the difference to seconds and hours. # # %A Monday # %a Mon # %B January # %b Jan # %d 05 day # %m month as 01 # %Y 2020 # %y 20 # %H 24 # %I 12 # %M 30 min # %S Seconds import time import datetime from time import gmtime, strftime ##define strftime as time/date right now # == def GetSpecByItem(GetThisOne): #get line by item in column 4 - 7 ItemValue = "--" with open("SPECIFICATIONS.txt" , 'r') as infile: for lineEQN in infile: # loop to find each line in the file for that dose if ((lineEQN[4:7]== GetThisOne)): ItemValue = lineEQN[30:60].strip() # Just the Data return(ItemValue) """ SPECIFICATIONS.txt IYf HRB Humalog R Date (2018, 12, 4, 10, 7, 00) ## IYf HRG Humulin R Date (2022, 12, 13, 5, 3, 30) ## """ # == Main() == print() Startt = "404" Stopp = "404" Answer = "Y" Answer = input("Run test A? (" + Answer + ")" ) if Answer == "Y" or Answer == "y" or Answer == "": print() print(" Running Test A:") # Year Mth Day Hour Min Sec Startt = 2018, 12, 4, 10, 7, 00 Stopp = 2022, 12, 12, 1, 15, 30 NowTime = 2022, 12, 14, 21, 15, 30 else: print(" Running Test B:") Startt = GetSpecByItem("HRG") Stopp = GetSpecByItem("HRB") NowTime = strftime("(%Y, %m, %d, %H, %M, %S)") print() print("55 NowTime = " + str(NowTime)) print("56 Startt = " + str(Startt)) print("57 Stopp = " + str(Stopp)) print() NowTime = datetime.datetime(*NowTime) Startt = datetime.datetime(*Startt) Stopp = datetime.datetime(*Stopp) #Start == Startt # True" #print("Startt test = " + Start) # = print() c = NowTime - Stopp minutes = c.total_seconds() / 60 minutes = c.seconds / 60 hours = 0 while (minutes > 59): minutes = minutes - 60 hours += 1 minutes = round(minutes) print ("77 Hours = <" + str(hours) + ">") print ("78 Minutes = <" + str(minutes) + ">") if hours > 7: print(" Time to inject
RE: Subtracting dates to get hours and minutes
Yes, it works like a charm. On the tupility of it all. Special thanks for the explanation too….. Now that the code no longer produces the errors, I see that the year and month not included in the calculation? How do I fix this? From: anthony.flury Sent: Thursday, December 15, 2022 1:47 PM To: Gronicus@SGA.Ninja Subject: RE: Subtracting dates to get hours and minutes What is likely happening is that when you read the data from the file you are not reading a tuple, you are reading a 26 charcter string. You have to convert that string into a tuple - the easiest way will be somthing like this : timet = tuple(int(x.strip()) for x in timestring[1:-1].split(',')) where timestring is the data you get from the file The [1:-1] removes the () from the data The .split(",") method creates a temporary list from the remaining string breaking the string where there are commas The x.strip() removes spaces from each item in the temporary list. Note that the * unpack operator doesn't just unpack tuples, it works on an iterable, so when you read the data from the file currently, and then use * on it, it will pass 26 separate characters to the function. -- Original Message -- From: Gronicus@SGA.Ninja <mailto:Gronicus@SGA.Ninja> To: "'Thomas Passin'" mailto:li...@tompassin.net> >; python-list@python.org <mailto:python-list@python.org> Sent: Thursday, 15 Dec, 22 At 18:14 Subject: RE: Subtracting dates to get hours and minutes So far so good , I can now use a variable in datetime.datetime but it only works if I hard-code the time/date information. Now I want to have the code read from a file but I get: TypeError: function takes at most 9 arguments (26 given) I figure that the structure in the file is incorrect. What should it be? The entry in the file is (2022, 12, 13, 5, 3, 30) but when my program tries to use it I get the error. The program is a bit more sophisticated now but here is the update with a sample of the SPECIFICATIONS.txt file: = # This program compares two Timedate values, subtracts the two and # converts the difference to seconds and hours. # # %A Monday # %a Mon # %B January # %b Jan # %d 05 day # %m month as 01 # %Y 2020 # %y 20 # %H 24 # %I 12 # %M 30 min # %S Seconds import time import datetime from time import gmtime, strftime ##define strftime as time/date right now # == def GetSpecByItem(GetThisOne): #get line by item in column 4 - 7 ItemValue = "--" with open("SPECIFICATIONS.txt" , 'r') as infile: for lineEQN in infile: # loop to find each line in the file for that dose if ((lineEQN[4:7]== GetThisOne)): ItemValue = lineEQN[30:60].strip() # Just the Data return(ItemValue) """ SPECIFICATIONS.txt IYf HRB Humalog R Date (2018, 12, 4, 10, 7, 00) ## IYf HRG Humulin R Date (2022, 12, 13, 5, 3, 30) ## """ # == Main() == print() Startt = "404" Stopp = "404" Answer = "Y" Answer = input("Run test A? (" + Answer + ")" ) if Answer == "Y" or Answer == "y" or Answer == "": print() print(" Running Test A:") # Year Mth Day Hour Min Sec Startt = 2018, 12, 4, 10, 7, 00 Stopp = 2022, 12, 12, 1, 15, 30 NowTime = 2022, 12, 14, 21, 15, 30 else: print(" Running Test B:") Startt = GetSpecByItem("HRG") Stopp = GetSpecByItem("HRB") NowTime = strftime("(%Y, %m, %d, %H, %M, %S)") print() print("55 NowTime = " + str(NowTime)) print("56 Startt = " + str(Startt)) print("57 Stopp = " + str(Stopp)) print() NowTime = datetime.datetime(*NowTime) Startt = datetime.datetime(*Startt) Stopp = datetime.datetime(*Stopp) #Start == Startt # True" #print("Startt test = " + Start) # = print() c = NowTime - Stopp minutes = c.total_seconds() / 60 minutes = c.seconds / 60 hours = 0 while (minutes > 59): minutes = minutes - 60 hours += 1 minutes = round(minutes) print ("77 Hours = <" + str(hours) + ">") print ("78 Minutes = <" + str(minutes) + ">") if hours > 7: print(" Time to inject Humulin R u500.") pause = input("Pause") # == -Original Message- From: Python-list mailto:python-list-bounces+gronicus=sga.ni...@python.org> > On Behalf Of Thomas Passin Sent: Tuesday, December 13, 2022 11:20 PM To: python-list@python.org <mailto:python-list@python.org> Subject: Re: Subtracting dates to get hours and minutes Your problem is that datetime.datetime does not accept a tuple as an argument. It expects an integer value for
Re: Subtracting dates to get hours and minutes
It's hard to be sure from what you have offered, but I suspect that you are taking the string "(2022, 12, 13, 5, 3, 30)" from the file and using it as is. When you feed that in as a starred argument, the string gets treated as a sequence where each item is a character in the string. Your example contains 26 characters, which matches the error message, so that's probably what is going on. You need to convert the string into the correct integers, because is the datetime function expects to get integers, not strings. It isn't going to work with a string that looks like a tuple when it is printed. Here is one way you could do this. From the input file, extract the string. Call it dstr. Then you have to get rid of the parentheses and separate out each item so you can convert it into an integer. So: items = dstr[1:-2].split(',') # This creates a list of strings. # print(items) --> ['2022', ' 12', ' 13', ' 5', ' 3', ' 3'] # Create a tuple of integers from the string items seq = (int(n) for n in items) # or make it a list instead: seq = [int(n) for n in items] # And here is the datetime object you wanted d1 = datetime.datetime(*seq) On 12/15/2022 1:14 PM, Gronicus@SGA.Ninja wrote: So far so good , I can now use a variable in datetime.datetime but it only works if I hard-code the time/date information. Now I want to have the code read from a file but I get: TypeError: function takes at most 9 arguments (26 given) I figure that the structure in the file is incorrect. What should it be? The entry in the file is (2022, 12, 13, 5, 3, 30) but when my program tries to use it I get the error. The program is a bit more sophisticated now but here is the update with a sample of the SPECIFICATIONS.txt file: = # This program compares two Timedate values, subtracts the two and # converts the difference to seconds and hours. # # %A Monday# %a Mon # %B January # %b Jan # %d 05 day# %m month as 01 # %Y 2020 # %y 20 # %H 24# %I 12# %M 30 min# %S Seconds import time import datetime from time import gmtime, strftime ##define strftime as time/date right now # == def GetSpecByItem(GetThisOne): #get line by item in column 4 - 7 ItemValue = "--" with open("SPECIFICATIONS.txt" , 'r') as infile: for lineEQN in infile: # loop to find each line in the file for that dose if ((lineEQN[4:7]== GetThisOne)): ItemValue = lineEQN[30:60].strip() # Just the Data return(ItemValue) """ SPECIFICATIONS.txt IYf HRB Humalog R Date (2018, 12, 4, 10, 7, 00) ## IYf HRG Humulin R Date (2022, 12, 13, 5, 3, 30) ## """ # == Main() == print() Startt = "404" Stopp = "404" Answer = "Y" Answer = input("Run test A? (" + Answer + ")" ) if Answer == "Y" or Answer == "y" or Answer == "": print() print(" Running Test A:") # Year Mth Day Hour Min Sec Startt = 2018, 12, 4, 10, 7, 00 Stopp = 2022, 12, 12, 1, 15, 30 NowTime = 2022, 12, 14, 21, 15, 30 else: print(" Running Test B:") Startt = GetSpecByItem("HRG") Stopp = GetSpecByItem("HRB") NowTime = strftime("(%Y, %m, %d, %H, %M, %S)") print() print("55NowTime = " + str(NowTime)) print("56 Startt = " + str(Startt)) print("57 Stopp = " + str(Stopp)) print() NowTime = datetime.datetime(*NowTime) Startt = datetime.datetime(*Startt) Stopp =datetime.datetime(*Stopp) #Start == Startt # True" #print("Startt test = " + Start) # = print() c = NowTime - Stopp minutes = c.total_seconds() / 60 minutes = c.seconds / 60 hours = 0 while (minutes > 59): minutes = minutes - 60 hours += 1 minutes = round(minutes) print ("77 Hours = <" + str(hours) + ">") print ("78 Minutes = <" + str(minutes) + ">") if hours > 7: print(" Time to inject Humulin R u500.") pause = input("Pause") # == -Original Message- From: Python-list On Behalf Of Thomas Passin Sent: Tuesday, December 13, 2022 11:20 PM To: python-list@python.org Subject: Re: Subtracting dates to get hours and minutes Your problem is that datetime.datetime does not accept a tuple as an argument. It expects an integer value for the first argument, but you supplied a tuple. In Python, yo
Re: Subtracting dates to get hours and minutes
On 2022-12-15 18:14, Gronicus@SGA.Ninja wrote: So far so good , I can now use a variable in datetime.datetime but it only works if I hard-code the time/date information. Now I want to have the code read from a file but I get: TypeError: function takes at most 9 arguments (26 given) I figure that the structure in the file is incorrect. What should it be? The entry in the file is (2022, 12, 13, 5, 3, 30) but when my program tries to use it I get the error. The program is a bit more sophisticated now but here is the update with a sample of the SPECIFICATIONS.txt file: = # This program compares two Timedate values, subtracts the two and # converts the difference to seconds and hours. # # %A Monday# %a Mon # %B January # %b Jan # %d 05 day# %m month as 01 # %Y 2020 # %y 20 # %H 24# %I 12# %M 30 min# %S Seconds import time import datetime from time import gmtime, strftime ##define strftime as time/date right now # == def GetSpecByItem(GetThisOne): #get line by item in column 4 - 7 ItemValue = "--" with open("SPECIFICATIONS.txt" , 'r') as infile: for lineEQN in infile: # loop to find each line in the file for that dose if ((lineEQN[4:7]== GetThisOne)): You don't need the parentheses, and certainly 2 pairs of them! ItemValue = lineEQN[30:60].strip() # Just the Data return(ItemValue) You're returning a _string_. I suggest using 'literal_eval' from the 'ast' module to convert the string safely into a tuple. However, if the 'for' loop fails to match a line, the function will return "--", which won't be of any use later on unless you check for it specifically and, say, report an error to the user. """ SPECIFICATIONS.txt IYf HRB Humalog R Date (2018, 12, 4, 10, 7, 00) ## IYf HRG Humulin R Date (2022, 12, 13, 5, 3, 30) ## """ # == Main() == print() Startt = "404" Stopp = "404" Answer = "Y" Answer = input("Run test A? (" + Answer + ")" ) if Answer == "Y" or Answer == "y" or Answer == "": print() print(" Running Test A:") # Year Mth Day Hour Min Sec Startt = 2018, 12, 4, 10, 7, 00 Stopp = 2022, 12, 12, 1, 15, 30 NowTime = 2022, 12, 14, 21, 15, 30 'Startt' and 'Stopp' here are tuples. else: print(" Running Test B:") Startt = GetSpecByItem("HRG") Stopp = GetSpecByItem("HRB") 'Startt' and 'Stopp' here are _strings_. NowTime = strftime("(%Y, %m, %d, %H, %M, %S)") print() print("55NowTime = " + str(NowTime)) print("56 Startt = " + str(Startt)) print("57 Stopp = " + str(Stopp)) print() NowTime = datetime.datetime(*NowTime) Startt = datetime.datetime(*Startt) Stopp =datetime.datetime(*Stopp) These will work if 'Startt' and 'Stopp' are tuples, but not if they're strings. In the latter case, you're effectively passing multiple single-characters strings into 'datetime'. #Start == Startt # True" #print("Startt test = " + Start) # = print() c = NowTime - Stopp minutes = c.total_seconds() / 60 minutes = c.seconds / 60 hours = 0 while (minutes > 59): minutes = minutes - 60 hours += 1 minutes = round(minutes) print ("77 Hours = <" + str(hours) + ">") print ("78 Minutes = <" + str(minutes) + ">") if hours > 7: print(" Time to inject Humulin R u500.") pause = input("Pause") # == -Original Message- From: Python-list On Behalf Of Thomas Passin Sent: Tuesday, December 13, 2022 11:20 PM To: python-list@python.org Subject: Re: Subtracting dates to get hours and minutes Your problem is that datetime.datetime does not accept a tuple as an argument. It expects an integer value for the first argument, but you supplied a tuple. In Python, you can use a sequence (e.g., tuple or list) the way you want by prefixing it with an asterisk. This causes the sequence of items to be treated as individual arguments. So: Startt = datetime.datetime(2022, 12, 13, 5, 3, 30) st1 = (2022, 12, 13, 5, 3, 30) dts1 = datetime.datetime(*st1) # NOT datetime.datetime(st1) dts1 == Startt # True On 12/13/2022 10:43 PM, Gronicus@SGA.Ninja wrote: As is, Test A works. Comment out Test A and uncomment Test B it fails.
RE: Subtracting dates to get hours and minutes
So far so good , I can now use a variable in datetime.datetime but it only works if I hard-code the time/date information. Now I want to have the code read from a file but I get: TypeError: function takes at most 9 arguments (26 given) I figure that the structure in the file is incorrect. What should it be? The entry in the file is (2022, 12, 13, 5, 3, 30) but when my program tries to use it I get the error. The program is a bit more sophisticated now but here is the update with a sample of the SPECIFICATIONS.txt file: = # This program compares two Timedate values, subtracts the two and # converts the difference to seconds and hours. # # %A Monday# %a Mon # %B January # %b Jan # %d 05 day# %m month as 01 # %Y 2020 # %y 20 # %H 24# %I 12# %M 30 min# %S Seconds import time import datetime from time import gmtime, strftime ##define strftime as time/date right now # == def GetSpecByItem(GetThisOne): #get line by item in column 4 - 7 ItemValue = "--" with open("SPECIFICATIONS.txt" , 'r') as infile: for lineEQN in infile: # loop to find each line in the file for that dose if ((lineEQN[4:7]== GetThisOne)): ItemValue = lineEQN[30:60].strip() # Just the Data return(ItemValue) """ SPECIFICATIONS.txt IYf HRB Humalog R Date (2018, 12, 4, 10, 7, 00) ## IYf HRG Humulin R Date (2022, 12, 13, 5, 3, 30) ## """ # == Main() == print() Startt = "404" Stopp = "404" Answer = "Y" Answer = input("Run test A? (" + Answer + ")" ) if Answer == "Y" or Answer == "y" or Answer == "": print() print(" Running Test A:") # Year Mth Day Hour Min Sec Startt = 2018, 12, 4, 10, 7, 00 Stopp = 2022, 12, 12, 1, 15, 30 NowTime = 2022, 12, 14, 21, 15, 30 else: print(" Running Test B:") Startt = GetSpecByItem("HRG") Stopp = GetSpecByItem("HRB") NowTime = strftime("(%Y, %m, %d, %H, %M, %S)") print() print("55NowTime = " + str(NowTime)) print("56 Startt = " + str(Startt)) print("57 Stopp = " + str(Stopp)) print() NowTime = datetime.datetime(*NowTime) Startt = datetime.datetime(*Startt) Stopp =datetime.datetime(*Stopp) #Start == Startt # True" #print("Startt test = " + Start) # = print() c = NowTime - Stopp minutes = c.total_seconds() / 60 minutes = c.seconds / 60 hours = 0 while (minutes > 59): minutes = minutes - 60 hours += 1 minutes = round(minutes) print ("77 Hours = <" + str(hours) + ">") print ("78 Minutes = <" + str(minutes) + ">") if hours > 7: print(" Time to inject Humulin R u500.") pause = input("Pause") # == -Original Message- From: Python-list On Behalf Of Thomas Passin Sent: Tuesday, December 13, 2022 11:20 PM To: python-list@python.org Subject: Re: Subtracting dates to get hours and minutes Your problem is that datetime.datetime does not accept a tuple as an argument. It expects an integer value for the first argument, but you supplied a tuple. In Python, you can use a sequence (e.g., tuple or list) the way you want by prefixing it with an asterisk. This causes the sequence of items to be treated as individual arguments. So: Startt = datetime.datetime(2022, 12, 13, 5, 3, 30) st1 = (2022, 12, 13, 5, 3, 30) dts1 = datetime.datetime(*st1) # NOT datetime.datetime(st1) dts1 == Startt # True On 12/13/2022 10:43 PM, Gronicus@SGA.Ninja wrote: > As is, Test A works. > Comment out Test A and uncomment Test B it fails. > In Test B, I move the data into a variable resulting with the report: > "TypeError: an integer is required (got type tuple) > > How do I fix this? > > #- > > import datetime > #= > # Test A Hard coded Date/Time > Startt = datetime.datetime(2022, 12, 13, 5, 3, 30) Stopp = > datetime.datetime(2022, 12, 12, 21, 15, 30) > > # = > # Test B Date/Time data as a variable > #Startt = (2022, 12, 13, 5, 3, 30) > #Stopp = (2022, 12, 12, 21, 15, 30) > > #Startt = datetime.datetime(Startt) > #Stopp = date
Re: Subtracting dates to get hours and minutes
On 12/14/2022 12:55 AM, Gronicus@SGA.Ninja wrote: I realized it had something to do with tupilation The simple fix is to add the * into the original code. Startt = datetime.datetime(*Startt) I am not sure what "dts1 == Startt # True" does It demonstrates that the version with the "*" gives the same result as the first expression. That line is not needed by any code, it's just there to show you that the proposed expression gives the desired result. Thank you. -Original Message- From: Python-list On Behalf Of Thomas Passin Sent: Tuesday, December 13, 2022 11:20 PM To: python-list@python.org Subject: Re: Subtracting dates to get hours and minutes Your problem is that datetime.datetime does not accept a tuple as an argument. It expects an integer value for the first argument, but you supplied a tuple. In Python, you can use a sequence (e.g., tuple or list) the way you want by prefixing it with an asterisk. This causes the sequence of items to be treated as individual arguments. So: Startt = datetime.datetime(2022, 12, 13, 5, 3, 30) st1 = (2022, 12, 13, 5, 3, 30) dts1 = datetime.datetime(*st1) # NOT datetime.datetime(st1) dts1 == Startt # True On 12/13/2022 10:43 PM, Gronicus@SGA.Ninja wrote: As is, Test A works. Comment out Test A and uncomment Test B it fails. In Test B, I move the data into a variable resulting with the report: "TypeError: an integer is required (got type tuple) How do I fix this? #- import datetime #= # Test A Hard coded Date/Time Startt = datetime.datetime(2022, 12, 13, 5, 3, 30) Stopp = datetime.datetime(2022, 12, 12, 21, 15, 30) # = # Test B Date/Time data as a variable #Startt = (2022, 12, 13, 5, 3, 30) #Stopp = (2022, 12, 12, 21, 15, 30) #Startt = datetime.datetime(Startt) #Stopp = datetime.datetime(Stopp) # = c = Startt - Stopp minutes = c.total_seconds() / 60 minutes = c.seconds / 60 hours = 0 while (minutes > 59): minutes = minutes - 60 hours += 1 minutes = round(minutes) print() print (" Hours = <" + str(hours) + ">") print (" Minutes = <" + str(minutes) + ">") # -- --- -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
RE: Subtracting dates to get hours and minutes
I realized it had something to do with tupilation The simple fix is to add the * into the original code. Startt = datetime.datetime(*Startt) I am not sure what "dts1 == Startt # True" does Thank you. -Original Message- From: Python-list On Behalf Of Thomas Passin Sent: Tuesday, December 13, 2022 11:20 PM To: python-list@python.org Subject: Re: Subtracting dates to get hours and minutes Your problem is that datetime.datetime does not accept a tuple as an argument. It expects an integer value for the first argument, but you supplied a tuple. In Python, you can use a sequence (e.g., tuple or list) the way you want by prefixing it with an asterisk. This causes the sequence of items to be treated as individual arguments. So: Startt = datetime.datetime(2022, 12, 13, 5, 3, 30) st1 = (2022, 12, 13, 5, 3, 30) dts1 = datetime.datetime(*st1) # NOT datetime.datetime(st1) dts1 == Startt # True On 12/13/2022 10:43 PM, Gronicus@SGA.Ninja wrote: > As is, Test A works. > Comment out Test A and uncomment Test B it fails. > In Test B, I move the data into a variable resulting with the report: > "TypeError: an integer is required (got type tuple) > > How do I fix this? > > #- > > import datetime > #= > # Test A Hard coded Date/Time > Startt = datetime.datetime(2022, 12, 13, 5, 3, 30) Stopp = > datetime.datetime(2022, 12, 12, 21, 15, 30) > > # = > # Test B Date/Time data as a variable > #Startt = (2022, 12, 13, 5, 3, 30) > #Stopp = (2022, 12, 12, 21, 15, 30) > > #Startt = datetime.datetime(Startt) > #Stopp = datetime.datetime(Stopp) > > # = > c = Startt - Stopp > minutes = c.total_seconds() / 60 > minutes = c.seconds / 60 > hours = 0 > > while (minutes > 59): > minutes = minutes - 60 > hours += 1 > minutes = round(minutes) > print() > print (" Hours = <" + str(hours) + ">") > print (" Minutes = <" + str(minutes) + ">") > > # > -- > --- > -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Subtracting dates to get hours and minutes
Your problem is that datetime.datetime does not accept a tuple as an argument. It expects an integer value for the first argument, but you supplied a tuple. In Python, you can use a sequence (e.g., tuple or list) the way you want by prefixing it with an asterisk. This causes the sequence of items to be treated as individual arguments. So: Startt = datetime.datetime(2022, 12, 13, 5, 3, 30) st1 = (2022, 12, 13, 5, 3, 30) dts1 = datetime.datetime(*st1) # NOT datetime.datetime(st1) dts1 == Startt # True On 12/13/2022 10:43 PM, Gronicus@SGA.Ninja wrote: As is, Test A works. Comment out Test A and uncomment Test B it fails. In Test B, I move the data into a variable resulting with the report: "TypeError: an integer is required (got type tuple) How do I fix this? #- import datetime #= # Test A Hard coded Date/Time Startt = datetime.datetime(2022, 12, 13, 5, 3, 30) Stopp = datetime.datetime(2022, 12, 12, 21, 15, 30) # = # Test B Date/Time data as a variable #Startt = (2022, 12, 13, 5, 3, 30) #Stopp = (2022, 12, 12, 21, 15, 30) #Startt = datetime.datetime(Startt) #Stopp = datetime.datetime(Stopp) # = c = Startt - Stopp minutes = c.total_seconds() / 60 minutes = c.seconds / 60 hours = 0 while (minutes > 59): minutes = minutes - 60 hours += 1 minutes = round(minutes) print() print (" Hours = <" + str(hours) + ">") print (" Minutes = <" + str(minutes) + ">") # - -- https://mail.python.org/mailman/listinfo/python-list
RE: Subtracting dates to get hours and minutes
As is, Test A works. Comment out Test A and uncomment Test B it fails. In Test B, I move the data into a variable resulting with the report: "TypeError: an integer is required (got type tuple) How do I fix this? #- import datetime #= # Test A Hard coded Date/Time Startt = datetime.datetime(2022, 12, 13, 5, 3, 30) Stopp = datetime.datetime(2022, 12, 12, 21, 15, 30) # = # Test B Date/Time data as a variable #Startt = (2022, 12, 13, 5, 3, 30) #Stopp = (2022, 12, 12, 21, 15, 30) #Startt = datetime.datetime(Startt) #Stopp = datetime.datetime(Stopp) # = c = Startt - Stopp minutes = c.total_seconds() / 60 minutes = c.seconds / 60 hours = 0 while (minutes > 59): minutes = minutes - 60 hours += 1 minutes = round(minutes) print() print (" Hours = <" + str(hours) + ">") print (" Minutes = <" + str(minutes) + ">") # - -- https://mail.python.org/mailman/listinfo/python-list
Re: Subtracting dates to get hours and minutes
The difference between two datetime objects is a timedelta object. https://docs.python.org/3/library/datetime.html#timedelta-objects . It has a total_seconds() method. This is a simple task, unless one of the datetimes has a time zone specified and the other doesn’t. From: Python-list on behalf of Marc Lucke Date: Monday, December 12, 2022 at 11:37 AM To: python-list@python.org Subject: Re: Subtracting dates to get hours and minutes *** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. *** my approach would be to convert your two date/times to seconds from epoch - e.g. https://urldefense.com/v3/__https://www.geeksforgeeks.org/convert-python-datetime-to-epoch/__;!!Cn_UX_p3!hBXQeMkZ3QYS6BI0yTHsADseWTXcDXOhKFkg35NnRMicvYQvwLo9c_ihSaTrG60LywsKQm6UNd7mAAYr$<https://urldefense.com/v3/__https:/www.geeksforgeeks.org/convert-python-datetime-to-epoch/__;!!Cn_UX_p3!hBXQeMkZ3QYS6BI0yTHsADseWTXcDXOhKFkg35NnRMicvYQvwLo9c_ihSaTrG60LywsKQm6UNd7mAAYr$> - then subtract the number, divide the resultant by 3600 (hours) & get the modulus for minutes. There's probably a standard function - it should be /very/ easy to do. - Marc On 12/12/2022 5:01 pm, Steve GS wrote: > How do I subtract two time/dates and calculate the hours and minutes > between? > Steve > -- https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!hBXQeMkZ3QYS6BI0yTHsADseWTXcDXOhKFkg35NnRMicvYQvwLo9c_ihSaTrG60LywsKQm6UNSbh5q0S$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!hBXQeMkZ3QYS6BI0yTHsADseWTXcDXOhKFkg35NnRMicvYQvwLo9c_ihSaTrG60LywsKQm6UNSbh5q0S$> -- https://mail.python.org/mailman/listinfo/python-list
Re: Subtracting dates to get hours and minutes
my approach would be to convert your two date/times to seconds from epoch - e.g. https://www.geeksforgeeks.org/convert-python-datetime-to-epoch/ - then subtract the number, divide the resultant by 3600 (hours) & get the modulus for minutes. There's probably a standard function - it should be /very/ easy to do. - Marc On 12/12/2022 5:01 pm, Steve GS wrote: How do I subtract two time/dates and calculate the hours and minutes between? Steve -- https://mail.python.org/mailman/listinfo/python-list
RE: Subtracting dates to get hours and minutes
I have seen vast conversations on this topic but if everything is in the same time-zone and daylight saving switchovers are not involved it is relatively straightforward.Check the timedelta docs. Or convert datetimes to ordinals and subtract then convert the result to whatever units please you.M--(Unsigned mail from my phone) Original message From: Steve GS Date: 12/12/22 17:34 (GMT+10:00) To: python-list@python.org Subject: Subtracting dates to get hours and minutes How do I subtract two time/dates and calculate the hours and minutesbetween?Steve-- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Subtracting dates to get hours and minutes
How do I subtract two time/dates and calculate the hours and minutes between? Steve -- https://mail.python.org/mailman/listinfo/python-list