实用又强大,6款Python时间&日期库推荐

应用开发2025-11-05 13:09:3754495

在使用 Python 的实用时间开发过程中,除了使用 datetime 标准库来处理时间和日期,又强还有许多第三方的大款开源库值得尝试。

1、日期Arrow

Arrow 是库推一个专门处理时间和日期的轻量级 Python 库,它提供了一种合理、实用时间智能的又强方式来创建、操作、大款格式化、日期转换时间和日期,库推并提供了一个支持许多常见构建方案的实用时间智能模块 API 。简单来说,又强它可以帮你以更简便的大款操作和更少的代码来使用日期和时间。其设计灵感主要来源于 moment.js 和 requests 。服务器租用日期

Quick start

$ pip install arrow  >>> import arrow >>> utc = arrow.utcnow() >>> utc <Arrow [2013-05-11T21:23:58.970460+00:00]> >>> utc = utc.replace(hours=-1) >>> utc <Arrow [2013-05-11T20:23:58.970460+00:00]> >>> local = utc.to(US/Pacific) >>> local <Arrow [2013-05-11T13:23:58.970460-07:00]> >>> arrow.get(2013-05-11T21:23:58.970460+00:00) <Arrow [2013-05-11T21:23:58.970460+00:00]> >>> local.timestamp 1368303838 >>> local.format() 2013-05-11 13:23:58 -07:00 >>> local.format(YYYY-MM-DD HH:mm:ss ZZ) 2013-05-11 13:23:58 -07:00 >>> local.humanize() an hour ago >>> local.humanize(locale=ko_kr) 1시간 전  

2、库推Delorean

Delorean 提供了一个相比于 datetime 和 pytz 的更好的抽象,让你处理时间更容易。它有很多有用的处理时区的特性,标准化时区或者从一个时区改变到另外一个时区。

Quick start

from datetime import datetime import pytz est = pytz.timezone(US/Eastern) d = datetime.now(pytz.utc) d = est.normalize(d.astimezone(est)) return d   from delorean import Delorean d = Delorean() d = d.shift(US/Eastern) return d  

3、Pendulum

原生的 datetime 足够应付基本情况,但当面对更复杂的用例时,通常会有的捉襟见肘,不那么直观。 Pendulum 在标准库的基础之上,提供了一个更简洁,更易于使用的企商汇 API ,旨在让 Python datetime 更好用。

Quick start

>>> import pendulum >>> now_in_paris = pendulum.now(Europe/Paris) >>> now_in_paris 2016-07-04T00:49:58.502116+02:00 # Seamless timezone switching >>> now_in_paris.in_timezone(UTC) 2016-07-03T22:49:58.502116+00:00 >>> tomorrow = pendulum.now().add(days=1) >>> last_week = pendulum.now().subtract(weeks=1) >>> if pendulum.now().is_weekend(): ...     print(Party!) Party! >>> past = pendulum.now().subtract(minutes=2) >>> past.diff_for_humans() >>> 2 minutes ago >>> delta = past - last_week >>> delta.hours 23 >>> delta.in_words(locale=en) 6 days 23 hours 58 minutes # Proper handling of datetime normalization >>> pendulum.create(2013, 3, 31, 2, 30, 0, 0, Europe/Paris) 2013-03-31T03:30:00+02:00 # 2:30 does not exist (Skipped time) # Proper handling of dst transitions >>> just_before = pendulum.create(2013, 3, 31, 1, 59, 59, 999999, Europe/Paris) 2013-03-31T01:59:59.999999+01:00 >>> just_before.add(microseconds=1) 2013-03-31T03:00:00+02:00 

4、dateutil

dateutil 是 datetime 标准库的一个扩展库,几乎支持以所有字符串格式对日期进行通用解析,日期计算灵活,内部数据更新及时。

Quick start

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

5、moment

用于处理日期/时间的 Python 库,设计灵感同样是来源于 moment.js 和 requests ,设计理念源自 Times Python 模块。

Usage

