Board logo

标题: [文本处理] [已解决]批处理或者VBS如何把UTC时间转换为本地时间? [打印本页]

作者: Batcher    时间: 2009-4-17 16:31     标题: [已解决]批处理或者VBS如何把UTC时间转换为本地时间?

20090416143849.156000+480
我想把这个UTC时间转换为本地时间。我遇到的问题在于,不知道如何获取本机所处的时区。如果能够用批处理或者VBS获取本机所处的时区(比如北京时间:GMT+8),那就好办了,给UTC时间加上对应的小时数就行了。 我尝试过修改本机时区,并监控注册表,发现系统修改了HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\下面的几个键值,但是我看不出这些数值的改变和时区的改变之间有什么对应关系。也就是说,不知道UTC时间加上几个小时才能得到本机系统时间。 操作系统:WinXP 系统语言:中文、英文等所有可能出现的语言 Google、论坛搜索无果。望各位指点迷津,用BAT或者VBS实现均可,多谢。
作者: zqz0012005    时间: 2009-4-17 17:54

Batcher 还在探究此问题啊,值得学习。
刚好前不久在verybat看到过一个WMI对象与时间有关
  1. strUTCTime = "20090416143849.156000+480"
  2. Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
  3. objSWbemDateTime.Value = strUTCTime
  4. dtmLocalTime = objSWbemDateTime.GetVarDate(true) '参数为true可以自动转换为本地时区
  5. wsh.echo dtmLocalTime
复制代码
关于WbemScripting.SWbemDateTime的详细资料:
http://msdn.microsoft.com/en-us/library/aa393687(VS.85).aspx
作者: everest79    时间: 2009-4-17 19:35

20090416143849.156000+480

UTC 20090416143849.156000
GMT +8*60  +480

wmic timezone get bias的值就是时区偏移量
20090416143849.156000+480表示已经在UTC时间添加了480分的时区偏移
作者: Batcher    时间: 2009-4-17 21:37     标题: 回复 2楼、3楼 的帖子 回复

感谢两位指点,受益良多。

在这个实际应用中的代码是否可以精简呢?
  1. 'cscript /nologo test.vbs
  2. '获取上次关机的日期和时间
  3. '我知道读取系统事件日志的方法可能显得简单些,此处不再赘述,纯粹为了学习更多知识。
  4. strValueName = "HKLM\SYSTEM\CurrentControlSet\Control\Windows\ShutdownTime"
  5. Set objShell = CreateObject("WScript.Shell")
  6. intArray = objShell.RegRead(strValueName)
  7. intTerm = intArray(7)*(2^56) + intArray(6)*(2^48) + intArray(5)*(2^40) + intArray(4)*(2^32) + intArray(3)*(2^24) + intArray(2)*(2^16) + intArray(1)*(2^8) + intArray(0)
  8. intDays = intTerm/(1E7*86400)
  9. dtmShutdownUTC = CDate(DateSerial(1601, 1, 1) + intDays)
  10. 'WScript.Echo dtmShutdownUTC
  11. Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
  12. objSWbemDateTime.SetVarDate(dtmShutdownUTC)
  13. dtmShutdownGMT = objSWbemDateTime.GetVarDate(false)
  14. 'WScript.Echo dtmShutdownGMT
  15. intBias = DateDiff("h", dtmShutdownGMT, dtmShutdownUTC)
  16. 'WScript.Echo intBias
  17. dtmShutdownLocal = DateAdd("h", intBias, dtmShutdownUTC)
  18. WScript.Echo dtmShutdownLocal
复制代码

作者: fastslz    时间: 2009-4-18 03:11

二者互转
  1. '本地时间转换为UTC
  2. Set SWDT = CreateObject("WbemScripting.SWbemDateTime")
  3. SWDT.SetVarDate Now, True
  4. Wscript.Echo SWDT
  5. 'UTC转换为本地时间
  6. aNow = SWDT.GetVarDate(True)
  7. Wscript.Echo aNow
