Quantcast

[Bug] git-log prints wrong unixtime with --date=format:%s

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[Bug] git-log prints wrong unixtime with --date=format:%s

Michael Heerdegen
Hello,

the command

   git log --pretty=format:%ad --date=format:%s

displays wrong unixtime values; apparently how much the printed value
differs from the expected value depends on the system's time zone and
whether daylight savings time is enabled or not.

Here is a reproducible recipe compiled by Yuri Khan who helped me
localizing the problem and whom I CC'd:

0. Versions

    $ git --version
    git version 2.8.2

    $ dpkg -l tzdata | tail -1
    ii  tzdata  2016d-0ubuntu0.16.04 all  time zone and
daylight-saving time data

1. Initialize an empty Git repository:

    $ git init test
    $ cd test

2. Make a commit, using the Europe/Berlin time zone:

    $ TZ=Europe/Berlin git commit -m 'test' --allow-empty

3. Examine the timestamp recorded in the commit object:

    $ git cat-file -p HEAD | grep author
    author Yuri Khan <[hidden email]> 1463260938 +0200

4. Check that it corresponds to the current time:

    $ date +%s
    1463260977

5. Try to get the commit date in the unixtime format:

    $ TZ=Europe/Berlin git log --pretty=format:%ad --date=format:%s -1

Expected result: 1463260938 (same as recorded in the commit object).
Observed result: 1463264538 (3600s = one hour ahead).

For lulz, use another time zone:

    $ TZ=Asia/Novosibirsk git log --pretty=format:%ad --date=format:%s -1

Expected result: 1463260938 (unixtime is always UTC and should not
depend on TZ).
Observed result: 1463246538 (-14400s = 4 hours behind).

Not even specifying the UTC time zone helps:

    $ TZ=UTC git log --pretty=format:%ad --date=format:%s -1

Expected result: still 1463260938.
Observed result: 1463268138 (7200s = 2 hours ahead).


FWIW, personally I have not bound the TZ environment variable, my time
zone is constantly that of Berlin, currently CEST.

Many thanks in advance,


Michael.

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [hidden email]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Bug] git-log prints wrong unixtime with --date=format:%s

Michael Heerdegen
Michael Heerdegen <[hidden email]> writes:

> the command
>
>    git log --pretty=format:%ad --date=format:%s
>
> displays wrong unixtime values; apparently how much the printed value
> differs from the expected value depends on the system's time zone and
> whether daylight savings time is enabled or not.

Two more comments:

  - --date=raw seems to work correctly (though the doc tells it would
    also use "%s").

  - When specifying versions like @{N hours}, the result seems to differ
    (by one hour?) from what I expect, too.


Thanks,

Michael.

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [hidden email]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Bug] git-log prints wrong unixtime with --date=format:%s

Jeff King
On Tue, May 17, 2016 at 07:25:31PM +0200, Michael Heerdegen wrote:

> Michael Heerdegen <[hidden email]> writes:
>
> > the command
> >
> >    git log --pretty=format:%ad --date=format:%s
> >
> > displays wrong unixtime values; apparently how much the printed value
> > differs from the expected value depends on the system's time zone and
> > whether daylight savings time is enabled or not.
>
> Two more comments:
>
>   - --date=raw seems to work correctly (though the doc tells it would
>     also use "%s").

I think the doc is misleading there. None of the date formats except the
recently added "--date=format" use strftime at all.

The problem is almost certainly some mismatch between git's idea of the
timezone and what we are feeding to strftime. I'm not sure if it will be
solvable, though. strftime() takes a "struct tm", which has no timezone
field. We feed it a "struct tm" either in the author's timezone or in
the user's timezone (if "format-local" was used). We could feed it in
UTC, but I'm not sure that solves the problem, and it probably makes new
ones.

In your examples, you showed that setting $TZ changes the epoch time
strftime gives us. Which probably means that it is guessing the fed
"struct tm" is in the local timezone, which would still be wrong.

And even if that did work, feeding it UTC means that all of the other
non-%s values would be in UTC, not in the author's or user's timezone.

So we need some way to tell strftime "...and by the way, this is the
timezone for the value we are currently feeding you." There isn't a slot
in "struct tm" for that, but I think maybe you can hack around it by
setting the global "timezone" variable (and then restoring it
afterwards).

