How do I subtract two time/dates and calculate the hours and minutes
between?
Steve
How do I subtract two time/dates and calculate the hours and minutes
between?
Steve
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) + ">")
# -----------------------------------------------------------------------------
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) + ">")
#
---------------------------------------------------------------------- -------
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 <python-list-bounces+gronicus=sga.ninja@python.org> 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
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) + ">")
#
---------------------------------------------------------------------- -------
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 <python-list-bounces+gronicus=sga.ninja@python.org> 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
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 <python-list-bounces+gronicus=sga.ninja@python.org> 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
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 <python-list-bounces+gronicus=sga.ninja@python.org> 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
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?
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?
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (0 / 16) |
Uptime: | 168:19:33 |
Calls: | 10,385 |
Calls today: | 2 |
Files: | 14,057 |
Messages: | 6,416,545 |