Skip to content

Instantly share code, notes, and snippets.

@yemrekeskin
Last active December 23, 2015 12:19
Show Gist options
  • Save yemrekeskin/6634241 to your computer and use it in GitHub Desktop.
Save yemrekeskin/6634241 to your computer and use it in GitHub Desktop.
public class Task
{
// Action based :D
public Action Action { get; set; }
public int? AbsoluteMinute { get; set; }
public int? AbsoluteHour { get; set; }
public int? SlidingMinute { get; set; }
public int? SlidingHour { get; set; }
private bool IsAbsolute { get { return AbsoluteMinute.HasValue; } }
private bool IsSliding { get { return SlidingMinute.HasValue; } }
private Func<DateTime> Watch;
public Task(Func<DateTime> currentTimeWatch = null)
{
this.Watch = currentTimeWatch ?? new Func<DateTime>(Helper.GetCurrentDatetime);
}
private DateTime last;
public bool IsTime()
{
var now = Normalize(Watch());
if (last == default(DateTime))
last = now;
bool risp = false;
if (IsAbsolute)
{
var absoluteTime = new DateTime(now.Year, now.Month, now.Day, AbsoluteHour.HasValue ? AbsoluteHour.Value : now.Hour, AbsoluteMinute.Value, 0);
risp = last < absoluteTime && absoluteTime <= now;
}
else if (IsSliding)
{
var minutes = (SlidingHour.HasValue ? SlidingHour.Value * 60 : 0) + SlidingMinute.Value;
risp = (now - last).TotalMinutes >= minutes;
}
if (risp)
last = now;
return risp;
}
private DateTime Normalize(DateTime arg)
{
return new DateTime(arg.Year, arg.Month, arg.Day, arg.Hour, arg.Minute, arg.Second);
}
}
// Helper Class
public static class Helper
{
public static DateTime GetCurrentDatetime()
{
// real datetime or custom system's datetime
return DateTime.Now;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment