SmartDate Format Changed in CSLA 3.5

SmartDate Format Changed in CSLA 3.5

Old forum URL: forums.lhotka.net/forums/t/4687.aspx


rsargent posted on Monday, April 14, 2008

I noticed that SmartDate formatting has changed slightly in CSLA 3.5. Is this intentional?

I execute the following code in both versions of CSLA and get the following output (assuming 10:00 am on April 14th):

Code
SmartDate dt = new SmartDate(DateTime.Now);

string SmartDateFormat = string.Format("{0}", dt);
string SmartDateToString = dt.ToString();
string SmartDateToStringNull = dt.ToString(null);
Trace.WriteLine(SmartDateFormat);
Trace.WriteLine(SmartDateToString);
Trace.WriteLine(SmartDateToStringNull);

CSLA 3.0.x Output
4/14/2008
4/14/2008
4/14/2008 10:00:00 AM

CSLA 3.5.0 Output
4/14/2008 10:00:00 AM
4/14/2008
4/14/2008 10:00:00 AM
As you can see, dt.Format() and dt.Format(null) produce different output, but the output is consistent from CSLA 3.0 to 3.5.

However, the default string format has changed to include the time in CSLA 3.5.

Any comments?

Thanks

Richard Sargent

RockfordLhotka replied on Monday, April 14, 2008

The default format string didn't change.

However, SmartDate in 3.5 has much richer support for the cast operation - it can convert itself to/from quite a number of types.

Notice that your first test uses a format string, but without specifying a format. Odds are that pre-3.5 the SmartDate was converted to a string (and thus formatted), but in 3.5 I bet that string.Format() is not converting it to a string (it may be converting it to a DateTime?) and so you are seeing the difference.

SmartDate in 3.5 is a much more complete replication of DateTime than the 3.0 version - especially in terms of type conversion.

rsargent replied on Wednesday, April 16, 2008

>> The default format string didn't change.
Sure it did.

If I have a string.Format("{0}", dt) in my code, the output will be different when I use CSLA 3.5 instead of CSLA 3.0. This is a change in the CSLA default behavior for SmartDate. This may not be a "breaking" change, but I will need to change my code to make the output the same across versions.

>> Notice that your first test uses a format string, but without specifying a format. Odds are that pre-3.5 the SmartDate was converted to a string (and thus formatted), but in 3.5 I bet that string.Format() is not converting it to a string (it may be converting it to a DateTime?) and so you are seeing the difference.

The difference appears to be the implementation of IFormattable.ToString.

When I call
string.Format("{0}", dt) in CSLA 3.0, the SmartDate object uses the SmartDate.ToString() method which returns SmartDate.Text.

In CSLA 3.5,
IFormattable.ToString is called with a null format. SmartDate now uses SmartDate.ToString(null), which produces a different result.

To be
consistent, ToString() and ToString(null) should return the same string.

RockfordLhotka replied on Wednesday, April 16, 2008

rsargent:
>> The default format string didn't change.
Sure it did.

If I have a string.Format("{0}", dt) in my code, the output will be different when I use CSLA 3.5 instead of CSLA 3.0. This is a change in the CSLA default behavior for SmartDate. This may not be a "breaking" change, but I will need to change my code to make the output the same across versions.

No, the default format string didn't change. Look at the code - you can see it is still the same.

The fact that the format string isn't used in this new scenario is not the same as saying the format string has changed...

My post wasn't denying that there was a change - I was speculating about how/why the change might have come to be - and obviously it isn't because the format string changed.


rsargent:
>> Notice that your first test uses a format string, but without specifying a format. Odds are that pre-3.5 the SmartDate was converted to a string (and thus formatted), but in 3.5 I bet that string.Format() is not converting it to a string (it may be converting it to a DateTime?) and so you are seeing the difference.

The difference appears to be the implementation of IFormattable.ToString.

When I call
string.Format("{0}", dt) in CSLA 3.0, the SmartDate object uses the SmartDate.ToString() method which returns SmartDate.Text.

In CSLA 3.5,
IFormattable.ToString is called with a null format. SmartDate now uses SmartDate.ToString(null), which produces a different result.

To be
consistent, ToString() and ToString(null) should return the same string.

That is useful information, thank you. I think this is a bug in the implementation of ToString(string). I'll change the ToString(string) overload to check for a null format string, a change you'll see in 3.5.1.

rsargent replied on Tuesday, May 06, 2008

Thanks!

We just updated to Csla 3.5.1.0 and it works the same as 3.0.x.

Richard

Copyright (c) Marimer LLC