I want to create some class that can calculate absolute or relative time period.
Meaning: the constructor will get either time period and units ( 3 weeks ) or two DateTime start and finish time. I have wrote the following code :
public class TimePeriod
{
public State TimePerriodState { get; private set; }
public RelativeTime RelativeTimePeriod { get; private set; }
public int UnitsOfTime { get; private set; }
public DateTime? StartTime { get; private set; }
public DateTime? EndTime { get; private set; }
public TimePeriod(RelativeTime relativeTime, int unitsOfTime)
{
TimePerriodState = State.Absolute;
RelativeTimePeriod = relativeTime;
UnitsOfTime = unitsOfTime;
}
public TimePeriod(DateTime startTime , DateTime endTime)
{
TimePerriodState = State.Relative;
StartTime = startTime;
EndTime = endTime;
}
public enum State
{
None,
Absolute,
Relative
}
public enum RelativeTime
{
None,
Hours,
Days,
Weeks,
Months,
Year
}
}
But I don’t like that the usage is base on the state.
In the end the data will appear in the UI as two different controls. Is there a better way of making the API little better? Maybe pass in the data and calculate the dateTime on the fly or some thing like that?
UPDATE :
the usage is relative to any time its being used. meaning enter a timespan is not possible. lets say we need fo have the relative time prior 10 days from now or fixed days from X to Y
The TimeSpan will be calculated in a different BL class (this is a POCO class)
You’re basically trying to store the same data twice.
You can define any period of time by either:
What you want to do is pick one way of storing it. If you want it the other way, write a function to calculate it. E.g: