Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Git parse date string?

I have a date string from Joda DateTime.toString that reads like below:

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

If I do

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

and then git log to check out the date, it's returning Sun Apr 19 12:43:55 2020 -0400 which seems wrong on the date part.

But if I change the time zone slightly from 04:00 to 05:00, Git seems to be parsing the date fine:

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

would give me the correct date time when I run git log command: Fri Apr 3 12:43:55 2020 -0500

What is wrong with the first date? Why does Git think it's from Apr 19 but not Apr 3?

like image 720
fcorange Avatar asked Apr 17 '26 16:04

fcorange


1 Answers

I think you found a bug.

git-commit claims it understands these formats, but it tries to understand a lot more.

   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 is a perfectly valid ISO 8601 datetime in the format YYYY-MM-DDThh:mm:ss.sss±hh:mm. It should work. For example, here's Ruby on Rails.

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

Git appears to be confused the fractional seconds and uses them as the month day. And yes, there also seems to be something about the -04:00 time zone in particular.

  • git commit --date='2020-04-03T12:43:55.021-04:00' -> Tue Apr 21 12:43:55 2020 -0700 (with fractional seconds, 0400 offset, incorrect)
  • git commit --date='2020-04-03T12:43:55-04:00' -> Fri Apr 3 12:43:55 2020 -0400 (no fractional seconds, 0400 offset, correct)
  • git commit --date='2020-04-03T12:43:55.021-05:00' -> Fri Apr 3 12:43:55 2020 -0500 (fractional seconds, 0500 offset, correct)
  • git commit --date='2020-04-03T12:43:22.023-04:00' -> Thu Apr 23 12:43:22 2020 -0700 (factional seconds, incorrect)
  • git commit --date='2020-04-03T12:43:22.024-04:00' -> Fri Apr 3 12:43:22 2020 -0400 (correct)

My guess is something in Git's ad hoc parsing is reading "21-04" as the date April 21st. For example.

  • git commit --date='12:43:22 23-04' -> Thu Apr 23 12:43:22 2020 -0700

Why it cuts off at April 23rd I have no idea.

It likely cuts off at 23 because there is no hour 24.

The bug is likely in match_multi_number in date.c.

like image 68
Schwern Avatar answered Apr 19 '26 12:04

Schwern



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!