使用batch file从不同的时区获取本地时间

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

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

我只是希望有人能以更“适当”的方式做到这一点。 search这个/其他网站,但到目前为止没有发现任何东西。

ps:没有“外部”工具/ etc,因为最终的batch file需要分布在欧洲,中东和非洲地区。

  1. 得到当地的日期和时间( wmic
  2. 得到本地抵消( wmic
  3. 在#2的帮助下将当地时间转换为UTC
  4. 得到TZ的偏移量。 ( tzutil /l
  5. 转换#3和#4的结果,不要提及日期的变化(这是变得复杂的地方)。

我想我没有忘记什么? 🙂

我需要通过批处理文件从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%