views:

337

answers:

3
  • Using Visual Studio 2008, c#, .net 2.0.

I have a Windows Forms client application that contains a scheduling UI section, currently this is housed only in the London office with the standard datetime picker control, the selected time is saved in a UK database (GMT) and a London based server aapplication processes the schedules.

There is a requirement to roll the client out to various global locations, Hong Kong, New York etc and allow them to setup schedules that run according to GMT time on the London server.

I'll have a label on screen saying "note schedules are GMT" what I need is a good way to present a datetime picker that always shows and is in sync with the database server's GMT time regardless of where the client app is running globally.

Suggestions on how to acheive this? thanks.

+1  A: 

The DateTime object has the ability to offset from local time to UTC/GMT time using .GetUTCXXX(), this will convert from the local time instance to a UTC offset based on the current Globalization/Localization setting being used in the runtime.

In addition there is a Timezone objet in the Globalization.CultureInfo that can be used for adjusting UTC/GMT times to local ones as well.

If the server is contactable, you could get the local time on the server and update your datepicker that way, but this may be overkill if you only need to have "approximate" times for the schedules and dont need that level of precision.

GrayWizardx
+2  A: 

The "GMT" timezone is ambiguous. On Windows, it refers to the timezone for London et al and includes daylight savings adjustment. In Greenwich at the Royal Observatory, it refers to the timezone that matches UTC with no DST adjustment.

Clearly, you want to avoid this kind of possible confusion. As well as the large number of mistakes you'll get when you ask users to make the conversion themselves. There's only one good way to do this, allow the user to enter times in the her local timezone. Convert that immediately to UTC and send that to the server. Anything coming back from the server should be converted back to local time at the last possible moment, just before display.

Hans Passant
I don't think this would work in my scenario. In addition to an Add schedule tab on the UI that I want to present in London GMT time, these is a tab showing running, queued and past jobs which run or are currently running on the London based server, the times on this Job summary tab are fed from the database that is London GMT time. So when a user is adding a job I want them to quickly scan the job summary tab to asses load on the server and schedule appropriately.
m3ntat
You possibly missed the last line of my post.
Hans Passant
A: 

I'm not sure if this will solve your problem or not, but check out the Any+Time(TM) JavaScript Library. Skip over the parts about the Date/Time widget and look for information about AnyTime.Converter, which is an object that converts strings to Date objects and back. It now includes format specifiers for UTC Offsets/TimeZones, as well as options for overriding the local time (check out the docs section on UTC Offsets, which includes options for utcFormatOffsetImposed and utcParseOffsetAssumed). There's also an example in the next section that explains how to take a local time and display it in GMT. If you have any questions or problems with it, submit them via the contact page for quick attention. I hope this helps!

Andrew M. Andrews III