time模块

time 模块可用来处理时间,详细的说明参考 time模块说明

逝去的秒数

逝去的秒数表示从某个时间(Python中是“Thu Jan 1 07:00:00 1970”)开始到现在所经过的秒数。

使用 time.time() 函数可以获得逝去的秒数:

>>time.time()
1388330058.8643

time.time()返回一个浮点数,可用于计算,比较,存储时间时间。

输出可读性强的时间字符串

要输出像“Thu Jan 1 07:00:00 1970”这种可读性强的时间字符串,我们可以使用 time.ctime() 函数,默认返回当前的时间:

>>> time.ctime()
'Sun Dec 29 23:17:44 2013'

也可以传入一个逝去的秒数,返回对应的时间字符串:

>>> time.ctime(0)
'Thu Jan 1 07:00:00 1970'

获取时间的各个部分信息

我们经常要分别获取一个时间的年、月、日、时、分、秒等信息,time模块定义了一个 struct_time 类型,用来存储时间的各个部分信息。该类型实现了元组协议,所以可以当作元组使用。

struct_time 的结构如下:

Index Attribute Values
0 tm_year (for example, 1993)
1 tm_mon range [1, 12]
2 tm_mday range [1, 31]
3 tm_hour range [0, 23]
4 tm_min range [0, 59]
5 tm_sec range [0, 61]; see (2) in
strftime() description
6 tm_wday range [0, 6], Monday is 0
7 tm_yday range [1, 366]
8 tm_isdst 0, 1 or -1; see below

有几个函数都可以返回struct_time, gmtime() 返回当前的UTC时间,localtime() 返回当前时间域的当前时间:

>>> time.gmtime()
time.struct_time(tm_year=2013, tm_mon=12, tm_mday=29, tm_hour=15, tm_min=35, tm_sec=57, tm_wday=6, tm_yday=363, tm_isdst=0)
>>> time.localtime()
time.struct_time(tm_year=2013, tm_mon=12, tm_mday=29, tm_hour=23, tm_min=36, tm_sec=6, tm_wday=6, tm_yday=363, tm_isdst=0)

gmtime() 和 localtime() 也接受一个逝去的秒数作为参数,并转换成struct_time:

>>> time.localtime(0)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=7, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

mktime() 接收 struct_time 参数并将其转化为逝去的秒数:

>>> time.mktime(time.localtime())
1388331386.0

解析和格式化时间

如何将类似”Sun Dec 29 23:17:44 2013“的字符串解析成 struct_time?

time模块提供了两个函数来处理这方面的工作。
函数 strptime() 用于将时间字符串解析成 struct_time,
函数 strftime() 则将 struct_time 格式化成可读性强的时间字符串

import time

now = time.ctime()
print now
parsed = time.strptime(now)
print parsed
print time.strftime("%a %b %d %H:%M:%S %Y", parsed) # output =>
# Sun Mar 9 13:01:19 2008
# (2008, 3, 9, 13, 1, 19, 6, 69, -1)
# Sun Mar 09 13:01:19 2008

这两个函数都依赖特定的格式说明信息,默认的格式说明为”%a %b %d %H:%M:%S %Y“。完整的格式列表可以在 这里 找到。

Directive Meaning Notes
%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.
 
%d Day of the month as a decimal
number [01,31].
 
%H Hour (24-hour clock) as a
decimal number [00,23].
 
%I Hour (12-hour clock) as a
decimal number [01,12].
 
%j Day of the year as a decimal
number [001,366].
 
%m Month as a decimal number
[01,12].
 
%M Minute as a decimal number
[00,59].
 
%p Locale’s equivalent of either
AM or PM.
(1)
%S Second as a decimal number
[00,61].
(2)
%U Week number of the year
(Sunday as the first day of
the week) as a decimal number
[00,53]. All days in a new
year preceding the first
Sunday are considered to be in
week 0.
(3)
%w Weekday as a decimal number
[0(Sunday),6].
 
%W Week number of the year
(Monday as the first day of
the week) as a decimal number
[00,53]. All days in a new
year preceding the first
Monday are considered to be in
week 0.
(3)
%x Locale’s appropriate date
representation.
 
%X Locale’s appropriate time
representation.
 
%y Year without century as a
decimal number [00,99].
 
%Y Year with century as a decimal
number.
 
%Z Time zone name (no characters
if no time zone exists).
 
%% A literal '%' character.  

