{"thread":{"id":"53383","subject":":format:%s for date fields seems to be shifted by timezone","startedAt":"2020-05-03T10:15:28Z","lastAt":"2020-05-06T19:06:53Z","messageCount":4,"participants":["clime","Jeff King"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"396868","messageId":"CAGqZTUu2U6FFXGTXihC64O0gB5Bz_Z3MbD750kMoJWMciAGH6w@mail.gmail.com","threadId":"53383","inReplyTo":null,"subject":":format:%s for date fields seems to be shifted by timezone","fromName":"clime","fromEmail":"clime7@gmail.com","sentAt":"2020-05-03T10:15:14Z","receivedAt":"2020-05-03T10:15:28Z","isPatch":false,"sender":{"key":"clime7@gmail.com","avatar":"https://gravatar.com/avatar/d99ae46a5b0f57415bb74fc6c8ecd7eec304ff769f298c3be00fabf961b215ff?d=mp&s=160"},"body":"Hello!\n\nmy current timezone is UTC+0200.\n\nI create a test repo, add one commit and create a tag:\n\nNow:\n\n$  git for-each-ref --format=\"%(taggerdate:format:%s)\" refs/tags\n1588504146\n\n$ date +\"%s\"\n1588500585\n\n$  git for-each-ref --format=\"%(taggerdate:raw)\" refs/tags\n1588500546 +0200\n\nSomehow %(taggerdate:format:%s) gives a Unix timestamp which is one\nhour in future and it is different than what ` date +\"%s\"` gives\naround approximately the same time the tag was created.\n\nclime\n"},{"id":"396911","messageId":"20200504154343.GA12842@coredump.intra.peff.net","threadId":"53383","inReplyTo":"CAGqZTUu2U6FFXGTXihC64O0gB5Bz_Z3MbD750kMoJWMciAGH6w@mail.gmail.com","subject":"Re: :format:%s for date fields seems to be shifted by timezone","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2020-05-04T15:43:43Z","receivedAt":"2020-05-04T15:43:46Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Sun, May 03, 2020 at 12:15:14PM +0200, clime wrote:\n\n> my current timezone is UTC+0200.\n> \n> I create a test repo, add one commit and create a tag:\n> \n> Now:\n> \n> $  git for-each-ref --format=\"%(taggerdate:format:%s)\" refs/tags\n> 1588504146\n> \n> $ date +\"%s\"\n> 1588500585\n> \n> $  git for-each-ref --format=\"%(taggerdate:raw)\" refs/tags\n> 1588500546 +0200\n> \n> Somehow %(taggerdate:format:%s) gives a Unix timestamp which is one\n> hour in future and it is different than what ` date +\"%s\"` gives\n> around approximately the same time the tag was created.\n\nIt's caused by strftime() being clever with DST. Try this:\n\n  git commit --date=@1559361600 --allow-empty -m summer\n  git commit --date=@1577854800 --allow-empty -m winter\n  git log --format=%ad --date=unix >unix\n  git log --format=%ad --date=format:%s >strftime\n  diff -u unix strftime\n\nWe get the winter time right, but the summer time wrong.\n\nThe issue is that strftime() takes a broken-down \"struct tm\", not a unix\ntime_t. We have all of the right values for hour/minute/etc there, so\nusing \"format:%H:%M:%S\" prints what you'd expect. But we never set the\n\"isdst\" field, so when it tries to convert back to unix time, it applies\na one-hour offset (if it's \"summer\" in your local timezone).\n\nUnfortunately I don't think we can solve this easily. If we were\noperating completely in your local timezone, then we would have gotten\nthat \"struct tm\" from localtime(), and its isdst field would be set\nproperly. And indeed, if you use \"--date=format-local:%s\", the problem\ngoes away.\n\nBut when we're formatting in the original author's timezone, which is\nthe default, we have no idea if they were in dst then or not. We only\nknow their offset-to-gmt, so we munge the time_t ourselves and use\ngmtime().\n\nSo there are a few reasons I think this is the best we can do:\n\n - the full timezone information literally isn't there in Git; we might\n   know the author was in +0200, but we don't know if they were\n   observing DST, or if they were simply in a timezone further east.\n\n - even if we had a zone, there's no system function to convert a time_t\n   to a tm in an arbitrary timezone (hence the gmtime() hack above;\n   we've tried playing games with $TZ and tzset(), but it's awkward and\n   unportable)\n\n - likewise, strftime() is doing the reverse conversion using the local\n   timezone anyway, which would be wrong.\n\nSo my advice is not to use \"%s\" (which isn't portable anyway). Use\n\"--date=unix\" or \"%(taggerdate:unix)\".\n\n-Peff\n"},{"id":"397222","messageId":"CAGqZTUs+xCPjQdeQVfU6EWARmZdjcgcHeSNxkKaTf316rX+8pg@mail.gmail.com","threadId":"53383","inReplyTo":"20200504154343.GA12842@coredump.intra.peff.net","subject":"Re: :format:%s for date fields seems to be shifted by timezone","fromName":"clime","fromEmail":"clime7@gmail.com","sentAt":"2020-05-06T18:03:52Z","receivedAt":"2020-05-06T18:04:06Z","isPatch":false,"sender":{"key":"clime7@gmail.com","avatar":"https://gravatar.com/avatar/d99ae46a5b0f57415bb74fc6c8ecd7eec304ff769f298c3be00fabf961b215ff?d=mp&s=160"},"body":"On Mon, 4 May 2020 at 17:43, Jeff King <peff@peff.net> wrote:\n>\n> On Sun, May 03, 2020 at 12:15:14PM +0200, clime wrote:\n>\n> > my current timezone is UTC+0200.\n> >\n> > I create a test repo, add one commit and create a tag:\n> >\n> > Now:\n> >\n> > $  git for-each-ref --format=\"%(taggerdate:format:%s)\" refs/tags\n> > 1588504146\n> >\n> > $ date +\"%s\"\n> > 1588500585\n> >\n> > $  git for-each-ref --format=\"%(taggerdate:raw)\" refs/tags\n> > 1588500546 +0200\n> >\n> > Somehow %(taggerdate:format:%s) gives a Unix timestamp which is one\n> > hour in future and it is different than what ` date +\"%s\"` gives\n> > around approximately the same time the tag was created.\n>\n> It's caused by strftime() being clever with DST. Try this:\n>\n>   git commit --date=@1559361600 --allow-empty -m summer\n>   git commit --date=@1577854800 --allow-empty -m winter\n>   git log --format=%ad --date=unix >unix\n>   git log --format=%ad --date=format:%s >strftime\n>   diff -u unix strftime\n>\n> We get the winter time right, but the summer time wrong.\n>\n> The issue is that strftime() takes a broken-down \"struct tm\", not a unix\n> time_t. We have all of the right values for hour/minute/etc there, so\n> using \"format:%H:%M:%S\" prints what you'd expect. But we never set the\n> \"isdst\" field, so when it tries to convert back to unix time, it applies\n> a one-hour offset (if it's \"summer\" in your local timezone).\n>\n> Unfortunately I don't think we can solve this easily. If we were\n> operating completely in your local timezone, then we would have gotten\n> that \"struct tm\" from localtime(), and its isdst field would be set\n> properly. And indeed, if you use \"--date=format-local:%s\", the problem\n> goes away.\n>\n> But when we're formatting in the original author's timezone, which is\n> the default, we have no idea if they were in dst then or not. We only\n> know their offset-to-gmt, so we munge the time_t ourselves and use\n> gmtime().\n>\n> So there are a few reasons I think this is the best we can do:\n>\n>  - the full timezone information literally isn't there in Git; we might\n>    know the author was in +0200, but we don't know if they were\n>    observing DST, or if they were simply in a timezone further east.\n>\n>  - even if we had a zone, there's no system function to convert a time_t\n>    to a tm in an arbitrary timezone (hence the gmtime() hack above;\n>    we've tried playing games with $TZ and tzset(), but it's awkward and\n>    unportable)\n>\n>  - likewise, strftime() is doing the reverse conversion using the local\n>    timezone anyway, which would be wrong.\n>\n> So my advice is not to use \"%s\" (which isn't portable anyway). Use\n> \"--date=unix\" or \"%(taggerdate:unix)\".\n\nHello Jeff,\n\nwhat about just printing the raw timestamp from either commit or tag,\ni.e. avoiding any conversion for format:%s?\n\nBest regards!\nclime\n\n>\n> -Peff\n"},{"id":"397230","messageId":"20200506190651.GA2421358@coredump.intra.peff.net","threadId":"53383","inReplyTo":"CAGqZTUs+xCPjQdeQVfU6EWARmZdjcgcHeSNxkKaTf316rX+8pg@mail.gmail.com","subject":"Re: :format:%s for date fields seems to be shifted by timezone","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2020-05-06T19:06:51Z","receivedAt":"2020-05-06T19:06:53Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, May 06, 2020 at 08:03:52PM +0200, clime wrote:\n\n> what about just printing the raw timestamp from either commit or tag,\n> i.e. avoiding any conversion for format:%s?\n\nWe don't parse the \"%s\" ourselves at all. We feed everything after the\ncolon to the system strftime(), and that function only takes a\nbroken-down \"struct tm\".\n\nThat said, we do already intercept \"%z\" for similar reasons in\nstrbuf_addftime(). So it would probably be possible to declare that we\ndon't respect system \"%s\" and just handle it ourselves. It would require\neither massaging the \"tm\" back to a time_t, or changing the signature to\ntake a redundant time_t (and not all callers might have that).\n\nSo if somebody wants to work on a patch in that direction, we could see\nhow ugly it ends up being. But I am reasonably happy with \"don't do\nthat\" as a solution in the meantime.\n\n-Peff\n"}]}