Click here to Skip to main content
15,886,026 members
Articles / Web Development / CSS

Building a Web Message Board using Visual Studio 2008, Part I - The Basic Message Board

Rate me:
Please Sign up or sign in to vote.
4.90/5 (83 votes)
30 Dec 2007CPOL47 min read 375.2K   3.7K   333  
This article builds a web based message board and uses several new technologies introduced with Visual Studio 2008 such as LINQ, WCF Web Programming, WCF Syndication, ASP.NET ListView, ASP.NET DataPager etc.
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using MessageBoard.Web.Properties;
using System.Runtime.Serialization;

namespace MessageBoard.Web
{
    /// <summary>
    /// This class has all teh helper functions to detect and save time zone values
    /// </summary>
    public static class TimeZoneUtility
    {
        /// <summary>
        /// Gets a list of time zones which have the specified UTC offset in minutes. If the string is invalid
        /// all time zones are returned.
        /// </summary>
        /// <param name="jsUtcOffset">The Utc offset from JavaScript ontained from the Request</param>
        /// <returns>List of time zones</returns>
        public static IEnumerable<TimeZoneInfo> GetTimeZonesFromUtcOffset(string jsUtcOffset)
        {
            int timeZoneOffset = 0;

            return (Int32.TryParse(jsUtcOffset, out timeZoneOffset)) ?
                GetTimeZonesFromUtcOffset(timeZoneOffset) :
                TimeZoneInfo.GetSystemTimeZones();
        }

        /// <summary>
        /// Returns timezone offset from specified offset obtained from JavaScript
        /// </summary>
        /// <param name="timeZoneOffset">Offset obtained from javaScript's getTimezoneOffset method</param>
        /// <returns>List of time zones</returns>
        public static IEnumerable<TimeZoneInfo> GetTimeZonesFromUtcOffset(int timeZoneOffset)
        {
            TimeSpan jsOffsetTimeSpan = new TimeSpan(0, -timeZoneOffset, 0);

            //UTC offset is in range from -14 to +14 hours
            //TimeSpan.Duration gives us the absolute value
            if (jsOffsetTimeSpan.Duration().TotalHours > 14)
                return TimeZoneInfo.GetSystemTimeZones();

            return from tz in TimeZoneInfo.GetSystemTimeZones()
                   where tz.BaseUtcOffset.Equals(jsOffsetTimeSpan)
                   select tz;
        }
    
        /// <summary>
        /// Returns TimeZoneInfo given the time zone id. If the timezone is invalid
        /// it returns the local timzezone
        /// </summary>
        /// <param name="id">The timezone id specified as string</param>
        /// <returns>The time zone info</returns>
        public static TimeZoneInfo GetTimeZoneFromId(string id)
        {
            if (String.IsNullOrEmpty(id))
                return TimeZoneInfo.Local;

            TimeZoneInfo info;

            try
            {
                info = TimeZoneInfo.FindSystemTimeZoneById(id);
            }
            catch (TimeZoneNotFoundException)
            {
                info = TimeZoneInfo.Local;
            }

            return info;
        }

        private static readonly string CookieName = "__TimeZoneInfo";

        /// <summary>
        /// Saves the time zone info in a cookie
        /// </summary>
        /// <param name="info">The time zone info to save in a cookie</param>
        public static void SaveTimeZoneInfoInCookie(TimeZoneInfo info)
        {
            HttpContext context = HttpContext.Current;

            if (context == null)
                throw new InvalidOperationException(Resources.NullHttpContext);
            
            HttpCookie cookie = new HttpCookie(CookieName, info.Id);
            cookie.Expires = DateTime.Now.AddYears(1); //Expire after a year
            context.Response.AppendCookie(cookie);
        }

        /// <summary>
        /// Uses the current HttpContext to obtain a TimzeZoneInfo value stored in a cookie.
        /// If no such TimezonInfo is found or the cookie data is invalid this function returns null
        /// </summary>
        /// <returns>The TimeZoneInfo from the cookie or null otherwise</returns>
        public static TimeZoneInfo GetTimeZoneInfoFromCookie()
        {
            HttpContext context = HttpContext.Current;

            if (context == null)
                throw new InvalidOperationException(Resources.NullHttpContext);

            HttpCookie cookie = context.Request.Cookies[CookieName];

            TimeZoneInfo info = TimeZoneInfo.Utc;

            if (cookie == null || String.IsNullOrEmpty(cookie.Value))
                return info;

            try
            {
                info = TimeZoneInfo.FindSystemTimeZoneById(cookie.Value);

            }
            catch (TimeZoneNotFoundException )
            {
                //It's ok just return null               
            }

            return info;
        }

        public static DateTime ConvertToCurrentTimeZone(DateTime dateTime)
        {
            return TimeZoneInfo.ConvertTimeFromUtc(dateTime, TimeZoneUtility.GetRequestTimeZone());
        }

        private static object _timeZoneKey = new object();

        /// <summary>
        /// Gets the current time zone of the Request
        /// </summary>
        /// <returns>The timezone value</returns>
        public static TimeZoneInfo GetRequestTimeZone()
        {
            TimeZoneInfo info = HttpContext.Current.Items[_timeZoneKey] as TimeZoneInfo;

            if (info == null)
            {
                info = GetTimeZoneInfoFromCookie();
                HttpContext.Current.Items[_timeZoneKey] = info;
            }

            return info;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions