Forums

how to workout the programs times?

Hi guys,

I'm working on my python script to create the button for tv programs.

I know how to create the variable to get the program title for the programs which I have to pull the data from the sqlite database.

I can also create the variable for the height with size I want, but I have no idea how to work out the program times to get the width size for the button control.

Here is the code I use:

#get the programs list
cur.execute('SELECT channel, title, start_date, stop_date FROM programs WHERE channel="channel"')
programList = list()
programs = cur.fetchall()


for row in programs:
     program = row[1].encode('ascii'), str(row[2]), str(row[3])
     program_title = row[1].encode('ascii')
     program_startdate = str(row[2])
     program_endDate = str(row[3])

     # find nearest half hour
     viewStartDate = datetime.datetime.now()
     viewStartDate -= datetime.timedelta(minutes = viewStartDate.minute % 30, seconds = viewStartDate.second)

     try:
         start_date = datetime.datetime.strptime(program_startdate, "%Y%m%d%H%M%S")
         end_date = datetime.datetime.strptime(program_endDate, "%Y%m%d%H%M%S")
     except TypeError:
         start_date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(program_startdate, "%Y%m%d%H%M%S")))
         end_date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(program_endDate, "%Y%m%d%H%M%S")))


         idx = str(program)
         notificationScheduled = program
         #convert the datetime object between start_date and end_date
         startDelta = start_date - viewStartDate
         stopDelta = end_date - viewStartDate

         program_start = self.secondsToXposition(startDelta.seconds)
         if startDelta.days < 0:
            program_start = self.epgView.left
         program_width = self.secondsToXposition(stopDelta.seconds) - program_start

         if program_start + program_width > self.epgView.right:
             program_width = self.epgView.right - program_start


         title = program_title
         program_height = 38

         programs_controls = xbmcgui.ControlButton(350, program_width * idx, 300, program_height, program_title)
         self.addControl(programs_controls)
     cur.close()

I use this to get the start date and end date format from sqlite database:

program_startdate = str(row[2])
program_endDate = str(row[3])

Here is the start date for each channel:

20140520170000
20140520170000
20140520170000
20140520170000
20140520170000
20140520170000
20140520170000

Here is the end date for each channel:

20140520173000
20140520180000
20140520183000
20140520173000
20140520180000
20140520180000

Do you know how i can work out the program time to get the width size for the button control??

If you subtract one datetime object from another, you'll get a timedelta and that has a total_seconds method that returns the number of seconds in the difference. The docs are here

cant you post an example code which is make things much easier for me to understand?

The trick is to use the datetime module to create two datetime objects, one for the start time and one for the end time. Then you can subtract the start from the end to get a timedelta object, from which you can get the length of the program in seconds, like Glenn said.

So:

>>> import datetime
>>> start = datetime.datetime.strptime("20140520170000", "%Y%m%d%H%M%S")
>>> end = datetime.datetime.strptime("20140520173000", "%Y%m%d%H%M%S")
>>> diff = end - start
>>> type(diff)
<type 'datetime.timedelta'>
>>> diff.total_seconds()
1800.0
>>> time_in_minutes = diff.total_seconds() / 60
>>> time_in_minutes
30.0
>>>