📜  Python DateTime - 时间类

📅  最后修改于: 2022-05-13 01:55:08.273000             🧑  作者: Mango

Python DateTime - 时间类

时间类表示独立于任何特定日期的一天中的本地时间。此类可以具有表示给定时间的时区的 tzinfo 对象。如果 tzinfo 为 None 则时间对象是天真的对象,否则它是有意识的对象。

句法:

所有参数都是可选的。 tzinfo 可以是 None 否则所有属性必须是以下范围内的整数 –

  • 0 <= 小时 < 24
  • 0 <= 分钟 < 60
  • 0 <= 秒 < 60
  • 0 <= 微秒 < 1000000
  • 折叠 [0, 1]

例子:



Python3
# Python program to
# demonstrate time class
 
from datetime import time
 
# calling the constructor
my_time = time(12, 14, 36)
 
print("Entered time", my_time)
 
# calling constructor with 1
# argument
my_time = time(minute = 12)
print("\nTime with one argument", my_time)
 
# Calling constructor with
# 0 argument
my_time = time()
print("\nTime without argument", my_time)
 
# Uncommenting time(hour = 26)
# will rase an ValueError as
# it is out of range
 
# uncommenting time(hour ='23')
# will raise TypeError as
# string is passed instead of int


Python3
from datetime import time
 
# Getting min time
mintime = time.min
print("Min Time supported", mintime)
 
# Getting max time
maxtime = time.max
print("Max Time supported", maxtime)


Python3
from datetime import time
 
# Creating Time object
Time = time(12,24,36,1212)
 
# Accessing Attributes
print("Hour:", Time.hour)
print("Minutes:", Time.minute)
print("Seconds:", Time.second)
print("Microseconds:", Time.microsecond)


Python3
from datetime import time
 
# Creating Time object
Time = time(12,24,36,1212)
 
# Converting Time object to string
Str = Time.isoformat()
print("String Representation:", Str)
print(type(Str))
 
Time = "12:24:36.001212"
 
# Converting string to Time object
Time = time.fromisoformat(Str)
print("\nTime from String", Time)
print(type(Time))


Python3
from datetime import time
 
# Creating Time object
Time = time(12,24,36,1212)
print("Original time:", Time)
 
# Replacing hour
Time = Time.replace(hour = 13, second = 12)
print("New Time:", Time)
 
# Formatting Time
Ftime = Time.strftime("%I:%M %p")
print("Formatted time", Ftime)


输出
Entered time 12:14:36

Time with one argument 00:12:00

Time without argument 00:00:00

类属性

让我们看看这个类提供的属性——

Attribute NameDescription
minMinimum possible representation of time
maxMaximum possible representation of time
resolutionThe minimum possible difference between time objects
hourThe range of hour must be between 0 and 24 (not including 24)
minuteThe range of minute must be between 0 and 60 (not including 60)
secondThe range of second must be between 0 and 60 (not including 60)
microsecondThe range of microsecond must be between 0 and 1000000 (not including 1000000)
tzinfoThe object containing timezone information
foldRepresents if the fold has occurred in the time or not

示例 1:获取最小和最大可表示时间

蟒蛇3

from datetime import time
 
# Getting min time
mintime = time.min
print("Min Time supported", mintime)
 
# Getting max time
maxtime = time.max
print("Max Time supported", maxtime)
输出
Min Time supported 00:00:00
Max Time supported 23:59:59.999999

示例 2:从时间类访问小时、分钟、秒和微秒属性

蟒蛇3

from datetime import time
 
# Creating Time object
Time = time(12,24,36,1212)
 
# Accessing Attributes
print("Hour:", Time.hour)
print("Minutes:", Time.minute)
print("Seconds:", Time.second)
print("Microseconds:", Time.microsecond)
输出
Hour: 12
Minutes: 24
Seconds: 36
Microseconds: 1212

类函数

时间类提供了各种函数,例如我们可以从字符串获取时间或将时间转换为字符串,根据我们的需要格式化时间等。让我们看看时间类提供的所有函数的列表。

时间类函数列表

Function NameDescription
dst()Returns tzinfo.dst() is tzinfo is not None
fromisoformat()Returns a time object from the string representation of the time
isoformat()Returns the string representation of time from the time object
replace()Changes the value of the time object with the given parameter
strftime()Returns a string representation of the time with the given format
tzname()Returns tzinfo.tzname() is tzinfo is not None
utcoffset()Returns tzinfo.utcffsets() is tzinfo is not None

让我们看一下上述函数的某些示例



示例 1:将时间对象转换为字符串,反之亦然

蟒蛇3

from datetime import time
 
# Creating Time object
Time = time(12,24,36,1212)
 
# Converting Time object to string
Str = Time.isoformat()
print("String Representation:", Str)
print(type(Str))
 
Time = "12:24:36.001212"
 
# Converting string to Time object
Time = time.fromisoformat(Str)
print("\nTime from String", Time)
print(type(Time))

输出

String Representation: 12:24:36.001212


Time from String 12:24:36.001212

示例 2:更改已创建时间对象的值并格式化时间

蟒蛇3

from datetime import time
 
# Creating Time object
Time = time(12,24,36,1212)
print("Original time:", Time)
 
# Replacing hour
Time = Time.replace(hour = 13, second = 12)
print("New Time:", Time)
 
# Formatting Time
Ftime = Time.strftime("%I:%M %p")
print("Formatted time", Ftime)
输出
Original time: 12:24:36.001212
New Time: 13:24:12.001212
Formatted time 01:24 PM

注意:有关Python日期时间的更多信息,请参阅Python日期时间教程