Windows Time Synchronization Script

Source: Internet
Author: User

#!/usr/bin/env python
#-*-Coding:utf-8-*-
# Script for Windows Time synchronization, set window Schedule task to execute every five minutes

Import time
Import Ntplib
Import Sys
Import OS


def main ():
Try
Ntp_client = Ntplib. NTPClient ()
Response = ntp_client.request (' s2g.time.edu.cn ')
Ntp_timestamp = Response.tx_time
Ntp_date = Time.strftime ('%y-%m-%d ', Time.localtime (Ntp_timestamp))
Ntp_time = Time.strftime ('%x ', Time.localtime (Ntp_timestamp))
Except
Print U ' cannot get the timestamp above the s2g.time.edu.cn '
Sys.exit ()

"Get Local server timestamp"
Local_timestamp = Time.time ()

"' Get the difference between the values '
diff = ABS (ntp_timestamp-local_timestamp)

"Sync Time"
If diff >= 1:
Os.system (' Date {} && time {} '. Format (ntp_date, ntp_time))

if __name__ = = ' __main__ ':
Main ()

Windows Time Synchronization Script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.