我得到的输出net time \\nas
格式如下:
Current time at \\nas is dd/mm/yyyy HH:MM:SS
Local time (GMT) at \\nas is dd/mm/yyyy HH:MM:SS
The command completed successfully.
以下方法让我得到了我需要的东西:
import subprocess
import time
import datetime
from _winreg import *
from dateutil.parser import parse
def runProcess(exe):
p = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while(True):
retcode = p.poll() #returns None while subprocess is running
line = p.stdout.readline()
yield line
if(retcode is not None):
break
def nasTime():
for line in runProcess(r"net time \\nas".split()):
timeLine = line
break
timeLine = timeLine.split("is ")[1].replace("\r\n", "")
hKey = OpenKey (HKEY_CURRENT_USER, r"Control Panel\International")
value, type = QueryValueEx (hKey, "sShortDate")
dayFirst = str(value).lower().startswith("d") # tells if day first format was being used
return time.mktime(parse(timeLine, dayfirst = dayFirst).timetuple())
从这里( runProcess
) 和另一个我不记得的地方窃取的代码。