Convert sent from WCF service to UTC, and when you create a new time on your client, specify them as good UTC. This will be the base time for a universal standard time zone. You can display the time for your client and do not forget to identify it as UTC time. This will alleviate any inconsistency or ambiguity about what really is.
DateTime serverTimeRaw = myService.GetServerTime(); DateTime serverTimeUTC = new DateTime(serverTimeRaw.Ticks, DateTimeKind.Utc); Console.WriteLine(serverTimeUTC); // Prints server time as UTC time
If you really need to represent the time in the appropriate time zone, you will need to send the time zone information along with DateTime. I would recommend creating a type that encapsulates both parts of the information, and return that, not DateTime itself. Time zone information is not an integral component of DateTime. These are two separate problems, and when compiling the text they contain only composite meaning.
class ZonedDateTime { public DateTime DateTimeUtc { get; set; } public TimeZoneInfo TimeZone { get; set; } public DateTime ToDateTime() { DateTime dt = TimeZoneInfo.ConvertTime(DateTimeUtc, TimeZone); return dt; } }
jrista
source share