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

Util Library and Extension Methods

Rate me:
Please Sign up or sign in to vote.
4.86/5 (49 votes)
7 May 2013CPOL2 min read 47.8K   1.6K   131  
A simple Util library.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cynosura;

namespace DateTimeExtensions
{
    public static class Utils
    {
        /// <summary>
        /// Represents TimeSpan in words
        /// Test Coverage: Included
        /// </summary>
        /// <param name="val"></param>
        /// <returns>String representation of the timespan</returns>
        public static string ToWords(this TimeSpan val)
        {
            return TimeSpanArticulator.Articulate(val, TemporalGroupType.day
                |TemporalGroupType.hour
                |TemporalGroupType.minute
                |TemporalGroupType.month 
                |TemporalGroupType.second 
                |TemporalGroupType.week 
                |TemporalGroupType.year);
        }
        /// <summary>
        /// Converts Datetime value at midnight
        /// Test Coverage: Included
        /// </summary>
        /// <param name="val"></param>
        /// <returns>DateTime value with time set to Midnight</returns>
        public static DateTime MidNight(this DateTime val)
        {
            return new DateTime(val.Year, val.Month, val.Day, 0, 0, 0);
        }
        /// <summary>
        /// Converts Datetime value at noon
        /// Test Coverage: Included
        /// </summary>
        /// <param name="val"></param>
        /// <returns>DateTime value with time set to Noon</returns>
        public static DateTime Noon(this DateTime val)
        {
            return new DateTime(val.Year, val.Month, val.Day, 12, 0, 0);
        }
        /// <summary>
        /// Checks if the Datetime lies within a given range
        /// Test Coverage: Included
        /// </summary>
        /// <param name="val"></param>
        /// <param name="floor">The floor value of the range</param>
        /// <param name="ceiling">The ceiling value of the range</param>
        /// <param name="includeBase">True to include the floor and ceiling values for comparison</param>
        /// <returns>Returns true if the value lies within the range</returns>
        public static bool IsWithinRange(this DateTime val, DateTime floor, DateTime ceiling, bool includeBase)
        {
            if (floor > ceiling)
                throw new InvalidOperationException("floor value cannot be greater than ceiling value");
            if (floor == ceiling)
                throw new InvalidOperationException("floor value cannot be equal to ceiling value");

            if (includeBase)
                return (val >= floor && val <= ceiling);
            else
                return (val > floor && val < ceiling);
        }
        /// <summary>
        /// Calculates the TimeSpan between the current Datetime and the provided Datetime
        /// Test Coverage: Included
        /// </summary>
        /// <param name="val"></param>
        /// <returns>TimeSpan between the current DateTime & the provided DateTime</returns>
        public static TimeSpan GetTimeSpan(this DateTime val)
        {
            TimeSpan dateDiff;
            if (val < DateTime.Now)
                dateDiff = DateTime.Now.Subtract(val);
            else if (val > DateTime.Now)
                dateDiff = val.Subtract(DateTime.Now);
            else
                throw new InvalidOperationException("value cannot be equal to DateTime.Now");
            return dateDiff;
        }
    }    
}

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
Software Developer (Senior) Schneider Electric, GTCI Bangalore
India India
Music is my passion,
Apart from programming I like to read a lot.

Comments and Discussions