更改字符串的次数

时间:2017-09-02 01:07:59

标签: python python-2.7

我正在处理我的代码,因为我正在检查getTime1以查看字符串是否有效。如果字符串有效,我希望在getTime1字符串有效时,每次将字符串更改为向后移动30分钟。

示例:getTime1字符串显示12:00AM,因此我想将其更改为向后30分钟以使其显示11:30PM。如果getTime1字符串显示12:30PM,我想将其更改为向后30分钟,以使其显示12:00PM

以下是代码:

getTime1 = self.getControl(344).getLabel()

if day_date >= 0 and day_date <= 6:
    if getTime1 == '12:00AM':
       self.getControl(344).setLabel('11:30PM')
    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:30PM')
    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')

但问题是getTime1字符串只会显示12小时的时间。

我想知道是否有办法可以减少代码,使其与6-7行而不是97行相似?

2 个答案:

答案 0 :(得分:1)

这个怎么样?

if getTime1 == '12:00AM':
    self.getControl(344).setLabel('11:30PM')
elif getTime1 == '12:00PM':
    self.getControl(344).setLabel('11:30AM')
else:
    ind = getTime1.find(':')
    if getTime1[ind+1:ind+3]=='30':
        getTime1 = getTime1[:ind]+':00'+getTime1[-2:]
        self.getControl(344).setLabel(getTime1)  
    else: 
        getTime1 = str(int(getTime1[:ind])-1)+':30'+getTime1[-2:]
        self.getControl(344).setLabel(getTime1)  

除了两个特殊情况,该程序找到:,检查是否存在30,如果是,则保持当前小时并使用AM创建一个新字符串/ PM标签和小时完好无损,但00代替30。如果结尾为00,则会将小时部分转换为int,减去1,将其转回字符串,在分钟部分添加30并再次保留AM } / PM部分。

答案 1 :(得分:0)

是的,有更简单的方法。您可能对内置time module感兴趣。它允许您解析字符串并对它们执行操作。你也可以这样做:

time = time.split(':')
hour = int(time[0])
minute = int(time[1][:2])

然后你可以弄清楚如何减去30分钟。要进行验证,您可以使用regex