Windows而非Linux中的计时问题

时间:2018-07-16 16:41:33

标签: python-3.x

我有一个以前在公司工作过的同事的以下功能,注释是不言自明的,问题是我现在正在使用Windows,并且设备的同步性存在问题。

有人会解决或知道Windows中与设备同步的解决方案吗?

def sync_time(self):
        """Sync time on SmartScan."""
        # On a SmartScan time can be set only by the precision of seconds
        # So we need to wait for the next full second until we can send
        # the packet on it's way to the scanner.
        # It's not perfect, but the error should be more or less constant.
        message = Maint()
        message.state = message.OP_NO_CHANGE

        now = datetime.datetime.utcnow()
        epoch = datetime.datetime(1970, 1, 1)

        # int and datetime objects
        seconds = int((now - epoch).total_seconds()) + 1  # + sync second
        utctime = datetime.datetime.utcfromtimestamp(seconds)

        # wait until next full second
        # works only on Linux with good accuracy
        # Windows needs another approach
        time.sleep((utctime - datetime.datetime.utcnow()).total_seconds())

        command = MaintRfc()
        command.command = command.SET_CLOCK
        command.data = (seconds, )
        message.add_message(command)

        self._handler.sendto(message)
        LOG.debug("Time set to: %d = %s", seconds, utctime)

0 个答案:

没有答案