I'd prefer to not have DayOfWeek in date-time entities at all.
Week and DayOfWeek are calendar concepts. Any decision we would take
does not respond to all possible situations.
.NET BCL designers made many "practical" design decisions, and as a
result we have too much unclear responsibilities in one class
System.DateTime. Work with such classes involves discussions: "use
this property, but only if you have that assumption, instead use
another property" etc.
I think it would be better to separate concerns. Connection between
LocalDateTime and calendar can be established via some extension
method in the future.
Oh, sorry. I should definitely look at the source code before
answering.
LocalDateTime by concept is just "Instant+Calendar". Let's leave it as
is
My point was to not making any assumptions regarding what kind of
calendar is under LocalDateTime. We can't change the world and make
things less difficult than there are.
Moreover, i'd move all properties which are wrappers over calendar
fields to extensions, Now it makes sense to add some "practical"
extensions, for example together with extension property
public int DayOfWeek(this CalendarSystem calendar)
{
get { return
calendar.Fields.DayOfWeek.GetValue(localInstant); }
}
we can add
public ISODayOfWeek ISODayOfWeek
{
get { (ISODayOfWeek)return
calendar.Fields.DayOfWeek.GetValue(localInstant); }
}
or better method that will throw exception for calendars with
different meaning of "day of week".
Later, it will be possible to add other extension methods to return
something like HermeticLunarDayOfWeek, not touching original concept
of "localInstant+calendar"
Enumeration values and extesnions for each calendar should be in the
separate namespace, if user want to work with ISO, he should
intentionally use corresponded namespace, extensions and enumerations.
Another option: add extensions to calendar itself.
Then usage pattern would be
localDateTime.Calendar. IsoDayOfWeek (possibly wrong result, don't
like it)
or
localDateTime.Calendar.GetISODayOfWeek (with exception for wrong
calendar)
or
((ISOCalendar)localDateTime.Calendar).IsoDayOfWeek (with casting
exception for wrong calendar).
I don't know what is better, but definitely don't like any
assumptions.
To bring back The Configurator's problem, not everyone agrees what the
first day of the week is and we could handle this through variations
(sub-classes or instances with different settings) of the
GregorianCalendar class. To do this we would have to not hard-code the
days of week values but define them on the calendar. If I could ask
for a calendar that has Sunday as the first day of the week and then
pass it around I would be able to easily change it to one that uses
Monday as the first day of the week without having to recompile or
change the sorting code or anything. It would just work ;-).
I know that you want to have the "common" case handled simply but I
would argue that the "common" case that you think exists doesn't. The
basics of the Gregorian Calendar are probably the most common in usage
(the months) but the little details (days of week) are where all of
the problems with all of the other date/time packages occur.
Specifically I was thinking that if I ask for a calendar where Sunday
is the first day of the week and then ask for the days of the week I
will get them in the order { Sunday, Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday } and if I asked for a Monday start then
the order would be { Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday, Sunday }. And if I compared the days using a week day
comparer they would be in that order. It would be easiest for any code
that uses the calendar to be able to assume that the days are always
in ascending order by int value. Having written calendar display code
I know what a pain it is to have to keep track of the fact that with
one setting the values are { 0, 1, 2, 3, 4, 5, 6 } and with another
setting the order is { 1, 2, 3, 4, 5, 6, 0}.
Besides as was previously mentioned some calendars may have more or
less than 7 days in a week. If we have hard coded enums then people
will use them and code will be harder to localize. But it you really
feel that enums are the way to go then I still say they should be a
part of the GregorianCalendar(System) object. At least then a
programmer will know that they are using calendar specific values.
I would think that you have at least two week thingies in play here:* A week concept, saying that "weeks go from sunday to saturday" or "weeks go from monday to sunday"* A week instance, containing the 3rd week of 2010 according to a specific calendarThe first is more of a set of properties, sort of like CultureInfo.The second is a fixed period of time.