Note that there are some explanatory texts on larger screens.

plurals
  1. POC#: Edge Cases when removing a subset of a day from a TimeSpan on a daily basis
    primarykey
    data
    text
    <p>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?</p> <p>ETA: I've currently got a set-up that looks like this:</p> <pre><code>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); } } </code></pre> <p>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.</p> <pre><code>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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload