I'm trying to determine a TimeSpan (eg., time between logout and login), but with the caveat that a part of each day shouldn't be counted (eg., don't count the time between 7PM and 7AM on a given day). Removing that time from full days is easy enough, but what's the best way to check for parts of a day (eg., user logs out at 6PM and logs back in at 8PM the same day) and other edge cases?
ETA: I've currently got a set-up that looks like this:
public class HasProductiveHours
{
//These fields should represent an hour of the day (in 24-hour format)
// in which production should start or stop.
public int startProductiveHour;
public int endProductiveHour;
//Resource type enum
public ResourceType resourceType
//How often this resource is collected, in seconds
public float harvestTime
public HasProductiveHours (int startProductiveHour, int endProductiveHour, ResourceType resourceType, int harvestTime)
{
// Other creation-time things
this.startProductiveHour = startProductiveHour;
this.endProductiveHour = endProductiveHour;
this.resourceType = resourceType;
this.harvestTime = harvestTime;
TimeSimulator.Instance.SimulateElapsedTime (this);
}
}
Instances of this class and its children are simulating production of some resource every hour or so (depending on the resource). There's a system in place to simulate elapsed time since the user's last logout.
DateTime lastLogoutTime;
public void Logout ()
{
// Other logout things
lastLogoutTime = DateTime.Now;
}
public void SimulateElapsedTime (HasProductiveHours toSimulate)
{
DateTime currentTime = DateTime.Now;
TimeSpan timeSinceLogout = currentTime - lastLogout;
int unproductiveHours = Math.Abs (toSimulate.startProductionHour - toSimulate.endProductionHour) * timeSinceLogout.Days;
timeSinceLogout.Subtract (new TimeSpan (unproductiveHours, 0, 0);
double secondsElapsed = timeSinceLogout.TotalSeconds;
int harvestsElapsed = (int)(secondsElapsed / toSimulate.harvestTime);
PlayerData.Instance.AddResource (toSimulate.resourceType, harvestsElapsed);
}