Question

I am working on an application that needs to set rules for periods of time. The company has different branches, each branch can set its own rules (i.e a branch starts work at 8.30 am, ends work at 17.30 pm, with 30 minutes pause for lunch; another branch start at 9.00, ends at 19.00 with 1 hour pause...)

So I need to define a class (let's call it WorkingDayDefinition for the moment) where start and end are not actually a DateTime, because they are not referred to any specific day in particular. At the moment the only option I see in C# is using Timespan for setting a duration from the beginning of the day, so that 8.30 pm would be TimeSpan(8,30,0) to be added to the Day part of whichever day.

Is this a best practice in C#?

I searched for third parties libraries that could help me, but so far my best bet is this one:

http://www.codeproject.com/Articles/168662/Time-Period-Library-for-NET

that is not strictly what I need

Was it helpful?

Solution

You could use Noda Time. It provides a LocalTime (see here):

LocalTime is an immutable struct representing a time of day, with no reference to a particular calendar, time zone or date.

For 8.30 you would do something like:

LocalTime openingAt = new LocalTime(8, 30);

OTHER TIPS

To me TimeSpam seems very suitable for what you want. It holds an interval of time, sometimes between two events, but in your case between the start of the day and the time you start/finish work. There is no reason I can think of not to use it just because the name might suggest this wasn't the original intention of the class. Plus it already integrates well with DateTimes for any time calculations you need to do later on down the road.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top