以格式yyyymmddHHMM创建日期序列,在bash中增加12小时(Create sequence of dates in format yyyymmddHHMM with 12 hour incr

编程入门 行业动态 更新时间:2024-10-06 10:28:51
以格式yyyymmddHHMM创建日期序列,在bash中增加12小时(Create sequence of dates in format yyyymmddHHMM with 12 hour increment in bash)

Tl; dr :在bash中以yyyymmddHHMM格式创建一个12小时递增的日期序列是否有很好的方法?

考虑我有一个python脚本,它接受一定的时间(yyyymmddHHMM)作为输入-t,我可以这样运行它,例如

python myscript.py -t 201411140000

然后开始myscript.py为2014-11-14 00:00。 现在我想运行许多日期的脚本,从2014-01-01 00:00到2014-11-14 00:00开始,增量为12小时,即我想生成以下所有行:

python myscript.py -t 201401010000 python myscript.py -t 201401011200 python myscript.py -t 201401020000 . . . python myscript.py -t 201411131200 python myscript.py -t 201411140000

最接近我的是(回声以验证结果而不运行它们):

for mm in {01..10}; do for dd in {01..31}; do for HH in 00 12; do echo python myscript.py -t 2014$mm$dd$HH\00; done; done; done

它产生从1月到10月的所有必需日期,但也有一些荒谬的日期 ,例如201402310000 ,程序然后必须处理(即抛出/记录错误)。 这不是一个大问题,但感觉很脏。 最后,需要另一个循环来处理11月1日至11月14日的缺失日期,这对我来说再次变得肮脏。

我怎样才能更好地创建这些日期 - 或者是否超出了适当的方式?

Tl;dr: Is there a nice way of creating a sequence of dates with 12 hour increment in the format yyyymmddHHMM in the bash?

Consider I have a python-script which accepts a certain time (yyyymmddHHMM) as input -t, i could run it like this for instance

python myscript.py -t 201411140000

which the then starts myscript.pyfor the date 2014-11-14 00:00. Now I want to run the script for many dates, beginning from 2014-01-01 00:00 to 2014-11-14 00:00 with an increment of 12 hours, i.e. i want to produce all the following lines:

python myscript.py -t 201401010000 python myscript.py -t 201401011200 python myscript.py -t 201401020000 . . . python myscript.py -t 201411131200 python myscript.py -t 201411140000

The closest to what I came is (echo to verify results without running them):

for mm in {01..10}; do for dd in {01..31}; do for HH in 00 12; do echo python myscript.py -t 2014$mm$dd$HH\00; done; done; done

It produces all required dates from Jan to Oct, but also some absurd dates, such as 201402310000, which the program then has to handle (i.e. throw/log errors). That is not a huge issue, but it feels dirty. In the end, another loop is required to handle the missing dates Nov-01 to Nov-14, which, again, seems dirty to me.

How can I create those dates more nicely - or is the way above the appropriate way of doing so?

最满意答案

您可以将开始日期转换为UNIX时间戳,并以43200秒(12小时)为增量迭代该范围。

for ((ts=$(date +%s --date "2014-11-01 0000"); ts <= $(date +%s --date "2014-11-14 0000"); ts+=12*3600)); do python myscript.py -t $(date +%Y%m%d%H%M --date @$ts) done

You can convert the start date to a UNIX timestamp, and iterate over the range in 43200-second (12-hour) increments.

for ((ts=$(date +%s --date "2014-11-01 0000"); ts <= $(date +%s --date "2014-11-14 0000"); ts+=12*3600)); do python myscript.py -t $(date +%Y%m%d%H%M --date @$ts) done

更多推荐

dates,-t,python,日期,电脑培训,计算机培训,IT培训"/> <meta name="descriptio

本文发布于:2023-07-09 10:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085546.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列   创建日期   小时   格式   hour

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!