📜  如何在Python创建时区感知日期时间对象

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

如何在Python创建时区感知日期时间对象

在这个例子中,我们将看到如何在Python创建一个时区感知的 DateTime 对象。

时区感知对象是Python DateTime 或包含时区信息的时间对象。一个有意识的对象代表一个特定的时刻,它是不可解释的。

检查对象是否时区感知:

我们可以轻松检查日期时间对象是否时区感知。为此,我们将使用 datetime 模块的 datetime.now()函数将当前日期和时间存储在一个新变量中。

然后我们将检查存储在 tzinfo 基类中的对象的时区信息。 tzinfo 是时区信息对象的抽象基类。

Python3
# Importing the datetime module
import datetime
 
# Storing the current date and time in
# a new variable using the datetime.now()
# function of datetime module
current_date = datetime.datetime.now()
 
# Checking the timezone information of the
# object stored in tzinfo base class
if current_date.tzinfo == None or current_date.tzinfo.\
        utcoffset(current_date) == None:
   
    # If passes the above condition then
    # the object is unaware
    print("Unaware")
else:
   
    # Else printing "Aware"
    print("Aware")


Python3
# Importing the datetime module
import datetime
 
# Storing the current date and time in
# a new variable using the datetime.now()
# function of datetime module
current_date = datetime.datetime.now()
 
# Replacing the value of the timezone in tzinfo class of
# the object using the replace() function
current_date = current_date.\
    replace(tzinfo=datetime.timezone.utc)
 
# Converting the date value into ISO 8601
# format using isoformat() method
current_date = current_date.isoformat()
 
# Printing the value of current_date
print(current_date)


Python3
# Importing the datetime module
import datetime
 
# Storing the current date and time in
# a new variable using the datetime.now()
# function of datetime module
current_date = datetime.datetime.now()
 
# Replacing the value of the timezone in tzinfo class of
# the object using the replace() function
current_date = current_date.replace(tzinfo=datetime.timezone.utc)
 
# Checking the timezone information of the
# object stored in tzinfo base class
if current_date.tzinfo == None or \
current_date.tzinfo.utcoffset(current_date) == None:
   
    # If passes the above condition then
    # the object is unaware
    print("Unaware")
else:
   
    # Else printing "Aware"
    print("Aware")
 
# Converting the date value into ISO 8601
# format using isoformat() method
current_date = current_date.isoformat()
 
# Printing the value of current_date
print(current_date)


Python3
# Importing the datetime module
import datetime
import pytz
 
# Storing the current date and time in
# a new variable using the datetime.now()
# function of datetime module and adding the timezone
# using timezone function of pytz module.
current_date = datetime.datetime.now(pytz.timezone('Africa/Abidjan'))
 
# Printing the value of current_date
print(current_date)


Python3
# Importing the datetime module
import datetime
import pytz
 
# Storing the current date and time in
# a new variable using the datetime.now()
# function of datetime module and adding the timezone
# using timezone function of pytz module.
current_date = datetime.datetime.now(pytz.timezone('Africa/Abidjan'))
 
# Checking the timezone information of the
# object stored in tzinfo base class
if current_date.tzinfo == None or current_date.\
tzinfo.utcoffset(current_date)== None:
   
    # If passes the above condition then
    # the object is unaware
    print("Unaware")
else:
    # Else printing "Aware"
    print("Aware")
     
# Printing the value of current_date
print(current_date)


输出:

Unaware

使用日期时间的时区感知对象

为此,我们将使用 datetime 模块的 datetime.now().time()函数将当前时间存储在一个新变量中。然后我们将使用 replace()函数替换对象的 tzinfo 类中的时区值。之后,使用 isoformat() 方法将日期值转换为 ISO 8601 格式。

代码:

蟒蛇3

# Importing the datetime module
import datetime
 
# Storing the current date and time in
# a new variable using the datetime.now()
# function of datetime module
current_date = datetime.datetime.now()
 
# Replacing the value of the timezone in tzinfo class of
# the object using the replace() function
current_date = current_date.\
    replace(tzinfo=datetime.timezone.utc)
 
# Converting the date value into ISO 8601
# format using isoformat() method
current_date = current_date.isoformat()
 
# Printing the value of current_date
print(current_date)

输出:

2021-08-30T09:45:43.291212+00:00

现在让我们使用本文第 1 部分中使用的方法检查对象是否具有时区感知能力。



蟒蛇3

# Importing the datetime module
import datetime
 
# Storing the current date and time in
# a new variable using the datetime.now()
# function of datetime module
current_date = datetime.datetime.now()
 
# Replacing the value of the timezone in tzinfo class of
# the object using the replace() function
current_date = current_date.replace(tzinfo=datetime.timezone.utc)
 
# Checking the timezone information of the
# object stored in tzinfo base class
if current_date.tzinfo == None or \
current_date.tzinfo.utcoffset(current_date) == None:
   
    # If passes the above condition then
    # the object is unaware
    print("Unaware")
else:
   
    # Else printing "Aware"
    print("Aware")
 
# Converting the date value into ISO 8601
# format using isoformat() method
current_date = current_date.isoformat()
 
# Printing the value of current_date
print(current_date)

输出:

Aware
2021-08-30T09:55:15.111556+00:00

使用 pytz 的时区感知对象

您还可以使用 pytz 模块来创建时区感知对象。

为此,我们将使用 datetime 模块的 datetime.now()函数将当前日期和时间存储在一个新变量中,然后我们将使用 pytz 模块的 timezone函数添加时区。

蟒蛇3

# Importing the datetime module
import datetime
import pytz
 
# Storing the current date and time in
# a new variable using the datetime.now()
# function of datetime module and adding the timezone
# using timezone function of pytz module.
current_date = datetime.datetime.now(pytz.timezone('Africa/Abidjan'))
 
# Printing the value of current_date
print(current_date)

输出:

2021-08-30 04:35:37.036990+00:00

现在让我们使用本文第 1 部分中使用的方法检查对象是否具有时区感知能力。

蟒蛇3

# Importing the datetime module
import datetime
import pytz
 
# Storing the current date and time in
# a new variable using the datetime.now()
# function of datetime module and adding the timezone
# using timezone function of pytz module.
current_date = datetime.datetime.now(pytz.timezone('Africa/Abidjan'))
 
# Checking the timezone information of the
# object stored in tzinfo base class
if current_date.tzinfo == None or current_date.\
tzinfo.utcoffset(current_date)== None:
   
    # If passes the above condition then
    # the object is unaware
    print("Unaware")
else:
    # Else printing "Aware"
    print("Aware")
     
# Printing the value of current_date
print(current_date)

输出:

Aware
2021-08-30 04:46:40.670455+00:00