Python教程-Python中的Epoch时间到日期时间的转换
在以下教程中,我们将使用一些示例来了解如何在Python编程语言中将Epoch时间转换为日期时间。我们将使用Python中的Epoch时间来分别转换为日期和时间。我们还将涵盖以下主题:
1.将日期时间转换为Epoch时间
2.将Epoch转换为日期时间字符串
3.将Epoch转换为日期时间毫秒
4.将Epoch转换为日期时间时区
5.将Unix Epoch转换为日期时间
6.将日期时间转换为毫秒级Epoch时间
7.将Epoch时间戳转换为日期时间
8.Python Epoch日期时间年份超出范围
但在开始之前,让我们简要了解Python中的Epoch时间是什么。
Python中的Epoch时间是什么?
Epoch时间,也称为POSIX时间、Unix时间和Unix时间戳,表示自1970年1月1日以来经过的秒数,不包括闰秒。Unix时间包含十个数字,可以一次性表示所有时区。
使用Python将Epoch时间转换为日期时间
我们可以使用Python的datetime.fromtimestamp()函数来将Epoch时间转换为日期时间。让我们考虑以下示例,演示了将Python Epoch转换为日期时间的过程。
示例:
# importing the datetime package
import datetime
# given epoch time
epoch_time = 40246871
# using the datetime.fromtimestamp() function
date_time = datetime.datetime.fromtimestamp( epoch_time )
# printing the value
print("Given epoch time:", epoch_time)
print("Converted Datetime:", date_time )
输出:
Given epoch time: 40246871
Converted Datetime: 1971-04-12 01:11:11
解释:
在上面的示例中,我们已经导入了datetime库。然后,我们定义了一个变量,用于以秒为单位存储Epoch时间的值。然后,我们使用datetime.fromtimestamp()函数将Epoch时间转换为日期时间,并稍后为用户打印输出。结果,提供的变量已成功转换为日期时间。
使用Python将日期时间转换为Epoch时间
为了将日期时间转换为Epoch时间,我们将使用timestamp()函数。首先,让我们考虑以下示例,说明了如何将日期时间转换为Epoch时间。
示例:
# importing the datetime package
import datetime
# using the timestamp() function to convert datetime into epoch time
epoch_time = datetime.datetime(2021, 6, 9, 2, 0).timestamp()
# printing the values
print("Converted epoch time:", epoch_time)
输出:
Converted epoch time: 1623184200.0
解释:
在上面的示例中,我们再次导入了datetime包。然后,我们定义了一个变量,用于存储通过timestamp()函数计算的日期时间的Epoch时间值,并将其打印出来。结果,给定的日期时间已成功转换为Epoch时间。
使用Python将Epoch时间转换为日期时间字符串
为了将Epoch时间转换为日期时间字符串,我们需要将Epoch时间转换为日期时间,然后将生成的日期时间变量传递给strftime()函数,以成功将其转换为日期时间字符串。
示例:
# importing the datetime package
import datetime
# given epoch time
epoch_time = 700024
# using datetime.fromtimestamp() function to convert epoch time into datetime object
mytimestamp = datetime.datetime.fromtimestamp( epoch_time )
# using strftime() function to convert
datetime_str = mytimestamp.strftime( "%Y - %m - %d %H : %M : %S")
# printing the values
print("Given epoch time:", epoch_time)
print("Converted datetime string:", datetime_str)
输出:
Given epoch time: 700024
Converted datetime string: 1970 - 01 - 09 07 : 57 : 04
解释:
在上面的示例中,我们再次导入了datetime库。然后,我们使用datetime.fromtimestamp()函数将给定的Epoch时间转换为日期时间,然后将其传递给strftime()函数,以将日期时间对象转换为日期时间字符串。strftime()函数的参数是我们要自定义字符串的格式代码。
- %Y 表示年份
- %m 表示月份
- %d 表示日期
- %H 表示小时
- %M 表示分钟
- %S 表示秒
使用Python将Epoch时间转换为日期时间毫秒
我们可以使用与将Epoch时间转换为日期时间相同的方法来获取带毫秒的日期时间。让我们考虑以下示例,演示了相同的过程。
示例:
# importing the datetime package
import datetime
# given epoch time
epoch_time = 402471.238201
# using the datetime.fromtimestamp() function
date_time = datetime.datetime.fromtimestamp( epoch_time )
# printing the value
print("Given epoch time:", epoch_time)
print("Converted Datetime:", date_time )
输出:
Given epoch time: 402471.238201
Converted Datetime: 1970-01-05 21:17:51.238201
解释:
在上面的示例中,我们导入了datetime包并定义了一个变量,用于存储带有小数位的Epoch时间值。然后,我们使用datetime.fromtimestamp()函数将此小数值转换为日期时间毫秒格式。
使用Python将Epoch时间转换为日期时间时区
我们可以使用pytz包的timezone()函数将Epoch时间转换为日期时间时区。让我们考虑以下示例,演示了相同的转换过程。
示例:
# importing the required package(s) and module(s)
from datetime import datetime
import pytz
# using the timezone() function
my_timezone = pytz.timezone('CST6CDT')
# using the localize() function
date_timezone = my_timezone.localize( datetime( 2021, 6, 10, 9, 34, 32 ), is_dst = None )
# printing the values
print( "DateTime Time zone:", date_timezone )
输出:
DateTime Time zone: 2021-06-10 09:34:32-05:00
解释:
在上面的示例中,我们从datetime包中导入了datetime模块,还导入了pytz库。然后,我们使用pytz库的timezone()函数,指定参数为"CST6CDT"。此函数返回指定时区的当前日期。然后,我们使用localize()函数将结果本地化。
使用Python将Unix Epoch时间转换为日期时间
将Unix Epoch时间转换为日期时间的过程与之前的类似。此过程涉及使用datetime.fromtimestamp()函数将Unix Epoch时间转换为日期时间对象,并使用strftime()函数将对象转换为合适的日期时间格式。
让我们考虑以下示例,演示了相同的过程。
示例:
# importing the datetime library
import datetime
# given epoch time
epoch_time = 252384207
# using the datetime.fromtimestamp() function
datetime_obj = datetime.datetime.fromtimestamp( epoch_time )
# using the strftime() function
datetime_str = datetime_obj.strftime( "%Y - %m - %d %H : %M : %S" )
# printing the values
print( "Unix epoch time:", epoch_time )
print( "DateTime:", datetime_str )
输出:
Unix epoch time: 252384207
DateTime: 1977 - 12 - 31 08 : 13 : 27
解释:
在上面的示例中,我们再次导入了datetime库,并定义了一个变量,其中包含Unix Epoch时间的值。然后,我们使用datetime.fromtimestamp()函数将Unix Epoch时间转换为日期时间对象。然后,我们使用strftime()函数将对象转换为适当的格式字符串,并为用户打印出来。
使用Python将日期时间转换为毫秒级Epoch时间
为了将日期时间转换为毫秒级的Epoch时间,我们可以使用strptime()函数将指定的字符串返回日期时间对象,然后使用timestamp()函数将对象转换为秒数。此外,我们需要将结果值乘以千,以生成毫秒级Epoch日期时间。
让我们考虑以下示例,演示了相同的过程。
示例:
# importing the datetime module
from datetime import datetime
# using the strptime() function
dt_obj = datetime.strptime( '10-6-2021 10:33:35,72', '%d-%m-%Y %H:%M:%S,%f')
epoch_msec = dt_obj.timestamp() * 1000
print("epoch time in milliseconds:", epoch_msec)
输出:
epoch time in milliseconds: 1623301415720.0
解释:
在上面的示例中,我们从datetime库中导入datetime模块。然后,我们使用strptime()函数将日期时间字符串转换为日期时间对象。然后,我们使用timestamp()函数将对象转换为Epoch时间格式。最后,我们还将Epoch时间乘以千,以将其转换为毫秒。
使用Python将Epoch时间戳转换为日期时间
我们可以使用datetime.fromtimestamp()函数将Epoch时间戳转换为日期时间。以下是示例:
示例:
# importing the datetime module
import datetime
# given epoch timestamp
epoch_time = 33456871
# using the datetime.fromtimestamp() function
the_datetime = datetime.datetime.fromtimestamp( epoch_time )
# printing the values
print( "epoch timestamp:", epoch_time )
print( "DateTime:", the_datetime )
输出:
epoch timestamp: 33456871
DateTime: 1971-01-23 11:04:31
解释:
在上面的示例中,我们导入了datetime库,并定义了一个变量,其中包含Epoch时间戳的值。然后,我们使用datetime.fromtimestamp()函数将Epoch时间戳转换为日期时间,并将其打印给用户。
理解Python中与Epoch日期时间相关的错误
在使用Python编程语言中处理Epoch日期时间时,我们可能会遇到一个错误,该错误称为Python Epoch日期时间年份超出范围或Python无效参数。
让我们通过一个示例来理解这个问题。
示例:
# importing the datetime module
import datetime
# using the timestamp() function
epoch_time = datetime.datetime(1960, 6, 10, 0, 0).timestamp()
# printing the values
print( epoch_time )
输出:
Traceback (most recent call last):
File "D:\Python\epochpython.py", line 5, in
epoch_time = datetime.datetime(1960, 6, 10, 0, 0).timestamp()
OSError: [Errno 22] Invalid argument
解释:
在上面的示例中,我们提供了年份为1960的日期时间,这在1970年之前,并且超出了时间戳范围。因此,结果,程序将返回一个无效参数错误。
我们可以通过将日期从1950替换为1970之后的任何日期来解决这个无效参数错误。