import moment from datetime import datetime # Create a moment from a string moment.date("12-18-2012") # Create a moment with a specified strftime format moment.date("12-18-2012", "%m-%d-%Y") # Moment uses the awesome dateparser library behind the scenes moment.date("2012-12-18") # Create a moment with words in it moment.date("December 18, 2012") # Create a moment that would normally be pretty hard to do moment.date("2 weeks ago") # Create a future moment that would otherwise be really difficult moment.date("2 weeks from now") # Create a moment from the current datetime moment.now() # The moment can also be UTC-based moment.utcnow() # Create a moment with the UTC time zone moment.utc("2012-12-18") # Create a moment from a Unix timestamp moment.unix(1355875153626) # Create a moment from a Unix UTC timestamp moment.unix(1355875153626, utc=True) # Return a datetime instance moment.date(2012, 12, 18).date # We can do the same thing with the UTC method moment.utc(2012, 12, 18).date # Create and format a moment using Moment.js semantics moment.now().format("YYYY-M-D") # Create and format a moment with strftime semantics moment.date(2012, 12, 18).strftime("%Y-%m-%d") # Update your moments time zone moment.date(datetime(2012, 12, 18)).locale("US/Central").date # Alter the moments UTC time zone to a different time zone moment.utcnow().timezone("US/Eastern").date # Set and update your moments time zone. For instance, Im on the # west coast, but want NYCs current time. moment.now().locale("US/Pacific").timezone("US/Eastern") # In order to manipulate time zones, a locale must always be set or # you must be using UTC. moment.utcnow().timezone("US/Eastern").date # You can also clone a moment, so the original stays unaltered now = moment.utcnow().timezone("US/Pacific") future = now.clone().add(weeks=2)  

6、When.py

提供对用户非常友好的特性来帮助执行常见的日期和时间操作。

Usage 

本文地址:http://www.bhae.cn/html/137e22799635.html
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

热门文章

全站热门

如何折叠手机支架,轻松享受便携观影体验(简单易学,以手机支架为切入点,掌握折叠技巧)

在Ubuntu下将chm文件转成pdf格式总共分为三个步骤:1.将chm提取出html文件: 2.将html按页排序 3.转换成pdf 在Ubuntu下进行如下操作:1.安装libchm-bin,  htmldocsudo apt-get install libchm-bin htmldoc2.提取出html文件extract_chmLib 3.转换成pdfhtmldoc -t pdf13 --webpage -f myFile.pdf *.html

目前 Opera 提供了针对 Ubuntu 的 .deb 文件(官方下载地址 http://opera.cn.uptodown.com/ubuntu)。Debian的.deb文件应该也可以工作。 Opera 提供了两种基本安装类型:使用静态库和共享库。它们的区别是 Opera 是如何链接到 Qt 库的。在共享库版本中,Opera 使用你机器上的 Qt,而静态库的版本则使用 Opera 内建的 Qt。静态库版本下载的体积更大,在菜单和文件选择器使用了点阵字库(没有抗锯齿功能)。动态的编译版本集成到系统中,这样它使用你其它 Qt 应用程序相同的抗锯齿设置。静态的编译版本在浏览器窗口和其它用户界面还是支持抗锯齿功能。静态版本可能更快和更稳定,至少 Opera 的爱好者已经体验了很长时间。你可以都试试,因为在 Ubuntu 中安装和卸载软件相当容易。 请访问Opera 官方下载http://opera.cn.uptodown.com/ubuntu网站,选择你需要的包并选择一个下载镜像站点。 你也可以访问Opera FTP或网站以获得最新的版本。 保存你下载的文件 安装Opera打开一个终端,转移到你放置下载的deb文件的位置,输入: sudo dpkg -i opera.deb注:Tab 键能够自动帮助你输入较长的复杂的文件名。前提是你在正确的目录中,只要输入“opera”并在上述命令说明的地方按下键盘上的 Tab,Ubuntu 会自动帮助你输入完整的文件名。 安装Opera需要一点时间。 假如你得到一些信息显示依赖的包未安装,请输入: sudo apt-get -f installapt 将会安装这些缺失的包。假如需要最新版,需要自己下载其中opera10.63更新,修复了输入法问题,可以正常连续输入文字了。 http://bbs.operachina.com/viewtopic.php f=78&t=89346 下载地址:http://snapshot.opera.com/unix/22184_10.63-6447/替换方案——通过 apt-get 安装 Opera将下列信息添加到你的 apt sources.list(/etc/apt/sources.list) 文件: deb http://deb.opera.com/opera/ pool/non-free 0 opera或者,你不需要最新的版本可以选择稳定的版本: deb http://deb.opera.com/opera/ stable non-free.修改后保存 sources.list 文件,然后在终端中输入: sudo apt-get update接下来: sudo apt-get install opera所有需要的依赖关系将会被处理,并且你不需要像上面所述创建桌面文件,这些都自动完成了。(译注:事实似乎并非如此,建议你将本文“Ubuntu 化 Opera(完成安装)”的部分进行一遍。) 另一个好处是你不需要持续关注未来的升级,它会自动完成。 在更高版本的opera, 他会自动建立一个文件 /etc/apt/sources.list.d/opera.list , 就像 google-chrome会建立 google-chrome.list 一样, 这个文件就是源信息. 可能在opera 10 测试版本,这个文件名会是 /etc/apt/sources.list.d/opera , 只要手工添加 .list 后缀就行. 假如提示只有 lenny , 只要在文件中找到 opera-beta字样, 改为: deb http://deb.opera.com/opera-beta/ lenny non-free就行. 以后只要 sudo apt-get update ; sudo apt-get install opera 或 apt-get upgrade 就能升级他.很方便. 设置 Opera 为默认浏览器要将 Opera 设置为系统默认的浏览器和邮件客户端,请这么做: 进入“系统 ->首选项 ->首选应用程序” 在“Web 浏览器”标签页选择“自定义”并输入: opera -newpage %s在“邮件阅读器”标签页选择“自定义”并输入: opera -newmail %s启动浏览器你可以通过“应用程序 ->Internet ->Opera”启动这个非常棒的浏览器了。ubuntu里的opera设置一首先,打开opera的 工具——首选项——高级——内容—— 打开 插件选项 ——更改路径,去掉 火狐的插件文件夹的勾。 一、flash插件 1.到Adobe网站(http://get.adobe.com/flashplayer/)下载flashplayer的插件,选择下载tar.gz的那个; 2.解压后,你会发现目录中有一个libflashplayer.so的文件(不用理那个脚本,我们不安装); 3.关闭Opera,把这个libflahsplayer.so放到你的Opera插件目录中,比如/usr/lib/opera/plugins/。假如不知道有哪些插件目录,在Opera中首选项->高级->内容->插件选项,弹出的窗口下方就是你现有的插件路径。当然你也可更改添加路径,但务必保证libflahsplayer.so文件放在现有的的路径中; 4.重新开启Opera,去一些网站测试一下吧:新浪、本友会。假如看到flash播放的内容,那就说明flash插件安装成功。 PS:假如你发现还是无法播放flash,情仔细检查你的插件路径是否正确;或者在Opera中按F12,查看是否开启了插件(Enable plug-ins)。假如你安装的是ubuntu系统,那么可以下载deb格式的那个,直接关闭opera,双击运行就ok了。 二、Opera之在线影音 将附件目录中的所有10个文件(5个*.so、5个*.xpt)拷到Opera的插件目录。比如/usr/local/opera/lib/opera/plugins。注意这个路径必须在Opera的可用插件路径中。 我们会发现,无论是火狐还是opera,在百度mp3那里试听歌曲时,假如离开页面歌曲就暂停了,这个情况,其实也很好解决,只要在播放器上 右击 选择配置——并 去掉 离开窗口 暂时播放 前面的勾就搞定了。也就是最后的一个选项。

远程共享文件方案中常见错误及解决方法(为什么远程共享文件时会发生错误,如何解决?)

戴尔2720怎么样?一款高性能显示器的全面评测

电脑安装游戏显示系统错误的解决方法(诊断和修复常见的系统错误问题)

电脑无法找到DNS错误解决方法(解析DNS错误的有效方法及实用工具推荐)

Canonical在最近宣布了Ubuntu 15.04 的发布计划,最终发布日期定为2015年4月23日。详细发布计划: Alpha 1 – 12月18日 (特色版本)Alpha 2 – 1月22日  (特色版本)特性冻结— 2月19日Beta 1 – 1月26日 (特色版本)UI 冻结 — 3月12Final Beta –3月26内核冻结 —4月9日RC版本– 4月16日

热门文章

友情链接

滇ICP备2023000592号-9