Hey guys,

I need some help with my code because I have a bit of trouble with changing the 12 hours time when I'm trying to change the strings in the object.

When I try this:

def GoLeft(self):

   if CurrentRow == 375:
       if self.channel_page >= 0:
          # Set the date and time row
          getTime1 = self.getControl(344)
          getTime2 = self.getControl(345)
          getTime3 = self.getControl(346)
          getTime3.setLabel(getTime2.getLabel())
          getTime2.setLabel(getTime1.getLabel())
          getTime1 = self.getControl(344).getLabel()
          hour = time.strftime("%I").lstrip('0')

          #Find the time for each clock to change the text
          if getTime1 == '12:00AM':
             self.getControl(344).setLabel('11:30AM')
          elif getTime1 == '12:30AM':
             self.getControl(344).setLabel('12:00AM')
          elif getTime1 == '1:00AM':
             self.getControl(344).setLabel('12:30AM')
          elif getTime1 == '1:30AM':
             self.getControl(344).setLabel('1:00AM')
          elif getTime1 == '2:00AM':
             self.getControl(344).setLabel('1:30AM')
          elif getTime1 == '2:30AM':
             self.getControl(344).setLabel('2:00AM')
          elif getTime1 == '3:00AM':
             self.getControl(344).setLabel('2:30AM')
          elif getTime1 == '3:30AM':
             self.getControl(344).setLabel('3:00AM')
          elif getTime1 == '4:00AM':
             self.getControl(344).setLabel('3:30AM')
          elif getTime1 == '4:30AM':
             self.getControl(344).setLabel('4:00AM')
          elif getTime1 == '5:00AM':
             self.getControl(344).setLabel('4:30AM')
          elif getTime1 == '5:30AM':
             self.getControl(344).setLabel('5:00AM')
          elif getTime1 == '6:00AM':
             self.getControl(344).setLabel('5:30AM')
          elif getTime1 == '6:30AM':
             self.getControl(344).setLabel('6:00AM')
          elif getTime1 == '7:00AM':
             self.getControl(344).setLabel('6:30AM')
          elif getTime1 == '7:30AM':
             self.getControl(344).setLabel('7:00AM')
          elif getTime1 == '8:00AM':
             self.getControl(344).setLabel('7:30AM')
          elif getTime1 == '8:30AM':
             self.getControl(344).setLabel('8:00AM')
          elif getTime1 == '9:00AM':
             self.getControl(344).setLabel('8:30AM')
          elif getTime1 == '9:30AM':
             self.getControl(344).setLabel('9:00AM')
          elif getTime1 == '10:00AM':
             self.getControl(344).setLabel('9:30AM')
          elif getTime1 == '10:30AM':
             self.getControl(344).setLabel('10:00AM')
          elif getTime1 == '11:00AM':
             self.getControl(344).setLabel('10:30AM')
          elif getTime1 == '11:30AM':
             self.getControl(344).setLabel('11:00AM')
          elif getTime1 == '12:00PM':
             self.getControl(344).setLabel('11:30AM')
          elif getTime1 == '12:30PM':
             self.getControl(344).setLabel('12:00PM')
          elif getTime1 == '1:00PM':
             self.getControl(344).setLabel('12:30AM')
          elif getTime1 == '1:30PM':
             self.getControl(344).setLabel('1:00PM')
          elif getTime1 == '2:00PM':
             self.getControl(344).setLabel('1:30PM')
          elif getTime1 == '2:30PM':
             self.getControl(344).setLabel('2:00PM')
          elif getTime1 == '3:00PM':
             self.getControl(344).setLabel('2:30PM')
          elif getTime1 == '3:30PM':
             self.getControl(344).setLabel('3:00PM')
          elif getTime1 == '4:00PM':
             self.getControl(344).setLabel('3:30PM')
          elif getTime1 == '4:30PM':
             self.getControl(344).setLabel('4:00PM')
          elif getTime1 == '5:00PM':
             self.getControl(344).setLabel('4:30PM')
          elif getTime1 == '5:30PM':
             self.getControl(344).setLabel('5:00PM')
          elif getTime1 == '6:00PM':
             self.getControl(344).setLabel('5:30PM')
          elif getTime1 == '6:30PM':
             self.getControl(344).setLabel('6:00PM')
          elif getTime1 == '7:00PM':
             self.getControl(344).setLabel('6:30PM')
          elif getTime1 == '7:30PM':
             self.getControl(344).setLabel('7:00PM')
          elif getTime1 == '8:00PM':
             self.getControl(344).setLabel('7:30PM')
          elif getTime1 == '8:30PM':
             self.getControl(344).setLabel('8:00PM')
          elif getTime1 == '9:00PM':
             self.getControl(344).setLabel('8:30PM')
          elif getTime1 == '9:30PM':
             self.getControl(344).setLabel('9:00PM')
          elif getTime1 == '10:00PM':
             self.getControl(344).setLabel('9:30PM')
          elif getTime1 == '10:30PM':
             self.getControl(344).setLabel('10:00PM')
          elif getTime1 == '11:00PM':
             self.getControl(344).setLabel('10:30PM')
          elif getTime1 == '11:30PM':
              self.getControl(344).setLabel('11:00PM')

