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