如何使用 cygwin date 返回过去,返回具有静态秒值的日期
我正在使用 git,尝试同步到去年每个月的第一次提交。我希望能够逐月回顾,在每个月的第一天,在每个月的第一秒。或者至少来自某个恒定的“秒”值。到目前为止,我有这个:
$(git rev-list --before "$(date -d "$(date +%Y-%m-01) -$i Months" +%Y-%m)- 01" -n 01 HEAD)
这显然不包括恒定的秒值。就目前情况而言,运行此脚本并在一小时后再次运行它会返回两个不同的 sha1,因为它距离我运行该脚本的确切时间要回溯 x 个月。我希望无论何时运行此脚本,返回的 sha1 都是相同的。这有道理吗?有什么想法吗?
I am using git, trying to sync to the first commits of each month for the last year. I want to be able to go back month by month, on the first day of the month, ON THE FIRST SECOND OF THE MONTH. or at least from some constant "seconds" value. So far I have this:
$(git rev-list --before "$(date -d "$(date +%Y-%m-01) -$i months" +%Y-%m)-01" -n 01 HEAD)
This obviously does not include a constant seconds value. As it stands, running this script and then running it again an hour later returns two different sha1's because it is going back x months FROM THE EXACT TIME at which I run the script. I want the returned sha1's to be the same no matter when I run this script. Does that make sense? Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您的命令中有一个拼写错误。它必须是
--before="$(...
,您缺少=
。date
的行为不是问题这里date +%Y-%m-01
将返回类似2011-10-01
的字符串,不包含其他时间信息,因此第二个日期调用。将减少$i
月数并且还将返回类似于2011-09-01
的格式的字符串,除了该字符串之外,不会将任何其他信息作为参数的值传递给
。git-rev-list
。 >--before=使用
git-rev-list
时必须考虑一些事项:HEAD
时,您始终引用 <例如,当您当前分支时。检查您的 HEAD 将更改的获得的提交 ID 之一,您可能想使用master
或任何其他分支名称作为参考。Git--before
或--after
参数时,这将导致输出混乱,因为它们依赖于提交者字段的时间戳。考虑到上述所有内容,以下命令对我有用:
$(git rev-list --after="$(date -d "$(date +%Y-%m-01) -$i Months" + %Y-%m)-01 00:00:00" master | tail -n 1)
这将返回给定月份第一天之后第一次提交的 ID。 (此提交不一定是在该月内进行的,也许该月没有任何提交。)
First of all, you have a typo in your command. It has to be
--before="$(...
, you are missing the=
.The behaviour of
date
is not the problem here.date +%Y-%m-01
will return something like2011-10-01
as a string, there's no additional time information included. So the second date call will decrease it by the$i
number of months and will also return a sting of the format like2011-09-01
. Besides that string no additional information is passed togit-rev-list
as value of argument--before=
.There are some things you have to consider using
git-rev-list
:HEAD
you're always referring to the current branch. So when you for example checkout one of the obtained commit-IDs your HEAD will change. You maybe want to usemaster
or any other branch name as reference instead.--before
or--after
arguments since they rely on the timestamp of the committer field.Considering all the said, the following command works for me:
$(git rev-list --after="$(date -d "$(date +%Y-%m-01) -$i months" +%Y-%m)-01 00:00:00" master | tail -n 1)
This will return the ID of the first commit after the first of the given month. (This commit was not necessarily made during that month, maybe there were no commits in that month anyway.)