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

作者:mighty13 时间:2023-07-25 08:46:01 

Python处理时间和时间戳的内置模块就有time,和datetime两个,本文先说time模块。

关于时间戳的几个概念

时间戳,根据1970年1月1日00:00:00开始按秒计算的偏移量。

时间元组(struct_time),包含9个元素。


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)

时间格式字符串,字符串形式的时间。

time模块与时间戳和时间相关的重要函数

time.time() 生成当前的时间戳,格式为10位整数的浮点数。

time.strftime()根据时间元组生成时间格式化字符串。

time.strptime()根据时间格式化字符串生成时间元组。time.strptime()与time.strftime()为互操作。

time.localtime()根据时间戳生成当前时区的时间元组。

time.mktime()根据时间元组生成时间戳。

示例

关于时间戳和格式化字符串的简单示例如下


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

实验


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'))

结果为


1506836224000
2017-10-01 13:37:04
2017-10-01 08:09:32
1506816572
1506816572
2017-10-01 08-09-32

来源:https://blog.csdn.net/mighty13/article/details/78147357

标签:Python,time,时间戳,字符串,格式化
0
投稿

猜你喜欢

  • Python Flask 上传文件测试示例

    2021-01-08 05:51:34
  • Python详细讲解图像处理的而两种库OpenCV和Pillow

    2022-08-14 05:23:19
  • python爬虫框架scrapy下载中间件的编写方法

    2021-10-16 13:37:19
  • SQLServer中的触发器基本语法与作用

    2024-01-25 18:18:02
  • MySQL如何查询当前正在运行的SQL语句

    2009-02-13 13:40:00
  • python字符串拼接.join()和拆分.split()详解

    2021-11-12 04:09:17
  • golang等待触发事件的实例

    2024-05-08 10:17:31
  • Django drf分页器的使用详解

    2022-04-09 08:23:35
  • python网络爬虫精解之pyquery的使用说明

    2021-05-28 13:01:19
  • 实例演示在SQL中启用全文检索

    2011-10-01 14:01:37
  • Python入门教程2. 字符串基本操作【运算、格式化输出、常用函数】 <font color=red>原创</font>

    2021-08-21 17:17:21
  • python yield和Generator函数用法详解

    2022-12-21 04:18:47
  • 解决mysql输入密码闪退的问题

    2024-01-21 02:29:14
  • Python实现周期性抓取网页内容的方法

    2023-04-12 01:33:36
  • mysql本地登录无法使用端口号登录的解决方法

    2024-01-25 06:51:30
  • Python新手实现2048小游戏

    2021-02-19 14:12:57
  • 教你如何使Python爬取酷我在线音乐

    2021-02-18 14:13:01
  • 六个窍门助你提高Python运行效率

    2021-07-19 08:31:15
  • Javascript中作用域的详细介绍

    2024-04-18 10:02:09
  • Python中操作符重载用法分析

    2023-04-05 12:34:51
  • asp之家 网络编程 m.aspxhome.com