views:

185

answers:

2

I have an ASP.NET web application that requires users to select their appropriate time zone so that it can correctly show local times for events.

In creating a simple approach for selecting the time zone, I started by just using the values from TimeZoneInfo.GetSystemTimeZones(), and showing that list.

The only problem with this is that since our application is primarily targeted at the United States, I'd like to show those entries first, basically starting with Eastern Time and working backwards (West) until I reach Atlantic time.

What's a good approach to do this in code?

A: 

Using LINQ:

var timezones = TimeZoneInfo.GetSystemTimeZones();
var orderedZones = (from t in timezones
                                where t.DisplayName.Contains("(US & Canada)")
                                select t).Concat(
                                from t in timezones 
                                where t.DisplayName.Contains("(US & Canada)") == false
                                select t
                               );
Dave Swersky
(expression == false) == (!expression), sorry for picking, makes more sense to me though since you didn't use expression == true for the first one though
NickLarsen
Well, that's considerably better than what I had, but it puts them way out of order--it goes from UTC-08:00 to UTC, then to UTC+.
reallyJim
@Nick- true, this was a quick hack.@reallyJim- you can order the unions independently by UTCOffset. That will order the groups in ascending order with the US zones at the top.
Dave Swersky
reallyJim
A: 

Ok, so my solution: I put the display name, Id, and the order I wanted them in the database. It works, as I just store the Id in a user object anyway. I'll grab it once, and store it in the application cache.

Hack-y, but it "solves" the problem.

reallyJim