3

So i need to script setting the system datetime from commandline (CMD.exe, BATCH possible) but im facing difficulties with time zones vs UTC and such.

My datetime source is UTC, which is the most portable and therefor makes sense.

Originally this code was implemented using (and equivalent for date part)

cmd.exe /C time HH:mm

which seems to work by setting the time and date but expects local time, so with respect to configured time zone. Since UTC is fed in, the result is mostly wrong and only sometimes right, depending on the time zone combination.

Since accessing system properties via WMIC is popular for getting the time (the time format is explained e.g. here and here), i cheerfully looked at the verb set instead of the widely used get and tried (from a cmd.exe started as Administrator):

C:\>wmic os get localdatetime
LocalDateTime
20180119112409.189000+060

C:\>wmic os set localdatetime=20180119122400.000000+060
Updating property(s) of '\\HOST\ROOT\CIMV2:Win32_OperatingSystem=@'
Property(s) update successful.

C:\>wmic os get localdatetime
LocalDateTime
20180119112438.447000+060

The set reports it was successful yet the value is not updated. The question here arises if one can provide +000 to signal that the time is UTC or if that would set localtimezone as well.

I have seen a different property regarding the time so i tried

C:\>wmic path win32_utctime set Hour=11
Updating property(s) of '\\HOST\root\cimv2:Win32_UTCTime=@'
ERROR:
Description = Provider is not capable of the attempted operation

What i can think of is get the UTC offset in a batch script via

C:\>wmic os get currenttimezone

parse the absolute value, apply it to the UTC datetime i have as input. Then split and use time and date again but this seems too tedious for such an easy task.

I dont have access to Powershell, the solution should be as portable as possible (will have to run on WindowsPE as well, with full wmic support enabled).

BNT
  • 768
  • 8
  • 24
  • [`SetDateTime` method of the `Win32_OperatingSystem` class](https://msdn.microsoft.com/en-us/library/aa393289(v=vs.85).aspx) sets the current system time on the computer. The calling process must have the [SE_SYSTEMTIME_NAME privilege](https://msdn.microsoft.com/en-us/library/windows/desktop/bb530716(v=vs.85).aspx). You probably do not have it: run `whoami /all|findstr /i "time"`. – JosefZ Feb 06 '18 at 20:24

0 Answers0