Created
March 19, 2018 12:43
-
-
Save acid-chicken/83e6f5f63b86f205d4004c3bf9f7deec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Parking | |
{ | |
class Program | |
{ | |
static readonly TimeSpan Delay = new TimeSpan(4, 0, 0); | |
static readonly TimeSpan Span = new TimeSpan(9, 0, 0); | |
static readonly TimeSpan OneDay = new TimeSpan(1, 0, 0, 0); | |
static void Main(string[] args) | |
{ | |
var intime = new DateTime(2018, 1, 1, 0, 0, 0); | |
var outtime = new DateTime(2018, 1, 5, 21, 30, 0); | |
Console.WriteLine($"{intime} ~ {outtime} -> {GetNightTime(intime, outtime)}"); | |
} | |
static TimeSpan GetNightTime(DateTime intime, DateTime outtime) | |
{ | |
if (intime <= outtime) | |
{ | |
intime += Delay; | |
intime = intime.TimeOfDay > Span ? intime.Date.AddDays(1.0) : intime; | |
outtime += Delay; | |
outtime = outtime.TimeOfDay > Span ? outtime.Date.AddDays(1.0) : outtime; | |
return Span - intime.TimeOfDay + (outtime.Date - intime.Date - OneDay).Days * Span + outtime.TimeOfDay; | |
} | |
else | |
{ | |
throw new ArgumentException(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment