Click here to Skip to main content
15,881,812 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.7K   1.6K   131  
A simple Util library.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using DateTimeExtensions;
namespace TestExtensions
{
    [TestFixture]
    public class Date
    {
        [Test]
        public void TEST_MIDNIGHT_NOON()
        {
            DateTime dateTime1 = new DateTime(2009, 11, 2,5,5,5);
            DateTime result1 = new DateTime(2009, 11, 2,12,0,0);

            DateTime dateTime2 = new DateTime(2009, 11, 1,4,3,10);
            DateTime result2 = new DateTime(2009, 11, 1,0,0,0);
          
            Assert.AreEqual(result1, dateTime1.Noon());
            Assert.AreEqual(result2, dateTime2.MidNight());
        }

        [Test]
        public void TEST_TIMESPAN_TO_WORDS()
        {
            TimeSpan timeSpan1 = new TimeSpan(5, 11, 2, 5);
            string result1 = "5 days, 11 hours, 2 minutes and 5 seconds";

            TimeSpan timeSpan2 = new TimeSpan(7, 1, 0, 0);
            string result2 = "1 week and 1 hour";

            TimeSpan timeSpan3 = new TimeSpan(365, 24, 0, 0);
            string result3 = "1 year and 1 day";

            TimeSpan timeSpan4 = new TimeSpan(14, 24, 0, 0);
            string result4 = "2 weeks and 1 day";

            Assert.AreEqual(result1, timeSpan1.ToWords());
            Assert.AreEqual(result2, timeSpan2.ToWords());
            Assert.AreEqual(result3, timeSpan3.ToWords());
            Assert.AreEqual(result4, timeSpan4.ToWords());
        }
        [Test]
        public void TEST_DATE_WITHIN_RANGE()
        {
            DateTime dateTime1 = new DateTime(2009, 1, 1);
            DateTime floor1 = new DateTime(2008, 12, 31);
            DateTime ceiling1 = new DateTime(2009, 1, 2);

            DateTime dateTime2 = new DateTime(2009, 1, 1);
            DateTime floor2 = new DateTime(2008, 1, 12);
            DateTime ceiling2 = new DateTime(2008, 12, 31);

            DateTime dateTime3 = new DateTime(2009, 1, 1);
            DateTime floor3 = new DateTime(2009, 1, 1);
            DateTime ceiling3 = new DateTime(2009, 1, 2);

            Assert.AreEqual(true,dateTime1.IsWithinRange(floor1,ceiling1,true));
            Assert.AreEqual(false,dateTime1.IsWithinRange(floor2,ceiling2,true));
            Assert.AreEqual(true,dateTime1.IsWithinRange(floor3,ceiling3,true));
            Assert.AreEqual(false,dateTime1.IsWithinRange(floor3,ceiling3,false));
        }
        [Test]
        public void TEST_TIME_SPAN()
        {
            DateTime futureDate = DateTime.Now.Add(new TimeSpan(2,3,4));
            DateTime pastDate = DateTime.Now.Subtract(new TimeSpan(2, 3, 4));

            TimeSpan result1 = new TimeSpan(2, 3, 4);

            Assert.AreEqual(result1, futureDate.GetTimeSpan());
            Assert.AreEqual(result1, pastDate.GetTimeSpan());
        }
    }
}

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