以各种格式生成日期列表

timerange的Python项目详细描述


安装和使用时间范围

timerange是一个命令行脚本,用于在 各种格式。 它用于生成日期/时间列表,然后可以传递给其他 程序或在shell/batch脚本中使用,其中包含日期和时间的操作 是必需的

它也可以为windows编译(使用py2exe)。

安装说明

timerange是一个单独的python脚本,除了 标准的python库。通常从源安装:

python setup.py install

在windows下,py2exe工具用于生成单个exe二进制文件:

python setup.py py2exe

生成的timerange.exe位于dist/文件夹中。

用法

获取有关命令行选项的帮助:

C:\> timerange --help
usage: timerange [-h] [-d DEBUG_LEVEL] [-s START] [-e END] [-p PERIOD]
                 [-o OUTPUT_FORMAT] [-i INPUT_FORMAT] [-fh] [-v]

timerange - generates a range of date/times in various formats

optional arguments:
  -h, --help            show this help message and exit
  -d DEBUG_LEVEL, --debug DEBUG_LEVEL
                        Sets the debug level and adds debug messages to the
                        output
  -s START, --start START
                        Date to start from (format "YYYY-MM-DD", "today",
                        "+7days" or "-2hours")
  -e END, --end END     End date (format "YYYY-MM-DD", "today", "+7days" or
                        "-2days")
  -p PERIOD, --period PERIOD
                        Period term - examples: "1 hour", "-2 days", "3
                        months, 5 days"
  -o OUTPUT_FORMAT, --output-format OUTPUT_FORMAT
                        Output format string. Use --format-help option for
                        more details
  -i INPUT_FORMAT, --input-format INPUT_FORMAT
                        Input format string. Use --format-help option for more
                        details
  -fh, --format_help    Show the format strings available for date/time input
                        and output
  -v, --version         Show the version

author: Todor Bukov <dev.todor@gmail.com> ver.1.06

获取有关输入和输出格式的帮助:

Available arguments for the date/time format options (-i/-o)

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

Notes:

(1) When used with the strptime() function, the %p directive only affects the output hour field if the %I directive is used to parse the hour.
(2) The range really is 0 to 61; this accounts for leap seconds and the (very rare) double leap seconds.
(3) When used with the strptime() function, %U and %W are only used in calculations when the day of the week and the year are specified.

Examples:
"%Y%-m-%d" results in something like "2012-01-01" (the actual date depends on the other parameters).

Dates can also be defined as relative to the current date/time using sign i.e. "+" and "-" or by using the keywords "from now" and "ago".

List of relative keywords:
- "now" - refers to the current time on the system
- "today" - same as "now", but with the time part zeroed (i.e. 00:00:00)
- "yesterday" - same as "now, -1 day"
- "tomorrow" - same as "now, +1 day"
- "ago" - refers to the period in the past e.g. "1 day ago" is the same as "-1 day"
- "from now" - refers to the period in the future (this is the default) e.g. "1 day", "+1 day", "1 day from now" are all the same.

Examples:
"1 day ago", "2 weeks from now", "-1 hour", "+2 minutes", "today", "tomorrow"

When using relative dates more than one keyword can be provided, seprated by comma.
Examples:
"1 day, 2 hours, 3 minutes" or "-3 days, -2 seconds"

NOTE: Combinations of keywords can sometimes lead to confusing results i.e. "+3days, -1 hour, 2 minutes ago".
These combinations are additive and the final results is the sum of the results yelded by the individual keywords.

显示今天的日期:

C:\> timerange
2012-01-29 00:00:00

C:\> timerange.exe -s "now" -e "now, 1 sec"
2012-01-29 11:21:10

生成具有指定期间的(相对)日期列表 命令行选项-p/–句点:

C:\> timerange -s "now" -e "tomorrow" -p "6 hours"
2012-01-29 11:23:04
2012-01-29 17:23:04
2012-01-29 23:23:04
2012-01-30 05:23:04
2012-01-30 11:23:04

相对日期可以与逗号分隔的标记组合在一起:

C:\> timerange -s "-3 days, -2 hours" -e "tomorrow, +1 day" -p "22 hours"
2012-01-26 09:24:06
2012-01-27 07:24:06
2012-01-28 05:24:06
2012-01-29 03:24:06
2012-01-30 01:24:06
2012-01-30 23:24:06

生成两个(绝对)日期之间的日期范围:

C:\> timerange -s  2012-01-01 -e 2013-01-01 -p "2 months, 2 days"
ERROR: Could not convert the argument: '2012-01-01' using the format '%Y-%m-%d %H:%M:%S (time data '2012-01-01' does not match format '%Y-%m-%d %H:%M:%S')

错误是由于日期的格式不是 脚本可以使用-i/–input format选项更改输入格式 (检查-fh命令选项的输出,了解格式的含义 字母):

C:\> timerange -s  2012-01-01 -i "%Y-%m-%d" -e 2013-01-01 -p "2 months, 2 days"
2012-01-01 00:00:00
2012-03-03 00:00:00
2012-05-04 00:00:00
2012-07-05 00:00:00
2012-09-05 00:00:00
2012-11-06 00:00:00

也可以使用-o/–output format选项更改输出格式:

C:\> timerange -s  2012-01-01 -i "%Y-%m-%d" -e 2013-01-01 -p "2 months, 2 days" -o "%b %d, %Y"
Jan 01, 2012
Mar 03, 2012
May 04, 2012
Jul 05, 2012
Sep 05, 2012
Nov 06, 2012

如果期间为负数,则日期也可以按降序排列:

C:\> timerange.exe -s "tomorrow" -e "-3 days" -p "-12 hours, -5 minute, -30 secs"
2012-01-30 11:26:10
2012-01-29 23:20:40
2012-01-29 11:15:10
2012-01-28 23:09:40
2012-01-28 11:04:10
2012-01-27 22:58:40
2012-01-27 10:53:10
2012-01-26 22:47:40

如果开始/结束日期和周期可能导致无限循环, 然后将抛出一个错误:

C:\> timerange.exe -s "tomorrow" -e "yesterday" -p "12 hours"
ERROR: The expression will never reach the final date with the given period

将句点设为负值将使上述表达式的工作方式为 需要:

C:\> timerange.exe -s "tomorrow" -e "yesterday" -p "-12 hours"
2012-01-30 11:29:04
2012-01-29 23:29:04
2012-01-29 11:29:04
2012-01-28 23:29:04
2012-01-28 11:29:04

开发和错误报告

脚本的最新版本可以从timerange website获得。 请向作者报告任何错误或改进建议。

许可证

该脚本在GNU通用公共许可版本3或更高版本下获得许可 (GPLv3)。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java刷新系统。用自己的记录器输出   使用Jython将参数从Java传递到Python   JavaSocksV4代理   java如何使用通知?   java@DirtiesConext不工作   java将多个jar组合成一个(使用maven)   java使用相等运算符比较两个类   java我怎样才能让两个JOptionPane一起出现在我的第一页上,并让它们在两个页面上都有正确的答案?   html无法访问java中资产文件夹内的文件   通过post命令向SpringWebApp发送对象时,java对象字段为null   单个实例中静态变量的类更改值(Java)   java解决方案是什么   试图检查网站所有受支持的密码套件的安全性,在java中遇到chacha20和poly1035问题