>   - When specifying versions like @{N hours}, the result seems to differ
>     (by one hour?) from what I expect, too.

This is probably a totally separate issue, as it would not be using
strftime (or IIRC, any of the standard time functions at all). Do you
have a detailed example that shows the problem?

-Peff
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [hidden email]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Bug] git-log prints wrong unixtime with --date=format:%s

Jeff King
On Tue, May 17, 2016 at 08:40:08PM -0400, Jeff King wrote:

> So we need some way to tell strftime "...and by the way, this is the
> timezone for the value we are currently feeding you." There isn't a slot
> in "struct tm" for that, but I think maybe you can hack around it by
> setting the global "timezone" variable (and then restoring it
> afterwards).

I tried a few obvious things, but couldn't make anything work. Setting
"timezone" manually seems to do nothing. It's supposed to be set by
putting the right thing in $TZ and then calling tzset(). So I tried
munging $TZ to something like "+0200". It did have _some_ effect, but I
couldn't get it to behave correctly in all situations. And using "%z"
and "%Z" always just gives GMT.

So I dunno. Perhaps there is some way to coax sane behavior out of
strftime, but I could not figure it out. Suggestions welcome.

-Peff
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [hidden email]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Bug] git-log prints wrong unixtime with --date=format:%s

Junio C Hamano
Jeff King <[hidden email]> writes:

> I tried a few obvious things, but couldn't make anything work. Setting
> "timezone" manually seems to do nothing. It's supposed to be set by
> putting the right thing in $TZ and then calling tzset(). So I tried
> munging $TZ to something like "+0200". It did have _some_ effect, but I

Wouldn't that be more like "UTC+0200"?

In any case, I do not think anybody wants to do tzset() on each and
every commit while running "git log".  Can we declare "format:<strftime>"
will always use the local timezone, or something?
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [hidden email]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: [Bug] git-log prints wrong unixtime with --date=format:%s

Randall S. Becker
On May 18, 2016 12:22 PM Jeff King wrote:

> > I tried a few obvious things, but couldn't make anything work. Setting
> > "timezone" manually seems to do nothing. It's supposed to be set by
> > putting the right thing in $TZ and then calling tzset(). So I tried
> > munging $TZ to something like "+0200". It did have _some_ effect, but
> > I
>
> Wouldn't that be more like "UTC+0200"?
>
> In any case, I do not think anybody wants to do tzset() on each and every
> commit while running "git log".  Can we declare "format:<strftime>"
> will always use the local timezone, or something?

Off the wall: Dealing in a dispersed team sharing a server that has a
timezone local for only two of the members, git log messes with me also from
a TZ POV. I would like to suggest a more general solution, like configuring
my own TZ in ~/.gitconfig which would potentially allow an override on the
command line. Would user.timezone be helpful in this situation and if set,
call setenv("TZ=...")? It's not an issue when I'm local, but if I touch a
clone on the server, even I get confused around DST changes in October ;).

Cheers,
Randall

-- Brief whoami: NonStop&UNIX developer since approximately
UNIX(421664400)/NonStop(211288444200000000)
-- In my real life, I talk too much.



--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [hidden email]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Bug] git-log prints wrong unixtime with --date=format:%s

Jeff King
In reply to this post by Junio C Hamano
On Wed, May 18, 2016 at 09:21:34AM -0700, Junio C Hamano wrote:

> Jeff King <[hidden email]> writes:
>
> > I tried a few obvious things, but couldn't make anything work. Setting
> > "timezone" manually seems to do nothing. It's supposed to be set by
> > putting the right thing in $TZ and then calling tzset(). So I tried
> > munging $TZ to something like "+0200". It did have _some_ effect, but I
>
> Wouldn't that be more like "UTC+0200"?

Maybe. I tried several different things, and couldn't make it work
sensibly with any of them. My test case is basically:

  git log --format="%ad%n%at" --date=format:'%H:%M %z (%Z)%n%s'

Which should give three lines: the human-readable time and date,
strftime's epoch time, and git's epoch time. And the following
conditions should be met:

  1. The human-readable time is in the author's timezone (or the user's
     if you use format-local).

  2. The zone offset and name in the first line matches the time we
     show.

  3. The epoch times for the latter lines should match.

