Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C# 4.0

Persian DateTime

Rate me:
Please Sign up or sign in to vote.
3.47/5 (25 votes)
25 Apr 2011CPOL 98.8K   8.2K   31  
A structure like System.DateTime which is designed to support Persian calendar
using System;
using System.ComponentModel;
using System.Text;

namespace Library.Helpers
{
    public static class Extensions
    {
        public static TAttribute GetItemAttribute<TAttribute>(this Enum value)
            where TAttribute: Attribute
        {
            if (value == null)
                return default(TAttribute);
            var attributes =
                (TAttribute[])
                value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof (TAttribute), false);
            return ((attributes.Length > 0) ? attributes[0] : default(TAttribute));
        }

        public static string GetItemDescription(this Enum value)
        {
            return value.GetItemDescription(true);
        }

        public static string GetItemDescription(this Enum value, bool parseNameIfNoDescription)
        {
            var descriptionAttribute = value.GetItemAttribute<DescriptionAttribute>();
            return ((descriptionAttribute == null)
                        ? value.ToString().SeparateCamelCase()
                        : descriptionAttribute.Description);
        }

        public static string SeparateCamelCase(this string name)
        {
            name.Replace(" ", "");
            var parserResult = new StringBuilder();
            foreach (var currChar in name)
            {
                if (!char.IsDigit(currChar)
                    &&
                    ((currChar.ToString().CompareTo("_") == 0)
                     || (currChar.ToString().CompareTo(currChar.ToString().ToUpper()) == 0)))
                    parserResult.Append(' ');
                parserResult.Append(currChar);
            }
            return parserResult.ToString().Trim();
        }
    }
}

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 Iran
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions