Python将SWL浮点类型时间戳转换为日期字符串(精确到秒)
在Python中,你可以使用datetime模块来进行时间戳的转换和格式化。下面是一个示例代码,演示了如何将SWL中的浮点类型时间戳转换为精确到秒的日期字符串:
import datetime
def swl_timestamp_to_datetime(timestamp):
# 将SWL中的浮点类型时间戳转换为datetime对象
dt = datetime.datetime.fromtimestamp(timestamp)
return dt
def datetime_to_string(dt):
# 将datetime对象格式化为字符串
dt_string = dt.strftime('%Y-%m-%d %H:%M:%S')
return dt_string
# 示例使用
sw_timestamp = 1627978956.123456789
dt = swl_timestamp_to_datetime(swl_timestamp)
dt_string = datetime_to_string(dt)
print(dt_string)
在上述代码中,swl_timestamp_to_datetime()函数将SWL中的浮点类型时间戳转换为datetime对象。然后,datetime_to_string()函数将datetime对象格式化为字符串,使用strftime()方法指定字符串的格式。最后,通过调用这两个函数,你可以将SWL时间戳转换为精确到秒的日期字符串。输出结果为:2021-08-03 10:09:16。
原文地址: https://www.cveoy.top/t/topic/qx0s 著作权归作者所有。请勿转载和采集!