by Keyvan Nayyeri via Keyvan Nayyeri on 1/29/2007 12:30:18 PM
For many people around the world time zone doesn't matter at all. For some people it's important for their jobs and business but for developers it's just a pain! I can remember many situations that I've gotten in trouble with time zones myself. Two times (before release of Community Server 2.0 and launching BlogMailr) I had some conversations with Ken about them and it's still a big problem for software developers. Many users ask about the lack of their local time zone in Community Server and hopefully they can get their answer in all details after reading this post. I see some posts with my eyes where some users thought that Telligent is encouraging racism and were asking for their time zones to be included in list.
In this CS Dev Guide post I want to talk about time zones in Community Server APIs but before that I'll give short introduction to some concepts that you will see. I'm sure you know all details about time zone in general but we needs some details to be able to calculate local and universal time and convert them to each other in programming.
A time zone is an area that gets a specific time based on its distance from Greenwich in England. Greenwich time is also known as UTC for you. Each area in our world gets a local time based on its distance. For example +1:00 hour, +2:00 hours, -6:00 hours and so on. Greenwich is a source for calculations so we'll be able to have different times wherever we live and find others times using some basic mathematic calculations.
Ok, stop, this is very simple, what's the problem? The trouble is with something known as daylight saving time. In many countries governments change their local time on specific dates and add or subtract one hour from their local time. There are economic benefits in doing this thing. But as there isn't any standard for daylight savings then each government has its own rules for daylight savings. And also in some modern countries some bright persons (who love tomato) suddenly decide to avoid doing this (got it?!!). However, difference comes from daylight saving and I haven't seen a good solution for it except using a webservice.
Let's follow this story in Community Server ...
In Community Server we have a list of pre-defined time zones but this list doesn't include all zones. It only contains time zones with integer values so for example you have a time zone for +4:00 but don't have a time zone for +4:30. It's possible to add any new time zone to this list but as you'll see later, restriction for time zone values won't be solved unless the team changes the core code. For each time zone we have some information that helps us to calculate local and universal time and convert a DateTime value from one time zone to another. Community Server stores these information both for normal and daylight times to be able to do its calculations easier. It stores a name for each time zone for both cases as well as a bias for each case. There is also a general name for each time zone.
Alright, let's get in more technical details.
Three classes and one enumeration in CommunityServer.Components play all roles about managing time zones in Community Server:
You know that in Community Server most (or all?!) DateTime values will be shown to ends users based on the time zone that they have chosen in their profiles. Community Server also gets the time zone for server to do its calculations. Therefore above classes are very important for the power of Community Server in DateTime calculations.
This enumeration simply lists all available time zone areas in Community Server to help to work with them easier.
This class is derived from System.TimeZone class in .NET Framework and does the main job around time zones. Community Server hasn't added many new things to .NET TimeZone object but I'll describe its properties and methods anyway. It has six properties (regarding what I wrote in background):
You see that DaylighBias and StandardBias are both integer properties so Community Server doesn't support any double values for time zones and this is the answer to a common question about Community Server. I hope that we see good changes around time zones in Calypso.
It also has five methods:
This class is actually a collection for TimeZoneIntrernational objects so has some common methods and properties to work with collections. You can add or remove a TimeZoneInternational object to or from it or iterate through its items.
This class is as simple as one property and one method. TimeZones is a ReadOnly property that returns a TimeZoneInternationalCollection of all available time zones in application. GetTimeZone() gets a TimeZoneArea enumeration value and returns its corresponding TimeZoneInternational object.
You know it's possible to write many good examples about time zones but I only write two examples to demonstrate some basic principles and you can find your thread to follow.
In GetAllLocalTimes() method below I use TimeZoneInternationalManager to get a TimeZoneInternationalCollection of all available TimeZoneInternational objects then use server time to calculate current time in all these areas then return a Hashtable of time zone names and DateTime value of current time for them.
Hashtable GetAllLocalTimes()
{
DateTime serverTime = UserTime.CurrentServerTime;
Hashtable results = new Hashtable();
TimeZoneInternationalCollection zones =
TimeZoneInternationalManager.TimeZones;
foreach (TimeZoneInternational zone in zones)
results.Add(zone.DisplayName, zone.ToLocalTime(serverTime));
}
return results;
In GetLocalTimeInWestAustralia() I use TimeZoneInternationalManager to get a TimeZoneInternational object for West Australia then return a DateTime value of current local time there.
DateTime GetLocalTimeInWestAustralia()
TimeZoneInternational localZone = TimeZoneInternationalManager.GetTimeZone
(TimeZoneArea.WestAustraliaStandardTime);
return localZone.ToLocalTime(serverTime);
Original Post: CS Dev Guide: Time Zone
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.