timvink / mkdocs-git-revision-date-localized-plugin

MkDocs plugin to add a last updated date to your site pages
https://timvink.github.io/mkdocs-git-revision-date-localized-plugin/index.html
MIT License
206 stars 44 forks source link

我在使用过程中,有时区问题 #35

Closed flc1125 closed 4 years ago

flc1125 commented 4 years ago

Question

我在使用过程中,有时区问题 I have time zone problems during use(from Google Translate)


我来自中国,Github 的时区是 +0 时区,但是中国应该是 +8。 I’m from China. Github’s time zone is +0, but China should be +8.(from Google Translate)


示例:

timvink commented 4 years ago

Hi @flc1125,

Thanks for taking the time to investigate and report. Much appreciated!

All timestamps are currently being reported in UTC (+0) format. That's not sufficiently clear from the documentation (yet). Timezones should be supported by the plugin.

Technical background

We're retrieving the unix timestamp from GIT:

https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/blob/dd82961c025c33830f5e466cd05d626e5a5aca3f/mkdocs_git_revision_date_localized_plugin/util.py#L89

(Note that %at retrieves the "author date, UNIX timestamp" reference

The UNIX timestamp is timezone independant as it's "the number of seconds (or milliseconds) elapsed since an absolute point in time, midnight of Jan 1 1970 in UTC time".

When formatting time (for type="datetime"), there's no timezone conversion:

https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/blob/dd82961c025c33830f5e466cd05d626e5a5aca3f/mkdocs_git_revision_date_localized_plugin/util.py#L60 https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/blob/dd82961c025c33830f5e466cd05d626e5a5aca3f/mkdocs_git_revision_date_localized_plugin/util.py#L67

Solution

The solution would be to add timezone support for displaying time. The locale and timezone are only very loosely coupled: you might have English sites in China en vice versa. So this would have to be a separate, additional option of the plugin.

This package usesbabel for localization, which already supports timezones, so an implementation would be fairly straightforward and alone the lines of:

from babel.dates import format_date, format_time, get_timezone
tzinfo = get_timezone("Asia/Shanghai")
format_time(revision_date, format="medium", locale = "zh", tzinfo=tzinfo)

The timeago plugin also accepts an ISO 8601 string where you specify the timezone as well.

I'll spend some time on this somewhere this week and release a new version with timezone support!

timvink commented 4 years ago

I just released v0.6 adding support for timezones, which should address this issue.

pip install --upgrade mkdocs-git-revision-date-localized-plugin

# mkdocs.yml
plugins:
  - git-revision-date-localized:
      type: datetime
      locale: zh
      time_zone: Asia/Shanghai

Let me know what you think?

flc1125 commented 4 years ago

你好,我已经升级成功,已经正常。

非常棒,太高效了,非常非常感谢你。:+1::+1: