Last week I was asked to test a small applet written by my colleague that could make Windows Mobile 5 device's local (system) date and time updated or set based on date/time received from an outside. The application used the .Net technology.
 
What stroked me was how elegant and simple the application was.
I also knew how to do this on a Windows server or client machine, but then spotted a difference: the only change was in using coredll.dll versa kernel32.dll (for a PC)! So simple, but the difference can be more than subtle for someone. So I have decided to share the code:
 
01 using System;
02 using System.Runtime.InteropServices;
03 using System.Collections.Generic;
04 using System.Text;
05 using System.IO;
06
07 namespace TimeSyncer
08 {
09     class TimeSync
10     {
11         [DllImport("coredll.dll")]
12         private extern static uint SetSystemTime(ref
13         SYSTEMTIME lpSystemTime);
14
15         private struct SYSTEMTIME
16         {
17             public ushort wYear;
18             public ushort wMonth;
19             public ushort wDayOfWeek;
20             public ushort wDay;
21             public ushort wHour;
22             public ushort wMinute;
23             public ushort wSecond;
24             public ushort wMilliseconds;
25         }
26
27         static int Main(string[] args)
28         {
29
30             LogMsgToFile("Application Started.");
31
32             DateTime serverDateTime;
33
34             if (args.Length == 0)
35             {
36                 System.Environment.Exit(1);
37             }
38             else
39             {
40                 try
41                 {
42                     // Obtained the date and time from the
43                     outside, e.g. a time server
44                     serverDateTime = DateTime.Parse(args[0]);
45                 }
46                 catch (System.FormatException)
47                 {
48                     System.Environment.Exit(1);
49                 }
50             }
51
52             SYSTEMTIME st = new SYSTEMTIME();
53
54             TimeSync tsync = new TimeSync();
55
56             DateTime updatedDateTime = tsync.GetDateTime(
57             serverDateTime);
58
59             st.wYear = (ushort)updatedDateTime.Year;
60             st.wMonth = (ushort)updatedDateTime.Month;
61             st.wDay = (ushort)updatedDateTime.Day;
62             st.wHour = (ushort)updatedDateTime.Hour;
63             st.wMinute = (ushort)updatedDateTime.Minute;
64             st.wSecond = (ushort)updatedDateTime.Second;
65
66             uint nReturn = SetSystemTime(ref st);
67
68             return 0; // as success
69         }
70
71         public DateTime GetDateTime(DateTime currentDateTime)
72         {
73             int hourOffsetValue;
74
75             // *** Converting time to GMT
76             // e.g. add 5 hours to the server supplied time
77             to make it GMT if it was sent from EST
78             // * modify the value below to suite your time
79             server input zone location
80             hourOffsetValue = 5;
81             currentDateTime = currentDateTime.AddHours(
82             hourOffsetValue);
83
84             return currentDateTime;
85         }
86     }
87 }
Share it: submit to reddit Digg It! or Bookmark it!