It will let me to change the time to 30 mins back, e.g: from 1:30AM to 1:00AM. When I have the time 1:00AM, it will not find the strings of 1:00AM to change it to 12:30PM. The reason for this is because I have two different type of AM and PM.

Can you please show me an example of how can I find the time between AM and PM before changing the strings in the object to the time that I actually want?

Recommended Answers

All 14 Replies

You can use the datetime module for these calculations. According to this module however, 12:00PM is at noon and 12:00AM is at midnight:

>>> import datetime as dt
>>> fm = '%I:%M%p'
>>> def change(s):
...     t = dt.datetime.strptime(s, fm) + dt.timedelta(days=36524)
...     return (t - dt.timedelta(minutes=30)).strftime(fm)
... 
>>> change('12:00AM')
'11:30PM'
>>> change('12:30AM')
'12:00AM'
>>> change('12:30PM')
'12:00PM'
>>> change('12:00PM')
'11:30AM'
>>> change('01:00PM')
'12:30PM'
>>> change('01:00AM')
'12:30AM'

@Gribouillis Thank you very much for this, I want to use the code in GoLeft without the return.

Something like this?

import datetime
getTime1 = getTime2.getLabel()
t = datetime.datetime.strptime(getTime1, "%I:%M%p")
time_half_hour_before = (t.datetime(days=-1, minutes=30)).strftime("%I:%M%p")
print time_half_hour_before

When I try it, it give me the error: AttributeError: 'datetime.datetime' object has no attribute 'datetime'

The error is highlight on this line:

time_half_hour_before = (t.datetime(days=-1, minutes=30)).strftime("%I:%M%p")

Do you know why I get an error and do you know how to fix it?

You must add something because a datetime without a date gives you a datetime on the first of january 1900. There will be an error if you try to strftime a datetime before 1900. That's why I added a century.
Use

import datetime
getTime1 = getTime2.getLabel()
t = datetime.datetime.strptime(getTime1, "%I:%M%p") + datetime.timedelta(days=36524)
time_half_hour_before = (t - datetime.timedelta(minutes=30)).strftime("%I:%M%p")
print time_half_hour_before

thanks but I still the same error, any idea?

My code works

>>> import datetime
>>> getTime1 = "01:00PM"
>>> t = datetime.datetime.strptime(getTime1, "%I:%M%p") + datetime.timedelta(days=36524)
>>> time_half_hour_before = (t - datetime.timedelta(minutes=30)).strftime("%I:%M%p")
>>> print time_half_hour_before
12:30PM

you must be running another snippet

Well I am using the old version of python which it is 2.4. How I can get it to work on a 2.4 version?

what does the exception traceback print ?

I dont understand what you mean. I cant print any of the code you have posted because i'm currently using python 2.4v.

Can you import datetime ?

Why don't you upgrade to 2.7 ?

I can't because I'm using xbmc media application which it support python version 2.6.6. Yes I have import datetime already?

All right, then create a file foo.py with the following code

import datetime
getTime1 = "01:00PM"
t = datetime.datetime.strptime(getTime1, "%I:%M%p") + datetime.timedelta(days=36524)
time_half_hour_before = (t - datetime.timedelta(minutes=30)).strftime("%I:%M%p")
print time_half_hour_before

Then run it and post here everything that python says.

Edit: python 2.6 is already much better than python 2.4, so if you can upgrade, upgrade.

Thanks, how i can call the file foo.py when i'm using the goleft function?

how i can call the file foo.py when i'm using the goleft function

It's not the question. I still don't understand why you say that the code doesn't work. Can your python interpreter run a file foo.py containing the previous code ?

If it doesn't work, the only way to find the error is to see the whole error message.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.