[API Proposal]: DateOnly support for Calendar
Background and motivation
There is a number of methods in Calendar class that could have overloads support DateOnly, in addition to DateTime. This would be nice for code where only discrete dates are dealt with.
Instead of having workarounds converting to and from DateTime it would be nice with properly implemented methods. All of the suggested methods could have default implementations to convert to and from DateTime so it is a non-breaking change.
API Proposal
namespace System.Globalization;
public abstract class Calendar
{
// Existing methods
// public DateTime AddDays(DateTime time, int days);
// public DateTime AddMonths(DateTime time, int months);
// public DateTime AddWeeks(DateTime time, int weeks);
// public DateTime AddYears(DateTime time, int years);
// public int GetDayOfMonth(DateTime time);
// public DayOfWeek GetDayOfWeek(DateTime time);
// public int GetDayOfYear(DateTime time);
// public int GetEra(DateTime time);
// public int GetMonth(DateTime time);
// public int GetWeekOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek);
// public int GetYear(DateTime time);
// public DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond);
// public DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era);
// New methods
public virtual DateOnly AddDays(DateOnly date, int days); // Would this be different from DateOnly.AddDays in any calendar? Can we skip it?
public virtual DateOnly AddMonths(DateOnly date, int months);
public virtual DateOnly AddWeeks(DateOnly date, int weeks);
public virtual DateOnly AddYears(DateOnly date, int years);
public virtual int GetDayOfMonth(DateOnly date);
public virtual DayOfWeek GetDayOfWeek(DateOnly date);
public virtual int GetDayOfYear(DateOnly date);;
public virtual int GetEra(DateOnly date);
public virtual int GetMonth(DateOnly date);
public virtual int GetWeekOfYear(DateOnly date, CalendarWeekRule rule, DayOfWeek firstDayOfWeek);
public virtual int GetYear(DateOnly date);
public virtual DateOnly ToDateOnly(int year, int month, int day);
public virtual DateOnly ToDateOnly(int year, int month, int day, int era);
}
API Usage
Alternative Designs
No response
Risks
None. Maybe increased API surface?
Tagging subscribers to this area: @dotnet/area-system-datetime See info in area-owners.md if you want to be subscribed.
Tagging subscribers to this area: @dotnet/area-system-globalization See info in area-owners.md if you want to be subscribed.