python-dateutil

Python 日期时间datetime 加一天,减一天,加减一小时一分钟,加减一年

天涯浪子 提交于 2020-08-14 04:15:04
计算年、月、日需要安装组件包 pip install python-dateutil 当前日期时间 import datetime print datetime.datetime.now() # 2018-05-08 16:53:30.101000 格式化时间 import datetime print datetime.datetime.now().strftime("%Y-%m-%d %H:%M") # 2018-05-08 16:54 多加一天 import datetime print (datetime.datetime.now()+datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S") # 2018-05-09 16:56:07 减一天 import datetime print (datetime.datetime.now()+datetime.timedelta(days=-1)).strftime("%Y-%m-%d %H:%M:%S") # 2018-05-07 16:56:59 可以把days改为hours minutes,就可以提前XX小时/分钟了。 timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[,

30 个 Python 的最佳实践、小贴士和技巧

可紊 提交于 2020-07-26 23:38:27
作者 | Erik-Jan van Baaren 译者 | 弯月 来源 | CSDN 原文: https://towardsdatascience.com /30-python-best-practices-tips-and-tricks-caefb9f8c5f5 以下为译文: 我想借本文为大家献上 Python 语言的30个最佳实践、小贴士和技巧,希望能对各位勤劳的程序员有所帮助,并希望大家工作顺利! 1、Python 版本 在此想提醒各位:自2020年1月1日起,Python 官方不再支持 Python 2。本文中的很多示例只能在 Python 3 中运行。如果你仍在使用 Python 2.7,请立即升级。 2、检查 Python 的最低版本 你可以在代码中检查 Python 的版本,以确保你的用户没有在不兼容的版本中运行脚本。检查方式如下: if not sys.version_info > (2, 7): # berate your user for running a 10 year # python version elif not sys.version_info >= (3, 5): # Kindly tell your user (s)he needs to upgrade # because you're using 3.5 features 3、IPython

Python timezone conversion adding minutes to the hour?

隐身守侯 提交于 2020-07-16 06:40:07
问题 So I'm trying to convert a bunch of hours (10:00:00, 14:00:00, etc) from a given timezone to UTC. When I do so, I keep maddeningly getting things back like "15:51:00". When you get to that line, and print what value it's using, it's using something like: 1900-01-01 12:00:00-05:51 Which is fine, except for the -05:51 bit. I have no idea why that -05:51 is there, and it's causing me problems. UTC conversion is hour to hour, yes? I think it's got something to do with my timezone conversions, but

Python timezone conversion adding minutes to the hour?

折月煮酒 提交于 2020-07-16 06:38:54
问题 So I'm trying to convert a bunch of hours (10:00:00, 14:00:00, etc) from a given timezone to UTC. When I do so, I keep maddeningly getting things back like "15:51:00". When you get to that line, and print what value it's using, it's using something like: 1900-01-01 12:00:00-05:51 Which is fine, except for the -05:51 bit. I have no idea why that -05:51 is there, and it's causing me problems. UTC conversion is hour to hour, yes? I think it's got something to do with my timezone conversions, but

Python timezone conversion adding minutes to the hour?

懵懂的女人 提交于 2020-07-16 06:38:33
问题 So I'm trying to convert a bunch of hours (10:00:00, 14:00:00, etc) from a given timezone to UTC. When I do so, I keep maddeningly getting things back like "15:51:00". When you get to that line, and print what value it's using, it's using something like: 1900-01-01 12:00:00-05:51 Which is fine, except for the -05:51 bit. I have no idea why that -05:51 is there, and it's causing me problems. UTC conversion is hour to hour, yes? I think it's got something to do with my timezone conversions, but

logwatch使用

