Python之深入解析优秀的时间格式转换工具Delorean
一、前言
- DeLorean 是一个 Python 的第三方模块,基于 pytz 和 dateutil 开发的,用于处理 Python 中日期时间的格式转换。
- 由于时间转换是一个足够微妙的问题,DeLorean 希望为移位、操作和生成日期时间提供一种更干净、更省事的解决方案。比如,实例化字符串形式的时间对象,Delorean 只需要 parse 指定字符串,不需要声明其格式就可以进行转换。
- 至于 Delorean 这个模块名称的由来,Delorean 是电影《回到未来》里的那辆极为炫酷的鸥翼汽车,采用这部电影里的非常具有代表性的汽车的名字作为库名,作者估计也是想表达使用这个库能让我们在时空里任意遨游,没有掣肘。
二、准备
① Python 安装
- 在 windows 上安装 Python:
- 下载 Python 的最新版本:访问链接 Python 官网,在 Windows 操作系统上安装 Python 3.10.0,大家也可以自行选择最新版的下载。
- 进入下一个页面后,拉到最下面,选择 Windows embeddable package (64-bit) 下载:
- 双击下载完成的应用程序,记得一定要勾选 ADD Python 3.7 to PATH,否则 cmd 无法运行 Python,然后选择 Install Now 进行安装,默认会安装 pip,而且将安装到 C 盘(推荐)。如果希望换一个盘安装,可以点击 Customize installation 在里面选择其他的盘。
- 在 macOS 上安装 Python:
- 其实 macOS 上是默认安装了 Python 的,只不过版本是 Python2.7,其相对于现在的 Python 3 缺少了很多特性,而且速度也比 Python 3 慢,因此还是建议大家装 Python 3。
- 同样地,下载 Python 的最新版本,访问链接:Python 官网,在 macOS 操作系统上安装 Python 3.7.4,大家也可以自行选择最新版的下载。
- 进入下一个页面后,拉到最下面,选择 macOS 64-bit universal2 installer 下载:
- 下载完成后得到一个 pkg 文件,双击打开,按照指示步骤进行安装即可。
② 安装依赖
- 请选择以下任一种方式输入命令安装依赖:
- Windows 环境打开 Cmd (开始 - 运行 - CMD);
- MacOS 环境打开 Terminal (command + 空格输入 Terminal);
- 如果使用的是 VSCode 编辑器或 Pycharm,可以直接使用界面下方的 Terminal。
pip install Delorean
三、Delorean 基础使用
- 轻松获取当前时间:
from delorean import Deloreand = Delorean()
print(d)
# Delorean(datetime=datetime.datetime(2021, 10, 6, 9, 5, 57, 611589), timezone='UTC')
- 将 datetime 格式的时间转化为 Delorean:
import datetime
from delorean import Deloreand = Delorean()
print(d)
d = Delorean(datetime=datetime.datetime(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC')
# 这里默认的是UTC时间
print(d)
# Delorean(datetime=datetime.datetime(2021, 10, 6, 9, 5, 57, 611589), timezone='UTC')
# Delorean(datetime=datetime.datetime(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC')
- 转换为国内时区:
import datetime
from delorean import Deloreand = Delorean(datetime=datetime.datetime(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC')
d = d.shift("Asia/Shanghai")
print(d)
# Delorean(datetime=datetime.datetime(2018, 5, 10, 16, 52, 23, 560811), timezone='Asia/Shanghai')
- 输出为 datetime、date 也不在话下:
import datetime
from delorean import Deloreand = Delorean(datetime=datetime.datetime(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC')
d = d.shift("Asia/Shanghai")
print(d.datetime)
print(d.date)
# 2018-05-10 16:52:23.560811+08:00
# 2018-05-10
- 查看无时区时间及时间戳:
import datetime
from delorean import Deloreand = Delorean(datetime=datetime.datetime(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC')
d = d.shift("Asia/Shanghai")
print(d.epoch)
print(d.naive)
# 1525942343.560811
# 2018-05-10 08:52:23.560811
- 用 unix 时间戳初始化 Delorean:
from delorean import epoch
d = epoch(1357971038.102223).shift("Asia/Shanghai")
print(d)
# Delorean(datetime=datetime.datetime(2013, 1, 12, 14, 10, 38, 102223), timezone='Asia/Shanghai')
- Delorean 支持 timedelta 的时间加减法。Delorean 可以使用 timedelta 进行加减,得到一个 Delorean 对象:
import datetime
from delorean import Deloreand = Delorean(datetime=datetime.datetime(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC')
d = d.shift("Asia/Shanghai")
print(d)
d2 = d + datetime.timedelta(hours=2)
print(d2)
d3 = d - datetime.timedelta(hours=3)
print(d3)
# Delorean(datetime=datetime.datetime(2018, 5, 10, 16, 52, 23, 560811), timezone='Asia/Shanghai')
# Delorean(datetime=datetime.datetime(2018, 5, 10, 18, 52, 23, 560811), timezone='Asia/Shanghai')
# Delorean(datetime=datetime.datetime(2018, 5, 10, 13, 52, 23, 560811), timezone='Asia/Shanghai')
四、Delorean 高级使用
- 通常情况下不关心有多少微妙或者多少秒,因此 Delorean 提供了非常方便的过滤方式:
from delorean import Deloreand = Delorean()
print(d)
# Delorean(datetime=datetime.datetime(2019, 3, 14, 4, 0, 50, 597357), timezone='UTC')
d.truncate('second')
# Delorean(datetime=datetime.datetime(2019, 3, 14, 4, 0, 50), timezone='UTC')
d.truncate('hour')
# Delorean(datetime=datetime.datetime(2019, 3, 14, 4, 0), timezone='UTC')
d.truncate('month')
# Delorean(datetime=datetime.datetime(2019, 3, 1, 0, 0), timezone='UTC')
d.truncate('year')
# Delorean(datetime=datetime.datetime(2019, 1, 1, 0, 0), timezone='UTC')
- 另外,datetime 格式的字符串处理的时候转换需要标明各种各样的格式,在 Delorean 直接 parse 就可以了:
from delorean import parse
parse("2011/01/01 00:00:00 -0700")
# Delorean(datetime=datetime.datetime(2011, 1, 1, 0, 0), timezone=pytz.FixedOffset(-420))
parse("2018-05-06")
# Delorean(datetime=datetime.datetime(2018, 6, 5, 0, 0), timezone='UTC')
Python之深入解析优秀的时间格式转换工具Delorean相关推荐
- Movavi HD Video Converter for Mac (优秀的视频格式转换工具) v22.1
今天和大家分享 Movavi HD Video Converter for Mac 中文版本,这是一款Mac上强大易用的视频格式转换工具,支持几乎所有常见的视频格式,提供大量针对不同设备的预设,简单易 ...
- 10款优秀的在线格式转换工具
1.online-convert online-convert是一个十分优秀的在线格式转换应用,支持格式覆盖文档.图片.音频.视频.电子书.Flash以及一些 不常见的格式.使用非常简单,你只需根据在 ...
- 牛逼!这个 Python 时间格式转换工具太优秀了!
这是「进击的Coder」的第 493 篇技术分享 作者:Ckend 来源:Python 实用宝典 " 阅读本文大概需要 6 分钟. " DeLorean 是一个 Python 的第 ...
- 2022-09-04T08:56:35.000+0000 时间格式转换
今天在跟前端联调接口的时候,发现前端显示的时间格式有点问题,想将其转为正确的时间格式,于是抽取了一个工具方法. /*** 时间格式转换工具** @param beforeTime 2022-09-04 ...
- # Python 字符串数据(二进制编码数据)格式转换及解析
Python 字符串数据(二进制编码数据)格式转换及解析 最近在linux上部署python,希望可以用python来调用串口,同时可以实现数据的格式转换. 在python这个做格式化转换不熟悉,花了 ...
- python 时间格式转换,秒级和毫秒级时间戳转换
1. 时间格式转换 # -*- coding=utf8 -*- import datetime import time import pytzutc = "2017-07-28T08:28: ...
- pandas 如何把时间转成index_pandas 时间格式转换的实现
pandas 时间格式转换的实现 OUTLINE 常见的时间字符串与timestamp之间的转换 日期与timestamp之间的转换 常见的时间字符串与timestamp之间的转换 这里说的字符串不是 ...
- php把时间格式转换为时间戳,php如何将时间格式转换成时间戳?
php时间格式转换为时间戳的方法:1.使用mktime()将时间转换为时间戳,语法为"mktime(小时.分钟.秒.月.日.年)":2.使用strtime()将字符串表示的日期转换 ...
- java输出格林威治时间_Java之格林威治时间格式转换成北京时间格式
Java之格林威治时间格式转换成北京时间格式 /** * Copyright 2020. yaodouwang.com Studio All Right Reserved * * Create on ...
最新文章
- C++知识点54——RTTI(运行时类型识别)
- ListView.getChildCount() 详解
- Dubbo 3.0 前瞻:重塑 Spring Cloud 服务治理
- HDOJ 2176 取石子游戏
- Python学习笔记:常用内建模块2:collections
- shell写的彩色进度条
- vmware_vcenter_api
- jqgrid删除某一列(隐藏)
- jprofiler安装与使用
- mysql Windows导入sql 失败
- 多目录多源文件的驱动Makefile模板
- 天题系列:Substring with Concatenation of All Words
- Matlab输出所有汉字(Unicode)
- FPGA UART串口协议
- 29-SpringBoot 安全与SpringSecurity
- 上传图片 可限制大小和文件类型
- 软件企业认定的好处及条件
- 强化学习(1)-介绍
- xctf攻防世界 MISC高手进阶区 黄金六年
- CSDN高校俱乐部“名师高校行”——贵州遵义站