python模块:时间处理模块
http://blog.csdn.net/pipisorry/article/details/53067168
常用python自带时间处理模块
python自带的时间处理模块参考[操作系统服务:time时间模块+datetime模块]
有一些情况下,datetime却并没有那么好用。比如:
1.创建日期时间范围
2.创建未固定的日期时间
3.检验两个日期时间的差值是否在一定的范围内。
dateutil模块
解析日期格式建议使用dateutil,不推荐使用datetime.datetime.strftime()。
The dateutil module provides powerful extensions tothe standard datetime module, available in Python.
>>> from dateutil.relativedelta import * >>> from dateutil.easter import * >>> from dateutil.rrule import * >>> from dateutil.parser import * >>> from datetime import * >>> now = parse("Sat Oct 11 17:13:46 UTC 2003") >>> today = now.date() >>> year = rrule(YEARLY,dtstart=now,bymonth=8,bymonthday=13,byweekday=FR)[0].year >>> rdelta = relativedelta(easter(year), today) >>> print("Today is: %s" % today) Today is: 2003-10-11 >>> print("Year with next Aug 13th on a Friday is: %s" % year) Year with next Aug 13th on a Friday is: 2004 >>> print("How far is the Easter of that year: %s" % rdelta) How far is the Easter of that year: relativedelta(months=+6) >>> print("And the Easter of that year is: %s" % (today+rdelta)) And the Easter of that year is: 2004-04-11
计算时间差的示例
其中中间lz加了一个timestamp的转换(为了输入到只接收整形数的函数中),直接不转换也可以。
from dateutil.parser import parse import datetime a = 'Sat Jul 31 20:15:24 +0000 2011' b = 'Sat Jul 30 20:07:44 +0000 2011' # print(parse(a)) aa = datetime.datetime.timestamp(parse(a)) bb = datetime.datetime.timestamp(parse(b)) print(datetime.datetime.fromtimestamp(aa) - datetime.datetime.fromtimestamp(bb)) )
[dateutil - powerful extensions to datetime]
Delorean:解决 Python 中有关日期处理的棘手问题的库
Delorean是一个非常酷的日期/时间库。这个项目由Mahdi Yusuf维护,类似JavaScript的moment,拥有非常完善的技术文档。
用它处理日期和时间非常方便。设置时区,截取到秒、分、小时,甚至使用特定步骤从一个日期进到另一个日期。文档里面有很多例子。
用Python处理时间有点痛苦。如果你用Python的标准时间库datetime,并且处理过时区,你会感到绝望。delorean提供了一个相比于datetime和pytz的更好的抽象,让你处理时间更容易。它有很多有用的处理时区的特性,标准化时区或者从一个时区改变到另外一个时区。
from delorean import Delorean
EST = "US/Eastern"
d = Delorean(timezone=EST)
其它时间相关的库
pytz python时区处理模块(同时也包括夏令时)。世界时区,使用tz database时区信息数据库。
arrow,更好的日期和时间处理Python库。
chronyk,一个Python 3版函数库,用于解析人写的时间和日期。
moment,类似Moment.js的日期/时间Python库。
radar,雷达,生成随机日期/时间。
sandglass(沙漏)** 是一个增强的、友好的时间处理库,目的是为了解放程序员的生产力。在python中有太多处理时间的库,datetime/date/time/calendar等等。需要记的细节太多,选择困难。而sandglass就是解决这个的青霉素。从各种麻烦的转换中解脱出来。只需记住 **Sandglass对象** 和 **ben()** 、 **tslice()** 、 **cronwalk()** 这几个主要的api即可。[sandglass 0.0.1]
Python日期时间处理: datestuff
when.py
友好的时间日期库
>>> import when >>> when.timezone() 'Asia/Shanghai' >>> when.today() datetime.date(2013, 5, 14) >>> when.tomorrow() datetime.date(2013, 5, 15) >>> when.now() datetime.datetime(2013, 5, 14, 21, 2, 23, 78838)
[https://github.com/dirn/When.py]
from: http://blog.csdn.net/pipisorry/article/details/53067168
ref: [Python时间库性能比较]
python模块:时间处理模块的更多相关文章
- Python 日期时间处理模块学习笔记
来自:标点符的<Python 日期时间处理模块学习笔记> Python的时间处理模块在日常的使用中用的不是非常的多,但是使用的时候基本上都是要查资料,还是有些麻烦的,梳理下,便于以后方便的 ...
- python基础===时间处理模块
时间模块 Python中有很多方便我们处理时间信息的模块 time 模块 datetime 模块 pytz 模块 dateutil 模块 这里我们着重介绍的是前两种 time模块 time.time( ...
- python模块 | 时间处理模块—datetime模块
在python中,与时间处理相关的模块有 time,datetime,calendar. 时间的两个概念: UTC(世界协调时): 整个地球分为二十四时区,每个时区都有自己的本地时间.格林威治天文时间 ...
- python时间-time模块
time是python自带的模块,用于处理时间问题,提供了一系列的操作时间的函数. 以下说明针对于 python2.7,其他版本可能有所差异. 模块提供了两个种表示时间的格式: 1.时间戳,是以秒表示 ...
- Python之日期与时间处理模块(date和datetime)
本节内容 前言 相关术语的解释 时间的表现形式 time模块 datetime模块 时间格式码 总结 前言 在开发工作中,我们经常需要用到日期与时间,如: 作为日志信息的内容输出 计算某个功能的执行时 ...
- Python常用模块-时间模块
在写代码的过程中,我们常常需要与时间打交道,在python中,与时间处理有关的模块有time,datetime和calendar.,这里主要介绍time和datetime模块 在python中,表示时 ...
- 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码
本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...
- python第十七天---时间模块、random模块
作完一个作业,开始新的学习: 有由今天的时间有限所有学习了以下两个模块,明天继续! 时间模块.random模块 import time #!usr/bin/env python #-*-coding: ...
- 【转】Python之日期与时间处理模块(date和datetime)
[转]Python之日期与时间处理模块(date和datetime) 本节内容 前言 相关术语的解释 时间的表现形式 time模块 datetime模块 时间格式码 总结 前言 在开发工作中,我们经常 ...
随机推荐
- Python中的上下文管理器和with语句
Python2.5之后引入了上下文管理器(context manager),算是Python的黑魔法之一,它用于规定某个对象的使用范围.本文是针对于该功能的思考总结. 为什么需要上下文管理器? 首先, ...
- python3+dlib人脸识别及情绪分析
一.介绍 我想做的是基于人脸识别的表情(情绪)分析.看到网上也是有很多的开源库提供使用,为开发提供了很大的方便.我选择目前用的比较多的dlib库进行人脸识别与特征标定.使用python也缩短了开发周期 ...
- Struts支持的contentType
'ez' => 'application/andrew-inset', 'hqx' => 'application/mac-binhex40', 'cpt' => 'applicat ...
- python3爬取女神图片,破解盗链问题
title: python3爬取女神图片,破解盗链问题 date: 2018-04-22 08:26:00 tags: [python3,美女,图片抓取,爬虫, 盗链] comments: true ...
- Codeforces Round #430 C. Ilya And The Tree
Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very ...
- bzoj 3745: [Coci2015]Norma
Description Solution 考虑分治: 我们要统计跨越 \(mid\) 的区间的贡献 分最大值和最小值所在位置进行讨论: 设左边枚举到了 \(i\),左边 \([i,mid]\) 的最大 ...
- ●BZOJ 4556 [Tjoi2016&Heoi2016]字符串
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4556 题解: 巨恶心...但是题很好呀,可以练习好几个比较麻烦的算法~ 1).预处理 首先用 ...
- NOIP2014-5-17模拟赛
Problem 1 双色球(ball.cpp/c/pas) [题目描述] 机房来了新一届的学弟学妹,邪恶的chenzeyu97发现一位学弟与他同名,于是他当起了善良的学长233 "来来来,学 ...
- 【UVA–11997 K Smallest Sums 】
·哦,这题要用优先队列?那大米饼就扔一个手写堆上去吧! ·英文题,述大意: 输入n个长度为n的序列(题中是k,2<=k<=750).一种结果定义为:从每个序列中都要挑选一个数加 ...
- 最近i学习微信卡券中的会员卡功能,弄清楚不容易 ,分享一下。
创建会员卡接口 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1451025283 [4.1 创建会员卡接口] { " ...