python之当前标准时间显示

Posted 知_行

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python之当前标准时间显示相关的知识,希望对你有一定的参考价值。

借编程金典的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之当前标准时间显示的主要内容,如果未能解决你的问题,请参考以下文章

python之模块和包

如何获取当前显示的片段?

js常用代码片段(更新中)

python 标准库模块之 os 模块

python调试之pdb调试工具

Sphinx、reStructuredText 显示/隐藏代码片段