Python之time模块13位时间戳字符串格式化与转换方法

2019-08-1219:27:33编程语言入门到精通Comments3,359 views字数 1952阅读模式

Python处理时间和时间戳的内置模块就有time,和datetime两个,本文先说time模块。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/15131.html

关于时间戳的几个概念文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/15131.html

时间戳,根据1970年1月1日00:00:00开始按秒计算的偏移量。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/15131.html

时间元组(struct_time),包含9个元素。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/15131.html

time.struct_time(tm_year=2017, tm_mon=10, tm_mday=1, tm_hour=14, tm_min=21, tm_sec=57, tm_wday=6, tm_yday=274, tm_isdst=0)

时间格式字符串,字符串形式的时间。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/15131.html

time模块与时间戳和时间相关的重要函数文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/15131.html

time.time() 生成当前的时间戳,格式为10位整数的浮点数。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/15131.html

time.strftime()根据时间元组生成时间格式化字符串。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/15131.html

time.strptime()根据时间格式化字符串生成时间元组。time.strptime()与time.strftime()为互操作。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/15131.html

time.localtime()根据时间戳生成当前时区的时间元组。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/15131.html

time.mktime()根据时间元组生成时间戳。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/15131.html

示例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/15131.html

关于时间戳和格式化字符串的简单示例如下文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/15131.html

import time

#生成当前时间的时间戳,只有一个参数即时间戳的位数,默认为10位,输入位数即生成相应位数的时间戳,比如可以生成常用的13位时间戳
def now_to_timestamp(digits = 10):
 time_stamp = time.time()
 digits = 10 ** (digits -10)
 time_stamp = int(round(time_stamp*digits))
 return time_stamp

#将时间戳规范为10位时间戳
def timestamp_to_timestamp10(time_stamp):
 time_stamp = int (time_stamp* (10 ** (10-len(str(time_stamp)))))
 return time_stamp

#将当前时间转换为时间字符串,默认为2017-10-01 13:37:04格式
def now_to_date(format_string="%Y-%m-%d %H:%M:%S"):
 time_stamp = int(time.time())
 time_array = time.localtime(time_stamp)
 str_date = time.strftime(format_string, time_array)
 return str_date

#将10位时间戳转换为时间字符串,默认为2017-10-01 13:37:04格式
def timestamp_to_date(time_stamp, format_string="%Y-%m-%d %H:%M:%S"):
 time_array = time.localtime(time_stamp)
 str_date = time.strftime(format_string, time_array)
 return str_date

#将时间字符串转换为10位时间戳,时间字符串默认为2017-10-01 13:37:04格式
def date_to_timestamp(date, format_string="%Y-%m-%d %H:%M:%S"):
 time_array = time.strptime(date, format_string)
 time_stamp = int(time.mktime(time_array))
 return time_stamp

#不同时间格式字符串的转换
def date_style_transfomation(date, format_string1="%Y-%m-%d %H:%M:%S",format_string2="%Y-%m-%d %H-%M-%S"):
 time_array = time.strptime(date, format_string1)
 str_date = time.strftime(format_string2, time_array)
 return str_date

实验文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/15131.html

print(now_to_date())
print(timestamp_to_date(1506816572))
print(date_to_timestamp('2017-10-01 08:09:32'))
print(timestamp_to_timestamp10(1506816572546))
print(date_style_transfomation('2017-10-01 08:09:32'))

结果为文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/15131.html

1506836224000
2017-10-01 13:37:04
2017-10-01 08:09:32
1506816572
1506816572
2017-10-01 08-09-32
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/15131.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/15131.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定