This is a very strange requirement and some explanation of why it's required could lead to a better answer.
Anyway, taking your question as you have posed it, you will need a custom struct.
System.DateTime
is basically a wrapper around a
long
that represents the number of ticks since 1st Jan 0001. You need to do something similar. As you only need day and not tick precision I would suggest wrapping an
int
. once that is done the rest is fairly standard stuff.
I have coded (not tested) a basic sample [because I found the problem sort of interesting] that meets your requirements except how the span is displayed - you can call
GetDaySpan
and use some math on the result to get what you want. There is a lot of optimisation that could be done but I leave that up to you too.
Enjoy...
public struct WierdDate
{
public const int DaysPerMonth = 30;
public const int DaysPerYear = DaysPerMonth * MonthsPerYear;
public const int MaxYear = 9999;
public const int MonthsPerYear = 12;
public static readonly WierdDate MaxWierdDate = new WierdDate(DaysPerMonth, MonthsPerYear, MaxYear);
public static readonly WierdDate MinWierdDate = new WierdDate();
private int days;
private WierdDate(int days)
{
this.days = days;
}
public WierdDate(int day, int month, int year)
{
if (day < 1 || day > DaysPerMonth)
throw new ArgumentOutOfRangeException("day", string.Format("day must be between 1 and {0}", DaysPerMonth));
if (month < 1 || month > MonthsPerYear)
throw new ArgumentOutOfRangeException("month", string.Format("month must be between 1 and {0}", MonthsPerYear));
if (year < 1 || year > MaxYear)
throw new ArgumentOutOfRangeException("year", string.Format("year must be between 1 and {0}", MaxYear));
days = ((year - 1) * DaysPerYear) + ((month - 1) * DaysPerMonth) + (day - 1);
}
public static bool operator ==(WierdDate wierdDateA, WierdDate wierdDateB)
{
return wierdDateA.days == wierdDateB.days;
}
public static bool operator !=(WierdDate wierdDateA, WierdDate wierdDateB)
{
return !(wierdDateA == wierdDateB);
}
public static WierdDate operator ++(WierdDate wierdDate)
{
return wierdDate + 1;
}
public static WierdDate operator --(WierdDate wierdDate)
{
return wierdDate - 1;
}
public static WierdDate operator +(WierdDate wierdDate, int value)
{
return new WierdDate(wierdDate.days + value);
}
public static WierdDate operator -(WierdDate wierdDate, int value)
{
return new WierdDate(wierdDate.days - value);
}
public static bool operator >(WierdDate wierdDateA, WierdDate wierdDateB)
{
return wierdDateA.days > wierdDateB.days;
}
public static bool operator <(WierdDate wierdDateA, WierdDate wierdDateB)
{
return wierdDateA.days < wierdDateB.days;
}
public static bool operator >=(WierdDate wierdDateA, WierdDate wierdDateB)
{
return wierdDateA.days >= wierdDateB.days;
}
public static bool operator <=(WierdDate wierdDateA, WierdDate wierdDateB)
{
return wierdDateA.days <= wierdDateB.days;
}
public int Day
{
get { return (TotalDays - (TotalMonths * DaysPerMonth)) + 1; }
}
public int Month
{
get { return (TotalMonths - (TotalYears * MonthsPerYear)) + 1; }
}
public int Year
{
get { return TotalYears + 1; }
}
public int TotalDays
{
get { return days; }
}
public int TotalMonths
{
get { return (days / DaysPerMonth); }
}
public int TotalYears
{
get { return days / DaysPerYear; }
}
public int GetDaySpan(WierdDate other)
{
return other.days - days;
}
public override bool Equals(object obj)
{
return obj is WierdDate && ((WierdDate)obj) == this;
}
public override int GetHashCode()
{
return days.GetHashCode();
}
public override string ToString()
{
return string.Format("Day: {0}, Month: {1}, Year: {2}", Day, Month, Year);
}
}