【问题标题】:Swatch Internet time in python在 python 中色板互联网时间
【发布时间】:2019-01-14 04:53:20
【问题描述】:

我有这个代码:

from time import localtime, timezone


def itime():
    """Calculate and return Swatch Internet Time

    :returns: No. of beats (Swatch Internet Time)
    :rtype: float
    """

    h, m, s = localtime()[3:6]
    beats = ((h * 3600) + (m * 60) + s + timezone) / 86.4

    if beats > 1000:
        beats -= 1000
    elif beats < 0:
        beats += 1000

    return beats

但它没有考虑时区。

选择zurich作为时区如何选择时区?

服务器在美国,但互联网时间以瑞士为基准

【问题讨论】:

标签: python timezone


【解决方案1】:

Swatch Internet Time 中没有时区;取而代之的是,使用了新的 Biel Meantime (BMT) 时标,基于 Swatch 位于瑞士 Biel 的总部,相当于中欧时间、西非时间和 UTC+01。参考https://en.wikipedia.org/wiki/Swatch_Internet_Time

但您可以使用以下步骤实现此目的:

1) - 以 UTC 获取当前时间。 (现在您不必担心服务器位置和时间)

2) - 将该时间转换为苏黎世时间,(苏黎世时间为UTC+2)。

3) - 将苏黎世时间转换为datetime.timetuple()

之前,datetime.localtime 向您返回当地时间的 timetuple 对象。

from datetime import datetime
from dateutil import tz

def itime():
    """Calculate and return Swatch Internet Time

    :returns: No. of beats (Swatch Internet Time)
    :rtype: float
    """
    from_zone = tz.gettz('UTC')
    to_zone = tz.gettz('Europe/Zurich')
    time = datetime.utcnow()
    utc_time = time.replace(tzinfo=from_zone)
    zurich_time = utc_time.astimezone(to_zone)

    h, m, s = zurich_time.timetuple()[3:6]

    beats = ((h * 3600) + (m * 60) + s) / 86.4

    if beats > 1000:
        beats -= 1000
    elif beats < 0:
        beats += 1000

    return beats

print itime()

希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 2010-11-13
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    相关资源
    最近更新 更多