Recipe2.7.Setting the System Time, Date, and Time Zone


Recipe 2.7. Setting the System Time, Date, and Time Zone

Problem

You want to set the system time, date, and time zone.

Solution

Using a graphical user interface

  1. From the Control Panel, open the Date and Time applet. This can also be accomplished by double-clicking the clock on the system tray or running timedate.cpl from the command line.

  2. You can configure the year, month, day, and time on the Date & Time tab and the time zone on the Time Zone tab.

  3. On Windows Server 2003, you can configure clock synchronization (Network Time Protocol (NTP) settings) from the Internet Time tab if the system is not part of a domain. If it is member of a domain, then the clock is synchronized automatically from an Active Directory domain controller.

Using a command-line interface

The following commands set the time to 11:02 p.m. and the date to November 1, 2005:

> time 23:02:00 > date 11/01/2005

The date format may vary depending on your locale.

Run this command to display the current date, time, and time zone:

> date /t & time /t & w32tm -tz

You can use this command to display time zone information from the registry:

> reg query \\<ServerName>\HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation

This command sets the SNTP server list:

> net time /setsntp:<ServerList>

For example:

> net time /setsntp:mytime.rallencorp.com,time.windows.com

This command queries the SNTP server:

> net time /querysntp

This command forces the local system to sync its time from the time source:

> net time /set

Using VBScript
' This code displays the local date, time, and time zone on a target computer ' ------ SCRIPT CONFIGURATION ------ strComputer = "."   ' e.g. rallen-srv01 ' ------ END CONFIGURATION --------- WScript.Echo "Current time using Now function: "  WScript.Echo vbTab & Now     set dicDaysOfWeek = CreateObject("Scripting.Dictionary") dicDaysOfWeek.Add 0, "Sun" dicDaysOfWeek.Add 1, "Mon" dicDaysOfWeek.Add 2, "Tue" dicDaysOfWeek.Add 3, "Wed" dicDaysOfWeek.Add 4, "Thu" dicDaysOfWeek.Add 5, "Fri" dicDaysOfWeek.Add 6, "Sat"     set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set objDateTime = objWMI.Get("Win32_Localtime=@") WScript.Echo "Current time using WMI: " WScript.Echo vbTab & dicDaysOfWeek.Item(objDateTime.DayOfWeek) & " " & _          objDateTime.Month & "/" & objDateTime.Day & "/" & _          objDateTime.Year & " " & objDateTime.Hour & ":" & objDateTime.Minute WScript.Echo "Time zone:" set colTZ = objWMI.ExecQuery("select * from Win32_TimeZone") for each objTZ in colTZ     Wscript.Echo vbTab & objTZ.Caption next

The easiest way to set the time and date via a script is by shelling out to the time and date commands, but this means the script must be run locally. Here is an example:

WScript.Echo "Current time: " & Now     strCommand = "cmd.exe /c time 23:02:00" set objWshShell = WScript.CreateObject("WScript.Shell") intRC = objWshShell.Run(strCommand, 0, TRUE) if intRC <> 0 then    WScript.Echo "Error returned from time command: " & intRC else    WScript.Echo "time command completed successfully" end if     strCommand = "cmd.exe /c date 11/01/2004" intRC = objWshShell.Run(strCommand, 0, TRUE) if intRC <> 0 then    WScript.Echo "Error returned from date command: " & intRC else    WScript.Echo "date command completed successfully" end if     WScript.Echo "New time: " & Now

If you want to set the time remotely, use the psexec command.


Discussion

If your server is part of a workgroup or NT 4 domain, you must manually configure the time and date settings on it. You can either set the time and date based on some external time source (such as your watch) or you can configure the server to synch from a time server. The latter is the preferred method because as long as the time server you are pointing your servers to has the correct time, your servers will have the correct time. If your server is part of an Active Directory domain, it will automatically sync its time from a domain controller.

Time and date synchronization is handled by the Windows Time service (W32Time), which was originally developed for Windows 2000 and is a compliant implementation of RFC 1769Simple Network Time Protocol (SNTP). For Windows Server 2003, W32Time was updated to support NTP, a more accurate protocol than SNTP.

W32Time is highly configurable via the registry. See MS KB 223184 for the list of registry settings you can configure for Windows 2000. A similar KB article has not been produced yet showing the differences for Windows Server 2003.

For a good whitepaper on the Windows 2000 Time service, visit the following URL: http://www.microsoft.com/windows2000/docs/wintimeserv.doc.

For more on the Windows Server 2003 Time service, visit the following URL: http://www.microsoft.com/Resources/Documentation/windowsserv/2003/all/techref/en-us/W2K3TR_times_intro.asp.

See Also

RFC 1305, RFC 1769, MS KB 216734 (How to Configure an Authoritative Time Server in Windows 2000), MS KB 223184 (Registry Entries for the W32Time Service), MS KB 247873 (Description of Date and Time Format Standards), MS KB 258059 (How to Synchronize the Time on a Windows 2000-Based Computer in a Windows NT 4.0 Domain), MS KB 262680 (A List of the Simple Network Time Protocol Time Servers That Are Available on the Internet), and MS KB 816043 (HOW TO: Turn On Debug Logging in the Windows Time Service)



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net