using System.Globalization;
public static String StringBetween(String str, String strFrom, String strTo)
int pFrom = str.IndexOf(strFrom) + strFrom.Length;
int pTo = str.LastIndexOf(strTo);
return str.Substring(pFrom, pTo - pFrom);
public static void Main()
string rrule = "DTSTART:20190729T230000Z\nRRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR,SA,SU;UNTIL=20190730T205959Z";
string timezone = "Pacific Standard Time";
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneConverter.TZConvert.WindowsToIana(timezone));
DateTime start = TimeZoneInfo.ConvertTimeFromUtc(DateTime.ParseExact(StringBetween(rrule, "DTSTART:", "\nRRULE:"), "yyyyMMdd'T'HHmmss'Z'", CultureInfo.InvariantCulture, DateTimeStyles.None), tz);
DateTime until = TimeZoneInfo.ConvertTimeFromUtc(DateTime.ParseExact(StringBetween(rrule, "UNTIL=", "Z"), "yyyyMMdd'T'HHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None), tz);
if (until.TimeOfDay < start.TimeOfDay)
until = until.AddDays(1);
DateTime end = DateTime.Parse(start.ToShortDateString() + " " + until.ToShortTimeString());
DateTime today = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz);
Recurrence recur = new Recurrence(StringBetween(rrule, "\nRRULE:", ""));
recur.StartDateTime = start;
recur.RecurUntil = until;
Console.WriteLine(recur.ToDescription());
Console.WriteLine("It is currently " + today);
Console.WriteLine("Start on " + start);
Console.WriteLine("Until " + until);
Console.WriteLine("End on " + end);
if (recur.OccursOn(today, false) && today >= start && today < end)
Console.WriteLine("Ad is scheduled");
Console.WriteLine("Ad is NOT scheduled");