Skip to content

Commit

Permalink
Merge pull request #30 from Qowaiv/date-extensions
Browse files Browse the repository at this point in the history
Month.Days(Year year)
  • Loading branch information
Corniel Nobel committed Oct 12, 2018
2 parents 0760a44 + 4f6331f commit 7414cdb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
18 changes: 15 additions & 3 deletions src/Qowaiv/Month.cs
Expand Up @@ -21,7 +21,6 @@ namespace Qowaiv
{
/// <summary>Represents a month.</summary>
[DebuggerDisplay("{DebuggerDisplay}")]
[SuppressMessage("Microsoft.Design", "CA1036:OverrideMethodsOnComparableTypes", Justification = "The < and > operators have no meaning for a month.")]
[Serializable, SingleValueObject(SingleValueStaticOptions.All, typeof(byte))]
[TypeConverter(typeof(MonthTypeConverter))]
public struct Month : ISerializable, IXmlSerializable, IJsonSerializable, IFormattable, IEquatable<Month>, IComparable, IComparable<Month>
Expand Down Expand Up @@ -108,8 +107,6 @@ public struct Month : ISerializable, IXmlSerializable, IJsonSerializable, IForma
/// <summary>Returns true if the month is empty or unknown, otherwise false.</summary>
public bool IsEmptyOrUnknown() => IsEmpty() || IsUnknown();



/// <summary>Gets the full name of the month.</summary>
public string GetFullName(IFormatProvider formatProvider)
{
Expand All @@ -126,6 +123,21 @@ public string GetShortName(IFormatProvider formatProvider)
: (formatProvider as CultureInfo ?? CultureInfo.InvariantCulture).DateTimeFormat.GetAbbreviatedMonthName(m_Value);
}

/// <summary>Returns the number of days for the month.</summary>
/// <param name="year">
/// The year to ask the number of days for.
/// </param>
/// <remarks>
/// If the year of month is empty or unknown -1 is returned.
/// </remarks>
public int Days(Year year)
{
return
year.IsEmptyOrUnknown() || IsEmptyOrUnknown()
? -1
: DateTime.DaysInMonth((int)year, m_Value);
}

#endregion

#region (XML) (De)serialization
Expand Down
9 changes: 3 additions & 6 deletions src/Qowaiv/Year.cs
Expand Up @@ -51,21 +51,18 @@ public struct Year : ISerializable, IXmlSerializable, IJsonSerializable, IFormat
/// </returns>
public bool IsLeapYear
{
get
{
return !IsEmptyOrUnknown() && DateTime.IsLeapYear(m_Value);
}
get => !IsEmptyOrUnknown() && DateTime.IsLeapYear(m_Value);
}

#endregion

#region Methods

/// <summary>Returns true if the year is empty, otherwise false.</summary>
public bool IsEmpty() { return m_Value == default(Int16); }
public bool IsEmpty() => m_Value == default(short);

/// <summary>Returns true if the year is unknown, otherwise false.</summary>
public bool IsUnknown() { return m_Value == Year.Unknown.m_Value; }
public bool IsUnknown() => m_Value == Unknown.m_Value;

/// <summary>Returns true if the year is empty or unknown, otherwise false.</summary>
public bool IsEmptyOrUnknown() => IsEmpty() || IsUnknown();
Expand Down
21 changes: 21 additions & 0 deletions test/Qowaiv.UnitTests/MonthTest.cs
Expand Up @@ -96,6 +96,27 @@ public void IsEmptyOrUnknown_TestStruct_IsFalse()
Assert.IsFalse(TestStruct.IsEmptyOrUnknown());
}

[TestCase(31, 1979, 12)]
[TestCase(30, 2000, 11)]
[TestCase(29, 1980, 02)]
[TestCase(28, 1981, 02)]
public void Days(int expected, int year, int month)
{
Assert.AreEqual(expected, ((Month)month).Days(year));
}

[Test]
public void Days_EmptyYear()
{
Assert.AreEqual(-1, Month.April.Days(Year.Empty));
}

[Test]
public void Days_EmptyMonth()
{
Assert.AreEqual(-1, Month.Empty.Days(2018));
}

#endregion

#region TryParse tests
Expand Down

0 comments on commit 7414cdb

Please sign in to comment.