views:

29

answers:

1

Hi all,

I've got a method which takes some parameters and generates some XML to be sent to an [archaic] web service.

I need to include in the XML:

Date (yyyy-mm-dd)

Opening time (hh:mm)

Closing time (hh:mm)

Now in the past when I've had to provide a date/time separately, I've taken in a single DateTime parameter and formatted the date into one field and the time into another.

In this scenario, I've got a single date and 2 times of day.

My initial approach was 3 input fields - one Date, 2 timespans. This feels wrong as the timespans can cover more than a day.

Another idea was to take 2 dates - use one for the date field and then the times from both for the other fields. This feels wrong as the dates provided may actually be different days.

The last option I can think of is having a OpeningDateTime parameter and an OpenDuration timespan, however, since the opening/closing times are stored against day of the week (not date), the developer would need to calculate the timespans themselves - which seems silly as it breaks DRY principles. Not to mention that again the timespan can be > 1 day

In short, I've got 3 solutions which would work but none of which feels right - and the first 2 solutions would only pick up problems at run-time. I'd prefer it if the method definition was such that passing in invalid data was difficult (if not impossible) - This would then be seen at design time

Maybe I'm over-analysing but there must be a neater way to do this

Does anyone have a preferred method and if so, why?

+3  A: 

What you really want is a library which has types representing a "date" and a "time" separately... like Noda Time... but unfortunately that's not ready for production yet :( Basically .NET doesn't have a very rich set of chronological types :(

I think your initial idea of DateTime + 2 TimeSpans is the best idea at the moment. In particular, the fact that DateTime.TimeOfDay is a TimeSpan suggests that's about as appropriate a representation as you can get within the normal approach.

Jon Skeet
Thanks - I wasn't aware of Noda Time - I've just scanned the blog and it looks very interesting - We might consider when it's a reached a more stable build.
Basiclife
@Basiclife: I'm hoping to find some time to do a bit more work on it soon, now that C# in Depth is nearly done. I'm likely to gut the public API to make sure that we make the important use cases really, really straightforward. If you could email me the kind of operations you want to perform, that would be really useful.
Jon Skeet
Lol only just realised you instigated Noda Time - I like the approach you're taking of an exemplary OS .Net project
Basiclife
I'll email you some use cases this evening.
Basiclife