Git如何解析日期字符串?

问题描述 投票:0回答:2

我有一个来自Joda DateTime.toString的日期字符串,其内容如下:

2020-04-03T12:43:55.019-04:00"

如果我这样做

git commit --date="2020-04-03T12:43:55.019-04:00”

然后是git log以检查日期,它返回的Sun Apr 19 12:43:55 2020 -0400在日期部分似乎是错误的。

但是如果我将时区从04:00稍微更改为05:00,Git似乎可以很好地解析日期:

git commit --date="2020-04-03T12:43:55.019-05:00”

我运行git log命令时会给我正确的日期时间:Fri Apr 3 12:43:55 2020 -0500

第一次约会怎么了?为什么Git认为是4月19日开始,而不是4月3日呢?

git date time jodatime
2个回答
1
投票

我认为您发现了一个错误。

[git-commit声称它了解这些格式,但是它试图理解更多。

   The GIT_AUTHOR_DATE, GIT_COMMITTER_DATE environment variables and the --date option
   support the following date formats:

   Git internal format
       It is <unix timestamp> <time zone offset>, where <unix timestamp> is the number
       of seconds since the UNIX epoch.  <time zone offset> is a positive or negative
       offset from UTC. For example CET (which is 1 hour ahead of UTC) is +0100.

   RFC 2822
       The standard email format as described by RFC 2822, for example Thu, 07 Apr
       2005 22:13:13 +0200.

   ISO 8601
       Time and date specified by the ISO 8601 standard, for example
       2005-04-07T22:13:13. The parser accepts a space instead of the T character as
       well.

           Note
           In addition, the date part is accepted in the following formats:
           YYYY.MM.DD, MM/DD/YYYY and DD.MM.YYYY.

2020-04-03T12:43:55.019-04:00是完全有效的ISO 8601 datetime,格式为YYYY-MM-DDThh:mm:ss.sss±hh:mm。它应该工作。例如,这里是Ruby on Rails。

[1] pry(main)> Time.parse("2020-04-03T12:43:55.21-04:00")
=> 2020-04-03 12:43:55 -0400

Git似乎很困惑小数秒,并将其用作月份。是的,尤其是-04:00时区似乎也有一些变化。

  • [git commit --date='2020-04-03T12:43:55.021-04:00'-> Tue Apr 21 12:43:55 2020 -0700(带小数秒,0400偏移,不正确)]
  • [git commit --date='2020-04-03T12:43:55-04:00'->星期五4月3日12:43:55 2020 -0400(无小数秒,0400偏移,正确)]
  • [git commit --date='2020-04-03T12:43:55.021-05:00'->星期五4月3日12:43:55 2020 -0500(分数秒,0500偏移,正确)]
  • [git commit --date='2020-04-03T12:43:22.023-04:00'-> 2020年4月23日,星期四12:43:22 -07-07(派系秒,不正确)
  • [git commit --date='2020-04-03T12:43:22.024-04:00'->星期五4月3日12:43:22 2020 -0400(正确)

我的猜测是,Git的临时解析将4月21日读为“ 21-04”。例如。

  • git commit --date='12:43:22 23-04'-> 2020年4月23日星期四12:43:22 2020-0700

为什么它在4月23日终止,我不知道。

由于没有24小时,所以可能会在23点截止。

该错误可能在match_multi_number in date.c中。


-1
投票

它的解析是临时的,它当然可以更好地处理此输入(或只是拒绝它),但是您的时代是不正确的。 TZ偏移量没有冒号。

git commit --date=2020-04-03T12:43:55.019-0400    # <-- `-0400`, not `-04:00`.

作品。

编辑:我看到时间戳的ISO规则允许:位于RFC 822规则不允许的偏移量中,这可能是造成混淆的原因。

尽管如此,我还是无法为此做足够的工作来制作补丁。

© www.soinside.com 2019 - 2024. All rights reserved.