Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

时区识别问题 #116

Open
TripleEarth opened this issue Sep 28, 2023 · 7 comments
Open

时区识别问题 #116

TripleEarth opened this issue Sep 28, 2023 · 7 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@TripleEarth
Copy link
Contributor

当前不足

由于时区不同的缘故,很多功能无法使用,比如逢魔之时

解决方案

对时间的判断函数里增加一个时区判断,然后转换成统一时间后进行判断

其他内容

No response

@TripleEarth TripleEarth added the enhancement New feature or request label Sep 28, 2023
@runhey
Copy link
Owner

runhey commented Sep 28, 2023

Alas 对底层的设计更加的优秀,包括说时区、多服务器、多语言、多客户端。
我在复制代码的时候直接把这部分扔掉了,如果需要加上,应该是按照最先的设计更好,不过忘记了那边是如何处理的

@TripleEarth
Copy link
Contributor Author

Alas 对底层的设计更加的优秀,包括说时区、多服务器、多语言、多客户端。 我在复制代码的时候直接把这部分扔掉了,如果需要加上,应该是按照最先的设计更好,不过忘记了那边是如何处理的

我尝试了使用 dateutil 来进行时区转换,可以做到将时间转换成国内时间从而满足代码的时间段判定。但是有个问题在于,如果时间段不符合,比如逢魔之时,会强制将代码的下次运行时间改到17:30之后,但是这个时间就是没有经过转换的。所以我在想,是否可以通过引入一个获取时间的函数,将所有的时间都通过这个函数来进行转换

@runhey
Copy link
Owner

runhey commented Oct 13, 2023

你的理解是正确的,不过我们先看看先驱是怎么设计的,Alas中有

SERVER_TO_LANG = {
    'cn': 'zh-CN',
    'en': 'en-US',
    'jp': 'ja-JP',
    'tw': 'zh-TW',
}
LANG_TO_SERVER = {v: k for k, v in SERVER_TO_LANG.items()}
SERVER_TO_TIMEZONE = {
    'cn': timedelta(hours=8),
    'en': timedelta(hours=-7),
    'jp': timedelta(hours=9),
    'tw': timedelta(hours=8),
}

def server_timezone() -> timedelta:
    return SERVER_TO_TIMEZONE.get(server_.server, SERVER_TO_TIMEZONE['cn'])


def server_time_offset() -> timedelta:
    """
    To convert local time to server time:
        server_time = local_time + server_time_offset()
    To convert server time to local time:
        local_time = server_time - server_time_offset()
    """
    return datetime.now(timezone.utc).astimezone().utcoffset() - server_timezone()

感觉有点绕 server_time_offset() 后面的操作都是再减去时差偏移

@runhey
Copy link
Owner

runhey commented Oct 13, 2023

想法应该是把本地时间映射到北京时间,以及北京时间 -> 本地时间。

@runhey
Copy link
Owner

runhey commented Oct 13, 2023

  def server_timezone() -> timedelta:
        return timedelta(hours=8)


    def server_time_offset() -> timedelta:
        """
        To convert local time to server time:
            server_time = local_time + server_time_offset()
        To convert server time to local time:
            local_time = server_time - server_time_offset()
        """
        return datetime.now(timezone.utc).astimezone().utcoffset() - server_timezone()

    _server_time_offset = server_time_offset()
    
    def server2local(server_time: datetime) -> datetime:
        return server_time + _server_time_offset
    
    def local2server(local_time: datetime) -> datetime:
        return local_time - _server_time_offset

给你参考一下,自己来提一个pr,毕竟只有你一个是倒时差的

@JarediKey
Copy link

+1,现在有两个倒时差的了

@runhey runhey added the help wanted Extra attention is needed label Jan 13, 2024
@zwldarren
Copy link

我也遇到这个时区问题了,这个方案用timedelta可能没法考虑到夏令时带来的变化,可以引入新的库pytz来做时区计算。不过我认为最好还是始终使用utc时间,然后在生成输出的时候再考虑时间转换。

  def server_timezone() -> timedelta:
        return timedelta(hours=8)


    def server_time_offset() -> timedelta:
        """
        To convert local time to server time:
            server_time = local_time + server_time_offset()
        To convert server time to local time:
            local_time = server_time - server_time_offset()
        """
        return datetime.now(timezone.utc).astimezone().utcoffset() - server_timezone()

    _server_time_offset = server_time_offset()
    
    def server2local(server_time: datetime) -> datetime:
        return server_time + _server_time_offset
    
    def local2server(local_time: datetime) -> datetime:
        return local_time - _server_time_offset

给你参考一下,自己来提一个pr,毕竟只有你一个是倒时差的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants