Incorrect age for commits made in timezone different from server's

Issue #213 resolved
DmitriMK created an issue

When a commit is done on a computer with a different timezone from that of the rhodecode server its age shown in the rhodecode pages is wrong. For example, in my case, the commits were done on my PC, which is currently in the MDT timezone (US/Mountain DST), ie -0600. The server where rhodecode is running is set up in the GMT zone. The ages for the commits from my PC are 6 hours higher as a result.

I am not sure but the problem could mostly be in the vcs code. Its vcs.backends.hg.MercurialChangeset class date function: {{{ @LazyProperty def date(self): return date_fromtimestamp(self._ctx.date()) }}} ...returns the "naive" datetime object containing the local time of the commit. In other words, it loses the timezone offset information. Here is date_fromtimestamp function from vcs.utils that the above calls: {{{ def date_fromtimestamp(unixts, tzoffset=0): """ Makes a datetime objec out of unix timestamp with given timezone offset :param unixts: :param tzoffset: """ return datetime.datetime(time.gmtime(float(unixts) - tzoffset)[:6]) }}}

My "quick-n-dirty" workaround was to change the above function to not subtract the tzoffset. Thus, it now returns the the GMT datetime. I've also changed the rhodecode.lib.age function to use datetime.utcnow instead of datetime.now. The ages are now computed correctly but a (possible) downside is that the times shown are GMT.

I believe the proper fix is to use timezone-aware datetimes (both in vcs and in the rhodecode's age function).

Comments (4)

  1. Marcin Kuzminski repo owner

    this is something tricky... I think changing age function to use utcnow requires that all times are GMT, Mercurial internally uses unix timestamps, which are utc, and additionally gives the offset. I think utc times are safe, but some users might be not happy with it, need to rethink that one

  2. DmitriMK reporter

    Agreed, I would want to see the timezone offset preserved too. Using GMT was just the quickest fix.

    I think the proper fix is to make the vcs.backends.hg.MercurialChangeset class date function return a timezone-aware datetime (also, I assume the equivalent Git backend code would have do the same if it does not already). And, secondly, the rhodecode.lib age function would have to use the datetime.now() with the local timezone specified (or use some other way to get the current time into a timezone-aware datetime).

  3. Marcin Kuzminski repo owner
    • changed status to open

    My proposed solution is to change VCS date_fromtimestamp method to return localtime from timestamp using datetime.datetime.fromtimestamp,.

  4. Log in to comment