- binaries.zip
- Binaries
- DateTimeExtensions.dll
- NumberExtensions.dll
- StringExtensions.dll
- source.zip
- Source
- DateTime.Extensions
- Lazy.Coders.sln
- Number.Extensions
- String.Extensions
- TestExtensions
|
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.