1

I have this code :

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

But it doesn't take in account the timezone.

How to choose a timezone for choosing zurich as timezone ?

The server is in the usa but the internet time is based in Swiss

Bussiere
  • 500
  • 13
  • 60
  • 119
  • `localtime()` should already take timezone into account, by subtracting `timezone` you're counting it twice. – toti08 Aug 07 '18 at 07:24
  • @toti08 the server is in the usa but the internet time is based in Swiss – Bussiere Aug 07 '18 at 07:53
  • Doesn't the following post answer your question? https://stackoverflow.com/questions/1398674/display-the-time-in-a-different-time-zone – feedMe Aug 07 '18 at 07:58
  • `pytz` I think this covers your issue, along with removing DST, as Swatch time doesn't observe DST. https://stackoverflow.com/questions/48321362/pytz-convert-time-to-utc-without-dst – Rolf of Saxony Aug 07 '18 at 08:06

1 Answers1

2

There are no time zones in Swatch Internet Time; instead, the new time scale of Biel Meantime (BMT) is used, based on Swatch's headquarters in Biel, Switzerland and equivalent to Central European Time, West Africa Time, and UTC+01. ref https://en.wikipedia.org/wiki/Swatch_Internet_Time

But you can achieve this using following steps:

1) - Get the current time in UTC. (Now you dont need to be worried about server location and its time)

2) - Convert the that time to Zurich time, (Zurich time is UTC+2).

3) - Convert Zurich time to datetime.timetuple().

Previously, datetime.localtime was returning you the timetuple object of local time.

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()

Hope this helps :)

kernel
  • 49
  • 6