We currently get (1) right (which is good, because it is IMHO the most
important). I haven't been able to get (2) to change at all, no matter
what I do. And (3) is the subject of debate here; I suspect I could make
it work at the cost of breaking (1) by giving a "struct tm" that is in
UTC.

> In any case, I do not think anybody wants to do tzset() on each and
> every commit while running "git log".  Can we declare "format:<strftime>"
> will always use the local timezone, or something?

I think that is basically the case already. It is in the author's
timezone or in the viewing user's timezone (as reported by localtime()),
and everything seems to work except the %z zone information and the %s
format.

For a raw "%s" date-format, my suggestion is: don't. It is redundant
with --date=raw.

I'd love to make %z work, but I have no idea how.

So I guess we could list those in BUGS in one of the manpages.

-Peff
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [hidden email]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Bug] git-log prints wrong unixtime with --date=format:%s

Jeff King
In reply to this post by Randall S. Becker
On Wed, May 18, 2016 at 12:44:43PM -0400, Randall S. Becker wrote:

> Off the wall: Dealing in a dispersed team sharing a server that has a
> timezone local for only two of the members, git log messes with me also from
> a TZ POV. I would like to suggest a more general solution, like configuring
> my own TZ in ~/.gitconfig which would potentially allow an override on the
> command line. Would user.timezone be helpful in this situation and if set,
> call setenv("TZ=...")? It's not an issue when I'm local, but if I touch a
> clone on the server, even I get confused around DST changes in October ;).

I'm not sure I understand the problem. If you don't like the local
timezone used on the server, why don't you set TZ there? What does
it buy us to introduce a git config option to do the same thing?

-Peff
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [hidden email]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Bug] git-log prints wrong unixtime with --date=format:%s

Michael Heerdegen
In reply to this post by Jeff King
Jeff King <[hidden email]> writes:

> This is probably a totally separate issue, as it would not be using
> strftime (or IIRC, any of the standard time functions at all). Do you
> have a detailed example that shows the problem?

Ok, this was probably false alarm.  Magit (An Emacs Git front end) shows
rounded duration values (rounded hours in my case) in its log views, I
think I just got confused because of this.  In my tests now, the results
were as expected.


Thanks,

Michael.
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [hidden email]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Bug] git-log prints wrong unixtime with --date=format:%s

Michael Heerdegen
In reply to this post by Jeff King
Jeff King <[hidden email]> writes:

>   3. The epoch times for the latter lines should match.

FWIW, I think the epoch time is the most practical way to transfer the
time of a commit into a different program.  When this program wants to
do it's own date arithmetic with it, unix time is just nice (no time
format parsing needed).

That's why it would be good if the unix time values could be relied on.

Conclusion: in addition to 3., I wished both epoch times were also
correct per se.


Regards,

Michael.

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [hidden email]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Bug] git-log prints wrong unixtime with --date=format:%s

Jeff King
On Thu, May 19, 2016 at 01:05:28AM +0200, Michael Heerdegen wrote:

> Jeff King <[hidden email]> writes:
>
> >   3. The epoch times for the latter lines should match.
>
> FWIW, I think the epoch time is the most practical way to transfer the
> time of a commit into a different program.  When this program wants to
> do it's own date arithmetic with it, unix time is just nice (no time
> format parsing needed).
>
> That's why it would be good if the unix time values could be relied on.

Oh, I agree that unix times are handy. I just think that "use %at in the
pretty-format, instead of %ad and then %s in the date-format" is not
such a bad workaround.

> Conclusion: in addition to 3., I wished both epoch times were also
> correct per se.

The %at one should be correct, no? If it's not, that's a separate bug.

-Peff
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [hidden email]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Bug] git-log prints wrong unixtime with --date=format:%s

Michael Heerdegen
Jeff King <[hidden email]> writes:

> Oh, I agree that unix times are handy. I just think that "use %at in the
> pretty-format, instead of %ad and then %s in the date-format" is not
> such a bad workaround.

I had missed %at (and %ct).  Yes, works perfectly - thanks for the hint.


Regards,

Michael.
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [hidden email]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Loading...