借编程金典的python标准时间类稍稍修改,显示当前标准时间
#simple definition of class Time. #coding:utf-8 import time from time import ctime class Time1: def __init__(self): #‘‘‘Intitializes hour,minute and second to zero‘‘‘ self.hour = int(((time.ctime()).split(‘ ‘,5)[4]).split(‘:‘, 3)[0]) #0-23 self.minute = int(((time.ctime()).split(‘ ‘,5)[4]).split(‘:‘, 3)[1]) # 0-59 self.second = int(((time.ctime()).split(‘ ‘,5)[4]).split(‘:‘, 3)[2])# 0-59 def printMilitary(self): #Prints object of class Time in military format print(‘%.2d:%.2d:%.2d‘ % (self.hour, self.minute, self.second)) def printStandard(self): #print object of class time in standard fromat standardTime = ‘‘ if self.hour == 0 or self.hour == 12: standardTime += ‘12:‘ else: standardTime += ‘%d:‘ % (self.hour % 12) standardTime += "%.2d:%.2d" % (self.minute, self.second) if self.hour < 12: standardTime += " AM" else: standardTime += " PM" print(standardTime) t = Time1() t.printStandard()
显示如下
5:18:25 PM
END!
时间: 2024-11-05 11:44:22