Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various improvements, unit testing etc. #26

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions ExtensionMethods/Dates/DateHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using System;
using System.Globalization;

namespace Umbraco.Community.ExtensionMethods.Dates {

public class DateHelpers {

/// <summary>
/// Gets the current age, from the specified date of birth.
/// </summary>
/// <param name="dateOfBirth">The date of birth.</param>
/// <returns>Returns the age based on the specified date of birth.</returns>
public static int GetAge(DateTime dateOfBirth) {
return GetAge(DateTime.Today, DateTime.Now);
}

/// <summary>
/// Gets the current age, from the specified date of birth. The age is calculated based on <var>dt</var>.
/// </summary>
/// <param name="dateOfBirth">The date of birth.</param>
/// <param name="dt"></param>
/// <returns>Returns the age based on the specified date of birth at the moment of <var>dt</var>.</returns>
public static int GetAge(DateTime dateOfBirth, DateTime dt) {
int age = dt.Year - dateOfBirth.Year;
if (dt.Month < dateOfBirth.Month || (dt.Month == dateOfBirth.Month && dt.Day < dateOfBirth.Day)) age--;
return age;
}

///<summary>
/// Gets the Day number and ordinal suffix for a given date.
///</summary>
///<param name="date">The date.</param>
///<returns>The day number and ordinal suffix.</returns>
public static string GetDayNumber(DateTime date) {
switch (date.Day) {
case 1:
case 21:
case 31:
return date.Day + "st";
case 2:
case 22:
return date.Day + "nd";
case 3:
case 23:
return date.Day + "rd";
default:
return date.Day + "th";
}
}

/// <summary>
/// Determines whether the specified date is weekday.
/// </summary>
/// <param name="date">The date.</param>
/// <returns>
/// Returns <var>TRUE</var> if the specified day is weekday; otherwise <var>FALSE</var>.
/// </returns>
public static bool IsWeekday(DateTime date) {
return date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday;
}

/// <summary>
/// Determines whether the specified date is weekend.
/// </summary>
/// <param name="date">The date.</param>
/// <returns>
/// Returns <var>TRUE</var> if the specified day is weekend; otherwise <var>FALSE</var>.
/// </returns>
public static bool IsWeekend(DateTime date) {
return !IsWeekday(date);
}

/// <summary>
/// Determines whether the year of the specified date is a leap year.
/// </summary>
/// <param name="date">The date.</param>
/// <returns>
/// Returns <var>TRUE</var> if the specified date is in a leap year; otherwise <var>FALSE</var>.
/// </returns>
public static bool IsLeapYear(DateTime date) {
return IsLeapYear(date.Year);
}

/// <summary>
/// Determines whether the specified year is a leap year.
/// </summary>
/// <param name="year">The year.</param>
/// <returns>
/// Returns <var>TRUE</var> if the specified year is a leap year; otherwise <var>FALSE</var>.
/// </returns>
public static bool IsLeapYear(int year) {
return (DateTime.DaysInMonth(year, 2).Equals(29));
}

/// <summary>
/// Get the English name of the day.
/// </summary>
/// <param name="date">The date.</param>
/// <returns>Returns the English name of the day.</returns>
public static string GetDayName(DateTime date) {
return date.ToString("dddd", CultureInfo.InvariantCulture);
}

/// <summary>
/// Gets the name of the day as specified by the current culture.
/// </summary>
/// <param name="date">The date.</param>
/// <returns>Returns the local name of the day.</returns>
public static string GetLocalDayName(DateTime date) {
return date.ToString("dddd", CultureInfo.CurrentCulture);
}

/// <summary>
/// Gets the name of the day as specified by <var>culture</var>.
/// </summary>
/// <param name="date">The date.</param>
/// <param name="culture">The culture to be used.</param>
/// <returns>Returns the local name of the day.</returns>
public static string GetLocalDayName(DateTime date, CultureInfo culture) {
return date.ToString("dddd", culture);
}

/// <summary>
/// Get the English name of the month.
/// </summary>
/// <param name="date">The date.</param>
/// <returns>Returns the English name of the month.</returns>
public static string GetMonthName(DateTime date) {
return date.ToString("MMMM", CultureInfo.InvariantCulture);
}

/// <summary>
/// Gets the name of the month as specified by the current culture.
/// </summary>
/// <param name="date">The date.</param>
/// <returns>Returns the local name of the month.</returns>
public static string GetLocalMonthName(DateTime date) {
return date.ToString("MMMM", CultureInfo.CurrentCulture);
}

/// <summary>
/// Gets the name of the month as specified by <var>culture</var>.
/// </summary>
/// <param name="date">The date.</param>
/// <param name="culture">The culture to be used.</param>
/// <returns>Returns the local name of the month.</returns>
public static string GetLocalMonthName(DateTime date, CultureInfo culture) {
return date.ToString("MMMM", culture);
}

}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Umbraco.Community.ExtensionMethods.Dates
namespace Umbraco.Community.ExtensionMethods.Dates.ExtensionMethods
{
/// Kudos to uComponents - http://ucomponents.codeplex.com/SourceControl/latest#uComponents.XsltExtensions/Dates.cs
public static class Dates
public static class DateExtensionMethods
{

/// <summary>
Expand All @@ -25,100 +21,46 @@ public static class Dates
/// <returns>
/// Returns the age based on the specified date of birth.
/// </returns>
public static int Age(this DateTime dateOfBirth)
{
//Today's date
var today = DateTime.Today;

// if month is less, or if month is equal, and day less
if (today.Month < dateOfBirth.Month || today.Month == dateOfBirth.Month && today.Day < dateOfBirth.Day)
{
// then they haven't had this year's birthday yet!
return today.Year - dateOfBirth.Year - 1;
}
else
{
// otherwise, substract the current year from date-of-birth.
return today.Year - dateOfBirth.Year;
}

// unable to parse date-of-birth.
return -1;
public static int Age(this DateTime dateOfBirth) {
return DateHelpers.GetAge(dateOfBirth);
}

///<summary>
/// Gets the Day number and ordinal suffix for a given date
/// Gets the Day number and ordinal suffix for a given date.
///</summary>
///<param name="date">The date</param>
///<returns>The day number and ordinal suffix</returns>
public static string GetDayNumber(this DateTime date)
{
switch (date.Day)
{
case 1:
case 21:
case 31:
return date.Day + "st";
case 2:
case 22:
return date.Day + "nd";
case 3:
case 23:
return date.Day + "rd";
default:
return date.Day + "th";
}
///<param name="date">The date.</param>
///<returns>The day number and ordinal suffix.</returns>
public static string GetDayNumber(this DateTime date) {
return DateHelpers.GetDayNumber(date);
}


/// <summary>
/// Determines whether the specified day is weekday.
/// Determines whether the specified date is weekday.
/// </summary>
/// <param name="day">The date</param>
/// <returns>
/// <c>true</c> if the specified day is weekday; otherwise, <c>false</c>.
/// </returns>
public static bool IsWeekday(this DateTime date)
{
var day = date.DayOfWeek;

switch (day)
{
// is a weekend?
case DayOfWeek.Saturday:
case DayOfWeek.Sunday:
return false;

// otherwise its a weekday?
default:
return true;
}
/// <param name="date">The date.</param>
/// <returns>Returns <var>TRUE</var> if the specified day is weekday; otherwise <var>FALSE</var>.</returns>
public static bool IsWeekday(this DateTime date) {
return DateHelpers.IsWeekday(date);
}

/// <summary>
/// Determines whether the specified date is weekend.
/// </summary>
/// <param name="date">The date</param>
/// <returns>
/// <c>true</c> if the specified date is weekend; otherwise, <c>false</c>.
/// </returns>
public static bool IsWeekend(this DateTime date)
{
return !IsWeekday(date);
/// <param name="date">The date.</param>
/// <returns>Returns <var>TRUE</var> if the specified day is weekend; otherwise <var>FALSE</var>.</returns>
public static bool IsWeekend(this DateTime date) {
return !DateHelpers.IsWeekday(date);
}


/// <summary>
/// Determines whether [is leap year] [the specified date].
/// Determines whether the year of the specified date is a leap year.
/// </summary>
/// <param name="date">The date.</param>
/// <returns>
/// <c>true</c> if [is leap year] [the specified date]; otherwise, <c>false</c>.
/// Returns <var>TRUE</var> if the specified date is in a leap year; otherwise <var>FALSE</var>.
/// </returns>
public static bool IsLeapYear(this DateTime date)
{
// test if number of days in Feburary is 29 for the current year in the date
return (DateTime.DaysInMonth(date.Year, 2).Equals(29));
public static bool IsLeapYear(this DateTime date) {
return DateHelpers.IsLeapYear(date);
}

/// <summary>
Expand Down Expand Up @@ -319,25 +261,61 @@ private static string GetDayNumberSuffix(DateTime date)
}

/// <summary>
/// Gets the month string of the given DateTime
/// Get the English name of the day.
/// </summary>
/// <param name="date">The DateTime object</param>
/// <returns>The string representation of month</returns>
/// <example>Returns "January" for the date 1.1.2013</example>
public static string GetMonthName(this DateTime date)
{
return date.ToString("MMMM");
/// <param name="date">The date.</param>
/// <returns>Returns the English name of the day.</returns>
public static string GetDayName(DateTime date) {
return DateHelpers.GetDayName(date);
}

/// <summary>
/// Gets the day string of the given DateTime.
/// Gets the name of the day as specified by the current culture.
/// </summary>
/// <param name="date">The DateTime object</param>
/// <returns>The string representation of day</returns>
/// <example>Returns "Sunday" for the date 1.1.2013</example>
public static string GetDayName(this DateTime date)
{
return date.DayOfWeek.ToString();
/// <param name="date">The date.</param>
/// <returns>Returns the local name of the day.</returns>
public static string GetLocalDayName(DateTime date) {
return DateHelpers.GetLocalDayName(date);
}

/// <summary>
/// Gets the name of the day as specified by <var>culture</var>.
/// </summary>
/// <param name="date">The date.</param>
/// <param name="culture">The culture to be used.</param>
/// <returns>Returns the local name of the day.</returns>
public static string GetLocalDayName(DateTime date, CultureInfo culture) {
return DateHelpers.GetLocalDayName(date, culture);
}

/// <summary>
/// Get the English name of the month.
/// </summary>
/// <param name="date">The date.</param>
/// <returns>Returns the English name of the month.</returns>
public static string GetMonthName(DateTime date) {
return DateHelpers.GetMonthName(date);
}

/// <summary>
/// Gets the name of the month as specified by the current culture.
/// </summary>
/// <param name="date">The date.</param>
/// <returns>Returns the local name of the month.</returns>
public static string GetLocalMonthName(DateTime date) {
return DateHelpers.GetLocalMonthName(date);
}

/// <summary>
/// Gets the name of the month as specified by <var>culture</var>.
/// </summary>
/// <param name="date">The date.</param>
/// <param name="culture">The culture to be used.</param>
/// <returns>Returns the local name of the month.</returns>
public static string GetLocalMonthName(DateTime date, CultureInfo culture) {
return DateHelpers.GetLocalMonthName(date, culture);
}

}

}
3 changes: 2 additions & 1 deletion ExtensionMethods/ExtensionMethods.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@
<Compile Include="Content.cs" />
<Compile Include="ASPNET.cs" />
<Compile Include="DataTypes.cs" />
<Compile Include="Dates.cs" />
<Compile Include="Dates\ExtensionMethods\DateExtensionMethods.cs" />
<Compile Include="Dates\DateHelpers.cs" />
<Compile Include="DocumentTypes.cs" />
<Compile Include="ImageGen.cs" />
<Compile Include="ImageResizing.cs" />
Expand Down
2 changes: 1 addition & 1 deletion ExtensionMethods/YouTube/YouTubeHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static string GetIdFromString(string subject) {
var tests = new[] {
Regex.Match(subject, "^((\\w|-){11}$)"),
Regex.Match(subject, "v=((\\w|-){11})"),
Regex.Match(subject, "\\/((\\w|-){11})$"),
Regex.Match(subject.Split('?')[0], "\\/((\\w|-){11})$"),
Regex.Match(subject, "\\/vi|v|embed/((\\w|-){11})")
};

Expand Down
1 change: 1 addition & 0 deletions TestSite/Views/Home.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using Umbraco.Community.ExtensionMethods.Dates
@using Umbraco.Community.ExtensionMethods.Dates.ExtensionMethods
@using Umbraco.Community.ExtensionMethods.Social
@using Umbraco.Community.ExtensionMethods.Strings;
@using Umbraco.Community.ExtensionMethods.ImageGen;
Expand Down
Loading