使用批处理文件从不同的时区获取本地时间

时间:2015-09-19 12:11:09

标签: windows batch-file cmd

我不想使用任何脚本语言,只需使用简单的命令行命令/脚本。

我可以使用(准备好的)TZ表(例如格式:UTC + 06:00 +'TZ name')来解决这个问题,然后使用TZUTIL收集他们的resp UTC值并计算时区的当地时间(最好是将DST纳入帐户)。

我只是希望有人已经以更“恰当”的方式做到了这一点。搜索了这个/其他网站,但到目前为止一无所获。

ps:没有'外部'工具/等,因为最终的批处理文件需要以EMEA方式分发。

2 个答案:

答案 0 :(得分:3)

  1. 获取当地日期和时间(wmic
  2. 获取本地偏移量(wmic
  3. 在#2
  4. 的帮助下将当地时间转换为UTC
  5. 获得TZ的偏移量。 (tzutil /l
  6. 转换#3和#4的结果,更不用说日期的变化(这是变得复杂的地方)。
  7. 我想我没有忘记什么? :)

答案 1 :(得分:1)

我需要通过批处理文件从MS Windows系统获取特定格式的时区信息。我找不到我需要的东西,所以我开发了自己的东西。以为我会分享给别人使用。使用代码并根据需要进行调整。

@echo off
REM Obtain the ActiveBias value and convert to decimal
for /f "tokens=3" %%a in ('reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation /v ActiveTimeBias ^| grep -i "ActiveTimeBias"') do set /a abias=%%a
for /f "tokens=3-8" %%n in ('reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation /v TimeZoneKeyName') do set tzn=%%n %%o %%p %%q %%r 2>NUL

REM Set the + or - sign variable to reflect the timezone offset
IF "%abias:~0,1%"=="-" (set si=+) ELSE (set si=-)
for /f "tokens=1 delims=-" %%t in ('echo %abias%') do set tzc=%%t

REM Calculate to obtain floating points (decimal values)
set /a tzd=100*%tzc%/60

REM Calculate the active bias to obtain the hour
set /a tze=%tzc%/60

REM Set the minutes based on the result of the floating point calculation
IF "%tzd%"=="0" (set en=00 && set si=)
IF "%tzd:~1%"=="00" (set en=00) ELSE IF "%tzd:~2%"=="00" (set en=00 && set tz=%tzd:~0,2%)
IF "%tzd:~1%"=="50" (set en=30) ELSE IF "%tzd:~2%"=="50" (set en=30 && set tz=%tzd:~0,2%)
IF "%tzd:~1%"=="75" (set en=45) ELSE IF "%tzd:~2%"=="75" (set en=45 && set tz=%tzd:~0,2%)

REM Adding a 0 to the beginning of a single digit hour value
IF %tze% LSS 10 (set tz=0%tze%)

REM Display timezone name and offset
echo %tzn% %si%%tz%%en%