How to determine what date time format the system is using?

孤者浪人 提交于 2019-12-02 11:31:24
Bleeding Fingers

The output I was getting from net time \\nas is in the following format:

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.

The following approach got me what I needed:

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

Code stolen from here(runProcess) and one other place I can't recall.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!