ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python的时间处理-time模块

2021-05-27 21:54:29  阅读:161  来源: 互联网

标签:24 python 元组 tm 2018 模块 time Out


time模块

时间的表示方法有三种:

  • 时间戳:表示的是从1970年1月1日0点至今的秒数
  • 格式化字符串表示:这种表示更习惯我们通常的读法,如2018-04-24 00:00:00
  • 格式化元祖表示:是一个具有九个元素的元祖

时间戳与格式元组的互相转换

import time   #导入time模块d = time.time()  # 显示当前的时间戳d
Out[16]: 1524570062.944023time.localtime(d)    #把时间戳转换为含9个元素的元组,转换为当地时间Out[17]: time.struct_time(tm_year=2018, tm_mon=4, tm_mday=24, tm_hour=19, tm_min=41, tm_sec=2, tm_wday=1, tm_yday=114, tm_isdst=0)

time.gmtime(d)     #把时间戳转换为含9个元素的元组,转换为UTC时间Out[18]: time.struct_time(tm_year=2018, tm_mon=4, tm_mday=24, tm_hour=11, tm_min=41, tm_sec=2, tm_wday=1, tm_yday=114, tm_isdst=0)

ld = time.localtime(d)  #ld.tm_year    #转换的元组,可以根据需要单独计算对应的时间Out[20]: 2018time.mktime(ld)  # 把元组在转换回时间戳Out[21]: 1524570062.0

备注:UTC时间为格林威治时间,比东八区晚8个小时!

 

时间戳与格式化字符串的转换:

时间戳-------->格式化元组---------->格式化字符串

help(time.strftime)
Help on built-in function strftime in module time:

strftime(...)
    strftime(format[, tuple]) -> string   #函数的格式使用    
    Convert a time tuple to a string according to a format specification.
    See the library reference manual for formatting codes. When the time tuple    is not present, current time as returned by localtime() is used.
    
    Commonly used format codes:    
    %Y  Year with century as a decimal number. 
    %m  Month as a decimal number [01,12].    %d  Day of the month as a decimal number [01,31].    %H  Hour (24-hour clock) as a decimal number [00,23].    %M  Minute as a decimal number [00,59].    %S  Second as a decimal number [00,61].    %z  Time zone offset from UTC.    %a  Locale's abbreviated weekday name.
    %A  Locale's full weekday name.
    %b  Locale's abbreviated month name.
    %B  Locale's full month name.
    %c  Locale's appropriate date and time representation.
    %I  Hour (12-hour clock) as a decimal number [01,12].    %p  Locale's equivalent of either AM or PM.    
    Other codes may be available on your platform.  See documentation for
    the C library strftime function.
ld   #格式化元组的数据Out[22]: time.struct_time(tm_year=2018, tm_mon=4, tm_mday=24, tm_hour=19, tm_min=41, tm_sec=2, tm_wday=1, tm_yday=114, tm_isdst=0)

time.strftime("%Y-%m-%d %H:%M:%S", ld)
Out[25]: '2018-04-24 19:41:02'time.strftime("%Y-%m-%d %X", ld)
Out[26]: '2018-04-24 19:41:02'time.strftime("%Y-%m-%d %X %p", ld) #时间戳转换为格式化元组Out[27]: '2018-04-24 19:41:02 PM'#字符串转换为格式化元组,注意格式对应time.strptime('2018-04-24 19:41:02 PM',"%Y-%m-%d %H:%M:%S %p")
Out[32]: time.struct_time(tm_year=2018, tm_mon=4, tm_mday=24, tm_hour=19, tm_min=41, tm_sec=2, tm_wday=1, tm_yday=114, tm_isdst=-1)

根据时间戳转换为字符格式

help(time.ctime)
Help on built-in function ctime in module time:

ctime(...)
    ctime(seconds) -> string
    
    Convert a time in seconds since the Epoch to a string in local time.
    This is equivalent to asctime(localtime(seconds)). When the time tuple is
    not present, current time as returned by localtime() is used.


time.ctime(time.time())
Out[34]: 'Tue Apr 24 20:02:46 2018'

根据struct_time格式的元组转换为字符串

help(time.asctime)
Help on built-in function asctime in module time:

asctime(...)
    asctime([tuple]) -> string
    
    Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
    When the time tuple is not present, current time as returned by localtime()    is used.


time.asctime(ld)
Out[36]: 'Tue Apr 24 19:41:02 2018'
datetime模块

dateimte模块对time模块进行了封装,提供了更多功能的接口:

在csdn上看到一个博客,讲的蛮详细的,博主禁止转载,因此附上链接。datetime模块

 

标签:24,python,元组,tm,2018,模块,time,Out
来源: https://blog.51cto.com/lovejxs/2823928

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有