如何确定系统使用的日期时间格式? [英] How to determine what date time format the system is using?

查看:77
本文介绍了如何确定系统使用的日期时间格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于集中日志记录的目的,该应用程序将通过网络使用(尚不涉及服务器端应用程序),我还需要存储发生特定事件的时间。这项任务非常简单,但正如我所注意到的,问题是网络上的计算机的时间设置不准确。因此,为了使时间标准化,我选择使用 净时间\\nas 。到目前为止,一切都很好。

For my centralized logging purposes for an application that will be used over the network(no server side application involved, yet) I am required to also store the time at which a particular event occurs. The task is pretty much easy but the problem is, as I have noticed, the computers over the network don't have their time setup accurately. Therefore in order to standardize the time I choose to grab it from the NAS using net time \\nas. All's good to this point.

现在的问题是,净时间返回时间的格式取决于在运行应用程序的特定系统的日期时间格式上进行更改(保证是一致的),因此呈现硬编码的日期格式转换时间戳没用。有解决方案和意见吗?

Now the problem is, the format in which net time returns the time is dependent on the the particular system's datetime format, on which the application is run, which is guaranteed to be consistent, and therefore rendering a hard coded dateformat conversion to timestamp useless. Any solutions and opinions?

不能使用解决方案

推荐答案

我从净时间获得的输出\\ 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.

以下方法使我得到了所需的东西:

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

在这里 runProcess )和一个我不记得的地方

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

这篇关于如何确定系统使用的日期时间格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