1

I'm new to python and I'm trying to get the actual minutes passed every day since 7:00. I am using mktime to get now_date1 and now_date2 in seconds, and then the plan it's to subtract and divide by 60 to get the minutes. But I get the following error:

AttributeError: 'str' object has no attribute 'timetuple'

It's this the correct approach?

Here it's the code

import time
import pytz
from datetime import datetime
from time import mktime as mktime

now_date = datetime.now(pytz.timezone('Europe/Bucharest'))
now_date1 = now_date.strftime('%H:%M:%S')
now_date2 = now_date.strftime('7:00:00')
# Convert to Unix timestamp
d1_ts = time.mktime(now_date1.timetuple())
GLR
  • 1,070
  • 1
  • 11
  • 29

2 Answers2

1

strftime returns a string. Not what you want.

You were pretty close, but there's no need to put time in the mix. Just modify your code like this and use time delta from datetime (inspired by How to calculate the time interval between two time strings):

import pytz
from datetime import datetime

now_date = datetime.now(pytz.timezone('Europe/Bucharest'))

from datetime import datetime
FMT = '%H:%M:%S'
now_date1 = now_date.strftime(FMT)
now_date2 = now_date.strftime('7:00:00')

tdelta = datetime.strptime(now_date1, FMT) - datetime.strptime(now_date2, FMT)

print(tdelta)

I get: 6:40:42 which seems to match since it's 12:42 here.

To get the result in minutes just do:

tdelta.seconds//60

(note that the dates have only correct hour/time/seconds, the year, month, etc.. are 1900 ... since they're not used)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
0

I think something like this might work:

import time
import datetime
from time import mktime as mktime

#current time
now_date = datetime.datetime.now()

#time at 7am
today = datetime.date.today()
now_date2 = datetime.datetime(today.year, today.month, today.day, 7, 0, 0, 0)

#difference in minutes
(now_date - now_date2).days * 24 * 60
ShreyasG
  • 766
  • 4
  • 11