views:

41

answers:

2

I have a strange business requirement to output dates as 01:00 through 24:00, instead of the usual 00:00 through 23:00. It is really a crazy requirement, but unfortunately I don't think I can avoid it.

This will be a configuration option in our software, so I'll still need to support the normal 00-23 as well, so I'm hoping I can do this somehow with format strings. But I'm also considering using a regex to post-process the result string if that makes it easier.

Thanks!

+3  A: 

Extend .NET by creating your own format provider: http://msdn.microsoft.com/en-us/library/0asazeez(VS.71).aspx

Pierre 303
I don't think this is a particularly good idea. The responsibility of IFormatProvider is to format based on a culture, not business rules. While it would work, it is warping the responsibility of that interface and the corresponding overload. Since this is a business concern, the logic should go in the model.
codekaizen
@code - I disagree. He basically is creating a custom culture, not defining business logic (he's using standard `DateTime` behavior, just displaying the value differently to the user). This is exactly what `IFormatProvider` is for.
Jon B
@Jon B - How is custom business logic a culture? He specifically states that this is a business requirement.
codekaizen
@code business logic != business requirement. The business require is that dates be displayed in a specific culture. It could just as easily be EN-US. It happens to be a non-standard "culture" where times run from 1-24.
Jon B
Having a format provider will also help a lot in conversions. For example converting from a string "24:03:00".
Pierre 303
Thanks for the suggestions. I may look into creating a custom format provider in the future if this option becomes more widely used in my application.
randbrown
+2  A: 

This sounds like a good candidate for a wrapper class which is used to format the DateTime according to business rules:

public class BusinessDateTimeFormatter
{
    public BusinessDateTimeFormatter(DateTime dateTime)
    {
        _dateTime = dateTime;
    }

    public override string ToString()
    {
        return String.Format("{0} {1}:{2}", _dateTime.Date, _dateTime.Hour + 1, _dateTime.Minute);
    }
}

Alternatively, and perhaps even more correctly, you can create a type which represents the business's idea of a DateTime. In object-oriented design, this replacement of primitive types with custom types happens frequently, since the domain to be modeled has special restrictions just like this one that you describe.

codekaizen
A customer formatter for a date sounds like the way to go for me. You are encapsulating it and from the name its clear why its been done
JonWillis
I ended up doing something along the lines of this suggestion - a simple business class to handle the formatting. I chose this approach because it's currently only required in one area of my application so it was this simplest solution for now. If it becomes required in other feature areas of the app, I may consider implementing a more robust custom formatter or something like that.
randbrown