time模块

时间的表示方法有三种:

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

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

import time   #导入time模块

d = time.time()  # 显示当前的时间戳

d
Out[16]: 1524570062.944023 time.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]: 2018 time.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模块

python的时间处理-time模块的更多相关文章

  1. Python之时间:datetime模块

    datetime在time基础之上封装了一些方法.但是time是经常使用的,datetime中的功能,time都能实现 一.datetime的三个模块 datetime.date datetime.t ...

  2. Python之时间:time模块

    import time   对于时间,使用最频繁的模块 1.获取当前时间 (1)时间戳 time.time() 时间戳:从1970年1月1日0点开始到现在按秒计算的偏移量 (2)时间元组 time.l ...

  3. Python之时间和日期模块

    1.import time 先要导入时间模块 1)time.time()得到当前的时间,返回的是时间戳,表示自1970年1月1日起到程序运行时的秒数 import time print(time.ti ...

  4. python之时间处理time模块

    import time import datetime ''' print(time.time()) #返回当前系统时间戳 print(time.ctime()) #返回当前系统时间 print(ti ...

  5. Python之时间:calender模块(日历)

    import calendar 1.星期 (1)calendar.day_name 星期的全称 print calendar.day_name for i in calendar.day_name: ...

  6. Python日期时间Date/Time

    Python程序可以处理多种方式的日期和时间.日期格式之间的转换是一种常见计算机的杂活. Python的时间和日历模块,能帮助处理日期和时间. Tick是什么? 为时间间隔,以秒为单位的浮点数.从“新 ...

  7. python 统计时间,写日志

    python 统计时间使用time模块,写日志使用logging模块,这两个都是标准模板. 测试socket使用socket模块 # 统计时间 ---------------------- impor ...

  8. Python的时间模块小结(转自:不懂真人)

    import datetimeprint time.time() #时间戳 print time.localtime(time.time()) #时间元组 print time.strftime('% ...

  9. 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码

    本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...

随机推荐

  1. POJ 2653 - Pick-up sticks - [枚举+判断线段相交]

    题目链接:http://poj.org/problem?id=2653 Time Limit: 3000MS Memory Limit: 65536K Description Stan has n s ...

  2. 使用nginx服务器如果遇到timeou情况时可以如下设置参数,使用fastcgi: fastcgi_connect_timeout 75; 链接 fastcgi_read_timeout 600; 读取 fastcgi_send_timeout 600; 发请求

    使用nginx服务器如果遇到timeou情况时可以如下设置参数,使用fastcgi: fastcgi_connect_timeout 75;  链接 fastcgi_read_timeout 600; ...

  3. 数据库管理系统的ACID特性

    数据库管理系统(DBMS)的事务都遵循着四种标准规格的约定.将这四种特性的首字母结合起来就统称为ACID特性.这些约定是所有DBMS都必须遵守的规则. 原子性 原子性是指在事务结束时,其中所包含的更新 ...

  4. pdb学习笔记

    参考资料:https://segmentfault.com/a/1190000006628456 下一行(不进入函数内部):n(ext) 单步(进入函数内部):s(tep) 打印:p 动态添加断点:1 ...

  5. Rikka with Sequence---hdu5828(区间更新与查找 线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5828 给你n个数,m个操作,操作k,l,r, k=1时 区间[l,r]每个数加x: k=2时,区间[l ...

  6. ubuntu环境下快速搭建开发环境

    接触ubuntu已经半年了,虽然游戏啊qq啊在linux下配置稍微麻烦一些,但是作为开发环境,ubuntu真的是好东西,无论是c啊还是php and etc 看到官网上文档开发环境建议wamp,如果是 ...

  7. POJ3468 a simple problem with integers 分块

    题解:分块 解题报告: 是个板子题呢qwq 没什么可说的,加深了对分块的理解趴还是 毕竟这么简单的板子题我居然死去活来WA了半天才调出来,,,哭了QAQ 还是说下我错在了哪几个地方(...是的,有好几 ...

  8. mysql 数据备份与恢复

    1.mysql的备份 命令:"mysqldump -u root -p 数据库名 [表名] > 备份文件名" 不写表名默认备份所有整个数据库. 注意:备份的文件中没有创建数据 ...

  9. 数据库级别DDL操作监控审计、数据库触发器/服务器触发器

    关键词:数据库触发器/服务器触发器  ,数据库级别DDL操作监控审计,禁止修改登录名密码 [1]数据库级别DDL操作监控审计 转自2012示例库,只能数据库级别,不能实例级别 use database ...

  10. Shell初学(二)变量及数组

    精简版: 定义:your_name=123      PS:=符号左右不能有空格! 使用:${your_name},单独使用变量时可以不加{} 只读:readonly your_name  PS:设置 ...