Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. def sync_time(self):
  2. """Sync time on SmartScan."""
  3. # On a SmartScan time can be set only by the precision of seconds
  4. # So we need to wait for the next full second until we can send
  5. # the packet on it's way to the scanner.
  6. # It's not perfect, but the error should be more or less constant.
  7. message = Maint()
  8. message.state = message.OP_NO_CHANGE
  9.  
  10. now = datetime.datetime.utcnow()
  11. epoch = datetime.datetime(1970, 1, 1)
  12.  
  13. # int and datetime objects
  14. seconds = int((now - epoch).total_seconds()) + 1 # + sync second
  15. utctime = datetime.datetime.utcfromtimestamp(seconds)
  16.  
  17. # wait until next full second
  18. # works only on Linux with good accuracy
  19. # Windows needs another approach
  20. time.sleep((utctime - datetime.datetime.utcnow()).total_seconds())
  21.  
  22. command = MaintRfc()
  23. command.command = command.SET_CLOCK
  24. command.data = (seconds, )
  25. message.add_message(command)
  26.  
  27. self._handler.sendto(message)
  28. LOG.debug("Time set to: %d = %s", seconds, utctime)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement