0

How do I convert US-style DateTime such as 5/1/2012 3:38:27 PM returned from the server to user's local time? I am developing for windows phone.

I've tried

DateTime localTime = serverTime.ToLocalTime();

but the result is off a couple of hours. I thought ToLocalTime() will take care of the conversion to any timezone the user are in? Perhaps I need to get the user's timezone info first?

EDIT 1

I think the serverTime is in the PST time zone

EDIT 2

My timezone is GMT +8. I tried the following, but the resulting localTime is 15 hour behind.

TimeZoneInfo localZone = TimeZoneInfo.Local;
DateTime localTime = TimeZoneInfo.ConvertTime(serverTime, localZone);

EDIT 3

This result in 7 hours behind my local time.

TimeZoneInfo localZone = TimeZoneInfo.Local;
DateTime dateTimeKind = DateTime.SpecifyKind(serverTime, DateTimeKind.Utc);
DateTime localTime = TimeZoneInfo.ConvertTime(dateTimeKind, localZone);

EDIT 4

OK I think I am getting there but not sure if this is applicable for all time zones. I think I still have to consider day light saving because the resulting local time is just one hour ahead now.

TimeZoneInfo localZone = TimeZoneInfo.Local;
double offset = localZone.GetUtcOffset(DateTime.Now).TotalHours;
DateTime dateTimeKind = DateTime.SpecifyKind(serverTime, DateTimeKind.Utc);
DateTime localTime = TimeZoneInfo.ConvertTime(dateTimeKind, localZone).AddHours(offset);

But then how do you get DLS is in effect for a particular time zone in Windows Phone? TimeZoneInfo.FindSystemTimeZoneById does not seem to be supported?

PutraKg
  • 2,226
  • 3
  • 31
  • 60

2 Answers2

1

For this to work, the DateTime-object serverTime must be of the UTC-form - or at least know what Kindit is. Read all the details around this under the remarks section of this page.

Best of luck!

Kris Selbekk
  • 7,438
  • 7
  • 46
  • 73
0

What does the time represent? If it is a specific moment in time, such as the date and time that something happened, then you should update your server code to return the time in one of the following formats:

// ISO8601 local time with offset.
// get from DateTimeOffset.ToString("o")
2012-05-01T15:38:27-07:00

// ISO8601 UTC time
// get from DateTime.ToString("o") when kind is UTC
2012-05-01T22:38:27Z

It's really important that you do this, because local times can be ambiguous when daylight savings ends. You must either provide the correct offset, (-8 for PST, -7 for PDT), or send as UTC.

There are very few scenarios where sending local time by itself makes sense. If you think you have one, please elaborate about what the time represents.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • The time represents the moment the server receives a string (chat message). Unfortunately I do not have access to the server to change the code there. Anyway I ended up not converting to local time but, get the server local time and calculate when the message was last posted, like `5 mins ago`, `2 hours ago` etc – PutraKg Apr 22 '13 at 14:22
  • @PutraKg - Be aware that you will have ambiguity on November 3rd from 1:00 AM to 2:00 AM, when the offset might be either -7 or -8 and you will have no way of knowing which one it is. If this matters to you, then you should have a discussion with whomever is responsible for the server code. – Matt Johnson-Pint Apr 22 '13 at 14:58
  • 1
    If you are subtracting from server time, then you might get a negative value, or an extra hour (on March 10 or Nov 3). Your code should be prepared to deal with the ambiguity, or the possibility of negative values. – Matt Johnson-Pint Apr 22 '13 at 15:02
  • You are right, thanks for the heads up. I will get in touch with them and ask how to best deal with this situation. – PutraKg Apr 22 '13 at 15:18