時間帯の判断を書く

1418 ワード

要求:python 3.xモジュール:datetimeテストプラットフォーム:ArchLinux
 
#!/usr/bin/env python3
import time
def currenttime():
    """
     :0-5
     :5-8
     :8-11
     :11-13
     :13-16
     :16-19
     :19-24
    """
    # H:M:S
    time_table = [('0'," "),
            ('5'," "),
            ('8'," "),
            ('11'," "),
            ('13'," "),
            ('16'," "),
            ('19'," ")
            ]
    date = time.asctime().split(' ')[3].split(':')
    #print('date: ',date)
    date_mark = ''
    hour = ''
    for i in time_table:
        if int(date[0]) > int(i[0]):
            if time_table.index(i) == 6:
                date_mark = time_table[-1][1]
                break
            continue
        else:
            if int(date[0])==int(i[0]):
                date_mark = i[1]
                break
            date_mark = time_table[time_table.index(i)-1][1]
            break
    if int(date[0]) < 12:
        hour = str(int(date[0]))
    else:
        hour = str(int(date[0])-12)

    str_time = ' %s %s  %s '%(date_mark,hour,date[1])#,date[2])

    print(str_time)

if __name__ == "__main__":
    currenttime()

印刷結果:
$ python3  currenttime.py
    10  50