复制代码

作者: Batcher    时间: 2009-4-18 03:22     标题: 回复 5楼 的帖子

这样只能进行格式转换,并未进行时区转换。
4楼的意图是时区转换,有何捷径否?

C:\Test>cscript /nologo test.vbs
20090418032409.000000+480
4/18/2009 3:24:09 AM


[ 本帖最后由 Batcher 于 2009-4-18 03:24 编辑 ]
作者: fastslz    时间: 2009-4-18 11:22

  1. strValueName = "HKLM\SYSTEM\CurrentControlSet\Control\Windows\ShutdownTime"
  2. Set objShell = CreateObject("WScript.Shell")
  3. intArray = objShell.RegRead(strValueName)
  4. intTerm = intArray(7)*(2^56) + intArray(6)*(2^48) + intArray(5)*(2^40) + intArray(4)*(2^32) + intArray(3)*(2^24) + intArray(2)*(2^16) + intArray(1)*(2^8) + intArray(0)
  5. intDays = intTerm/(1E7*86400)
  6. dtmShutdownUTC = CDate(DateSerial(1601, 1, 1) + intDays)
  7. 'WScript.Echo dtmShutdownUTC
  8. Set SWDT = CreateObject("WbemScripting.SWbemDateTime")
  9. SWDT.SetVarDate dtmShutdownUTC, True
  10. SWDT.Value = Left(SWDT,21)&"+000"
  11. dtmShutdownLocal = SWDT.GetVarDate(True)
  12. Wscript.Echo dtmShutdownLocal
复制代码

这样也不算捷径
作者: Batcher    时间: 2009-4-18 13:19     标题: 回复 7楼 的帖子

感觉这个才是SWDT的直接用法,学习了。我那个太绕弯子了。
作者: dahual    时间: 2010-6-8 19:30

碰巧遇到这个问题要解决,就到这里来了。。。
Batcher提供的信息很有用,楼上的几个也说明了原理。通过再次搜索,了解了真相,如下。
  1. '参考:
  2. 'http://www.windowsitpro.com/article/windows-2000/where-do-windows-2000-and-windows-nt-store-time-zone-information-.aspx
  3. 'http://www.realsoftware.com/listarchives/realbasic-nug/2004-12/msg01260.html
  4. Option Explicit
  5. Function GetNetTime(ByVal Url)
  6.     Dim Bias '时间偏移(分钟)
  7.     Dim objHttp,objReg
  8.     Dim DateLine,tmpDate
  9.     Dim GTime,LocalTime
  10.    
  11.     'On Error Resume Next
  12.     Set objReg=CreateObject("WScript.Shell")
  13.     '[ActiveTimeBias]:该键值存储当前系统时间相对格林尼治标准时间的偏移(以分钟为单位)
  14.     '[Bias]:该键值存储当前本地时间相对格林尼治标准时间的偏移(以分钟为单位)
  15.     Bias=objReg.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias")
  16.     Set objReg=Nothing
  17.    
  18. Set objHttp=CreateObject("MSXML2.XMLHTTP")
  19.     'Set objHttp=CreateObject("Microsoft.XMLHTTP")
  20.     With objHttp
  21.     .open "HEAD",Url,False
  22.     .send
  23.     DateLine=.getResponseHeader("Date")
  24.     End With
  25.     Set objHttp = Nothing
  26.     DateLine=Left(DateLine,InStr(1,DateLine,"GMT",vbTextCompare)-2)
  27.     'MsgBox DateLine
  28.     tmpDate=Split(DateLine,",")
  29.     GTime=tmpDate(1)
  30.     LocalTime=DateAdd("n",-CLng(Bias),GTime) '北京时间:GMT+8
  31.     GetNetTime=LocalTime
  32. End Function
  33. MsgBox GetNetTime("http://www.microsoft.com")
复制代码

作者: dahual    时间: 2010-6-8 19:43

存储的键值相当于:Hex(DateDiff("n",Now,GTime))




欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2