码迷,mamicode.com
首页 > 编程语言 > 详细

python之当前标准时间显示

时间:2016-07-05 18:48:59      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

借编程金典的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!

python之当前标准时间显示

标签:

原文地址:http://www.cnblogs.com/changbo/p/5644410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!