Vicente J. Botet Escriba wrote:
Le 07/05/13 12:46, Vicente J. Botet Escriba a écrit :
I would like to discuss about some alternatives to take in account the following use case "Reset a date if it resulting date is valid and report if this succeeds" when we have two axes of variation on the date design.
Let me add two parameters to the possible date class. * validity: unchecked/checked * representation: serial, ymd(calendar), iso_ordinal, iso_week...
date
is an unchecked serial date. It corresponds +/- to the N3344 date suggestion. date is a checked ymd date. It corresponds +/- to the H.H 2011 date proposal. WARNING !!! I'm not saying that a template class is a good thing, it is just to state clearly the features of the date.
N3344 suggest a date::set_if_valid_date function.
date
ud=...; if (ud.set_if_valid_date(y,m,d)) { // xx } else { // yy } But if we want to do as for the date constructors we will need multiple overloads again for this function :(.
That possibly modifies ud. Spelling it "try_set" will be clearer
(and shorter!). The benefit of this approach is that the
checking is not wasted; the result is stored in ud. The downside
is that ud is still unchecked, so the checked characteristic is
lost except from context.
Thus, creating a date
As show on this ML serial+unchecked dates validation could have some surprises. We can get the same service building a ymd date that can provide a way to be validated.
//------------- // Validity check done when is_valid() is called on ymd_date.
date
dd=...; date yd(y,m,d); if (yd.is_valid()) { // CHECK dd = yd; // xx } else { // yy }
That should probably be in addition to try_set(). That is, both have their uses, so choosing between them is problematic.
Instead of using explicitly date
we can use a make_essence() factory that returns some kind of tuple essence_date that is not a date, but its essence. It contains everything needed to build a date.
That sounds like Howard's ymd struct idea. The latter name is certainly more convenient and specific than "essence". Nevertheless, I understand that you're trying to be abstract here.
This essence should be implicitly convertible to any date. The make_essence_date() factory supports all the different orders of parameters.
Presumably, that also would include day-of-year, week-of-year, and other, similar constructs.
The operator/() expression would also create an essence date.
// -------------- //No validity check is done.
date
ud = make_essence(y,m,d); date ud1 = m/d/y;
If s/make_essense/make_date/, that looks very workable.
This essence dates support date validity, that is provides a is_valid() function.
// -------------- // Validity check done when is_valid() is called on essence.
date
ud; auto d = y/m/d; if (d.is_valid()) { // CHECK ud = d; // xx } else { // yy } //-------------- // Validity check done at construction of cd.
serial_date<checked> cd = make_essence(m,d,y); //serial_date<checked> cd = m/d/y;
That makes sense. serial_date<checked>(essence) can call essence::is_valid(), so the validity checking logic is only one place.
Summary: IMO Having checked and unchecked dates with several representations could help to make dates interface more precise and concise. Checked dates are always valid. Dates with a specific representations would provide only, as suggested by Howard, the functions that are efficient. The use of essence dates factories simplify the interface of all the date classes construction. Only 1 constructor by representation needed independent of the parameters order
date(essence<ymd> &&); date(essence<serial> &&); date(essence
&&); date(essence &&); After doing some trials, it seems that the essence essence<T> class is equivalent to date
:).
Here you've taken an unexpected turn, though looking back at the
options you mentioned for the representation axis, it shouldn't
have been unexpected. I was thinking your make_essence() would
take various arguments, including day-of-year and similar special
cases, convert them to ymd values, and the various date classes
would construct from that information.
ymd is likely not the best intermediate format of the information
for every representation. A date