Python中处理时间 —— time模块的更多相关文章

  1. Python中的random模块,来自于Capricorn的实验室

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  2. Python中的logging模块

    http://python.jobbole.com/86887/ 最近修改了项目里的logging相关功能,用到了python标准库里的logging模块,在此做一些记录.主要是从官方文档和stack ...

  3. Python中的random模块

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  4. 浅析Python中的struct模块

    最近在学习python网络编程这一块,在写简单的socket通信代码时,遇到了struct这个模块的使用,当时不太清楚这到底有和作用,后来查阅了相关资料大概了解了,在这里做一下简单的总结. 了解c语言 ...

  5. python中的StringIO模块

    python中的StringIO模块 标签:python StringIO 此模块主要用于在内存缓冲区中读写数据.模块是用类编写的,只有一个StringIO类,所以它的可用方法都在类中.此类中的大部分 ...

  6. python中的select模块

    介绍: Python中的select模块专注于I/O多路复用,提供了select  poll  epoll三个方法(其中后两个在Linux中可用,windows仅支持select),另外也提供了kqu ...

  7. Python中的re模块--正则表达式

    Python中的re模块--正则表达式 使用match从字符串开头匹配 以匹配国内手机号为例,通常手机号为11位,以1开头.大概是这样13509094747,(这个号码是我随便写的,请不要拨打),我们 ...

  8. python中的shutil模块

    目录 python中的shutil模块 目录和文件操作 归档操作 python中的shutil模块 shutil模块对文件和文件集合提供了许多高级操作,特别是提供了支持文件复制和删除的函数. 目录和文 ...

  9. Python中使用operator模块实现对象的多级排序

    Python中使用operator模块实现对象的多级排序 今天碰到一个小的排序问题,需要按嵌套对象的多个属性来排序,于是发现了Python里的operator模块和sorted函数组合可以实现这个功能 ...

随机推荐

  1. asp.net mvc 从数据库中读取图片的实现代码

    首先是创建一个类,继承于ActionResult,记住要引用System.Web.Mvc命名空间,如下: public class ImageResult : ActionResult { publi ...

  2. Oracle基础(八) 数据完整性

    一.数据完整性 数据完整性要求数据库中的数据具有准确性.准确性是通过数据库表的设计和约束来实现的.为了实现数据完整性,数据库需要做两方面的工作: 确保每行的数据符合要求. 确保每列的数据符合要求. 为 ...

  3. Windows防火墙出站、入站相关知识总结

    出站默认是全部允许,只禁止相关出站规则指定的条目:入站默认是全部禁止,只允许相关入站规则指定的条目. 入站规则其实没什么好说的,直接添加规则允许某IP或端口访问,或允许某程序全部访问,就行了. 要控制 ...

  4. 转:关于视频H264编解码的应用实现

    转:http://blog.csdn.net/scalerzhangjie/article/details/8273410 项目要用到视频编解码,最近半个月都在搞,说实话真是走了很多弯路,浪费了很多时 ...

  5. 【转】亿欧盘点:杭州十家代表性O2O企业

    [ 亿欧导读 ] 11月13日亿欧网将走入杭州,联合B座12楼.正和岛召开“2014 中国O2O新商业峰会“.亿欧网据O2O产业图谱,整理出杭州十家O2O企业:点我吧.快的打车.杭州19楼.婚礼纪.淘 ...

  6. JNA参数传递问题,Java数组

    版权声明:本文为博主原创文章,未经博主允许不得转载. 本文主要讲述使用JNA模拟结构体并将结构体数组作为参数传递给对应的方法. C语言结构体定义如下: typedef struct Rect { in ...

  7. Windows命令行中使用SSH连接Linux

    转自 http://www.linuxidc.com/Linux/2014-02/96625.htm 1.下载: openssh for Winodws: 免费下载地址在 http://linux.l ...

  8. MyFragment

    手机横竖屏自动切换不同的View: Landscape-Horizontal-横屏 Portrait-Vertical-竖屏 package com.example.shad_fnst.myfragm ...

  9. 《转载》两个activity界面间跳转切换动画效果

    1overridePendingTransition Activity的切换动画指的是从一个activity跳转到另外一个activity时的动画. 它包括两个部分:一部分是第一个activity退出 ...

  10. js 调用php代码

    <?php $test = "var a = ".$_GET['test'].";"; ?> <mce:script type="t ...