Esempio n. 1
0
        public ITimeSlot Build(string descriptor)
        {
            descriptor = Descriptor.Translate(descriptor, _culture, CultureInfo.InvariantCulture);

            if (descriptor == Descriptor.Keywords.Always)
            {
                return(new AlwaysTimeSlot());
            }
            if (descriptor == Descriptor.Keywords.Never)
            {
                return(new NeverTimeSlot());
            }

            var words = descriptor.Split(' ').ToList();

            if (words.Contains(Descriptor.Keywords.Intersecting) ||
                words.Contains(Descriptor.Keywords.Or) ||
                words.Contains(Descriptor.Keywords.But) ||
                descriptor.Contains("(") ||
                descriptor.Contains(")"))
            {
                return(ComplexTimeSlot.FromDescriptor(descriptor, _aliasesProvider));
            }

            if (descriptor.StartsWith(Descriptor.Keywords.Between))
            {
                return(AbsoluteTimeSlot.FromDescriptor(descriptor));
            }
            if (descriptor.StartsWith(":"))
            {
                return(AliasTimeSlot.FromDescriptor(descriptor, _aliasesProvider));
            }
            if (!descriptor.StartsWith(Descriptor.Keywords.Every))
            {
                throw new InvalidDescriptorException(descriptor);
            }

            if (words.Contains(Descriptor.Keywords.Day))
            {
                return(DailyRecurrentTimeSlot.FromDescriptor(descriptor));
            }
            if (words.Contains(Descriptor.Keywords.Month))
            {
                return(MonthlyRecurrentTimeSlot.FromDescriptor(descriptor));
            }
            if (words.Contains(Descriptor.Keywords.Week))
            {
                return(WeeklyRecurrentTimeSlot.FromDescriptor(descriptor));
            }
            if (words.Contains(Descriptor.Keywords.Year))
            {
                return(YearlyRecurrentTimeSlot.FromDescriptor(descriptor));
            }

            throw new InvalidDescriptorException(descriptor);
        }
Esempio n. 2
0
        public void TestContains()
        {
            {
                var rule = new YearlyRecurrentTimeSlot(1, 1, 31, 12);
                Assert.IsTrue(rule.Contains(System.DateTime.Now));
            }

            {
                var rule = new YearlyRecurrentTimeSlot(1, 1, new TimeSpan(12, 36, 59), 31, 12, new TimeSpan(23, 59, 9));
                Assert.IsFalse(rule.Contains(new System.DateTime(1, 1, 1, 12, 35, 45)));
            }
        }
Esempio n. 3
0
        public void TestToString()
        {
            {
                var          proof = new YearlyRecurrentTimeSlot(11, 12, new TimeSpan(0), 13, 12, new TimeSpan(0));
                const string str   = "EVERY YEAR BETWEEN 11/12 00:00 AND 13/12 00:00";

                Assert.AreEqual(str, proof.ToString());
            }

            {
                var          proof = new YearlyRecurrentTimeSlot(11, 12, new TimeSpan(8, 30, 0), 13, 12, new TimeSpan(9, 15, 0));
                const string str   = "EVERY YEAR BETWEEN 11/12 08:30 AND 13/12 09:15";

                Assert.AreEqual(str, proof.ToString());
            }
        }
Esempio n. 4
0
        public void TestBuilder()
        {
            {
                var          proof   = new YearlyRecurrentTimeSlot(11, 12, 13, 12);
                const string str     = "EVERY YEAR BETWEEN 11/12 00:00 AND 13/12 23:59";
                var          builded = new TimeSlotBuilder(NoAliasProvider).Build(str);

                Assert.AreEqual(proof, builded);
            }

            {
                var          proof   = new YearlyRecurrentTimeSlot(11, 12, new TimeSpan(8, 30, 0), 13, 12, new TimeSpan(9, 15, 0));
                const string str     = "EVERY YEAR BETWEEN 11/12 08:30 AND 13/12 09:15";
                var          builded = new TimeSlotBuilder(NoAliasProvider).Build(str);

                Assert.AreEqual(proof, builded);
            }
        }