谁说胖子不能爱 提交于 2020-04-29 20:04:53
logwatch是一款用 Perl 语言编写的开源日志解析分析器。它能对原始的日志文件进行解析并转换成结构化格式的文档,也能根据您的使用情况和需求来定制报告。logwatch 的主要目的是生成更易于使用的日志摘要,并不是用来对日志进行实时的处理和监控的。正因为如此,logwatch 通常被设定好时间和频率的自动定时任务来调度运行或者是有需要日志处理的时候从命令行里手动运行。一旦日志报告生成,logwatch 可以通过电子邮件把这报告发送给您,您可以把它保存成文件或者直接显示在屏幕上。 安装: yum install logwatch -y 配置文件:/usr/share/logwatch/default.conf/logwatch.conf 我目前不想让它每天都执行,所以就删掉了/etc/cron.daily/0logwatch文件 手动执行 logwatch ################### Logwatch 7.4.0 (03/01/11) #################### Processing Initiated: Wed Apr 29 18:18:37 2020 Date Range Processed: yesterday ( 2020-Apr-28 ) Period is day. Detail Level of Output: 0 Type of

日期文本互转,时间戳,时间差 以及 时区变换

时光怂恿深爱的人放手 提交于 2020-04-29 13:14:43
2019-05-25 重新编辑了一下 以下基本我在日常写代码中,最常用的一些日期相关部分。 1、当前时间转文本 strftime() 无论是 time 或 datetime ,哪个模块都可以,具体怎么输出,自行调整格式参数 '%Y-%m-%d %H:%M:%S' %字符 表意 数值范围 %y 年(2位) 00, 01, …, 99 %Y 年(4位) 0001, 0002, …, 2013, 2014, …, 9998, 9999 %m 月 01, 02, …, 12 %d 日 01, 02, …, 31 %H 时(24小时制) 00, 01, …, 23 %M 分 00, 01, …, 59 %S 秒 00, 01, …, 59 %f 毫秒 000000, 000001, …, 999999 %z 时区 (empty), +0000, -0400, +1030, +063415, -030712.345216 %a 工作日作为语言环境的缩写名称。 太阳,周一,......,周六(en_US); 所以,Mo,...,Sa(de_DE) %A 平日作为语言环境的全名。 星期日,星期一,......,星期六(en_US); Sonntag,Montag,......,Samstag(de_DE) %w 工作日为十进制数,其中0表示星期日,6表示星期六。 0,1,...,6 %d

【经验分享】30个使用Python编程的实践、建议和技巧

随声附和 提交于 2020-04-27 16:18:20
2020年,你立了什么新的 Flag?无论如何,只要在学编程,在用Python,我们都是一家人!我们先为大家准备 30 个非常优秀的 Python 实践技巧。希望这些诀窍能在实际工作中帮助大家,并且学到一些有用的知识。 1、使用 python 3 由于官方从2020年1月1日起就停止了对python2.7的更新支持,因此本教程的大部分例子都只能在python 3环境下运行。如果你仍然在使用2.7版本,请先升级到python 3。 2、检查并使用满足需求的最小python版本 你可以在代码中检查Python 版本,以确保你的代码使用者没有使用不兼容的版本运行脚本。使用以下代码进行简单的检查: if not sys.version_info > (2, 7): # berate your user for running a 10 year # python version elif not sys.version_info >= (3, 5): # Kindly tell your user (s)he needs to upgrade # because you're using 3.5 features 3、使用IPython IPython 基本上是一个增强的shell,仅仅是为了自动补全功能,IPython也是值得使用的,但其实它还有更多作用,包括内建的Magic命令

datetime strftime does not output correct timestamp

六月ゝ 毕业季﹏ 提交于 2020-01-14 14:56:29
问题 The following: >>> from dateutil.parser import parse >>> parse("2013-07-02 00:00:00 -0000") datetime.datetime(2013, 7, 2, 0, 0, tzinfo=tzutc()) shows that the time should be 12am on July 2nd 2013 in UTC. However: >>> parse("2013-07-02 00:00:00 -0000").strftime("%s") '1372744800' 1372744800 is actually Tue, 02 Jul 2013 06:00:00 UTC, which is wrong. Very confused. 回答1: See this question: Convert python datetime to epoch with strftime Python doesn't actually support %s as an argument to strftime

Extraction of some date formats failed when using Dateutil in Python

蹲街弑〆低调 提交于 2020-01-14 03:08:10
问题 I have gone through multiple links before posting this question so please read through and below are the two answers which have solved 90% of my problem: parse multiple dates using dateutil How to parse multiple dates from a block of text in Python (or another language) Problem : I need to parse multiple dates in multiple formats in Python Solution by Above Links : I am able to do so but there are still certain formats which I am not able to do so. Formats which still can't be parsed are: