Exemplo 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);
        }
Exemplo n.º 2
0
        public void TestContains()
        {
            {
                var rule = new AbsoluteTimeSlot(System.DateTime.Now, System.DateTime.MaxValue);
                Assert.IsTrue(rule.Contains(System.DateTime.Now));
            }

            {
                var rule = new AbsoluteTimeSlot(System.DateTime.MinValue, System.DateTime.Now.AddDays(-6));
                Assert.IsFalse(rule.Contains(System.DateTime.Now));
            }
        }
Exemplo n.º 3
0
        public void TestFrench()
        {
            {
                var          proof      = new AbsoluteTimeSlot(new System.DateTime(2015, 06, 12, 12, 21, 00), new System.DateTime(2015, 07, 03, 17, 04, 00));
                const string descriptor = "ENTRE 12/06/2015 12:21 ET 03/07/2015 17:04";
                Assert.AreEqual(proof, new TimeSlotBuilder(NoAliasProvider, new CultureInfo("fr")).Build(descriptor));
            }

            {
                var          proof      = new AbsoluteTimeSlot(new System.DateTime(2015, 06, 12, 12, 21, 55), new System.DateTime(2015, 06, 18));
                const string descriptor = "ENTRE 12/06/2015 12:21 ET 18/06/2015 00:00";
                Assert.AreEqual(proof, new TimeSlotBuilder(NoAliasProvider, new CultureInfo("fr")).Build(descriptor));
            }
        }
Exemplo n.º 4
0
        public void TestDescriptor()
        {
            {
                var          proof      = new AbsoluteTimeSlot(new System.DateTime(2015, 06, 12, 12, 21, 00), new System.DateTime(2015, 07, 03, 17, 04, 00));
                const string descriptor = "BETWEEN 12/06/2015 12:21 AND 03/07/2015 17:04";
                Assert.AreEqual(proof, new TimeSlotBuilder(NoAliasProvider).Build(descriptor));
            }

            {
                var          proof      = new AbsoluteTimeSlot(new System.DateTime(2015, 06, 12, 12, 21, 55), new System.DateTime(2015, 06, 18));
                const string descriptor = "BETWEEN 12/06/2015 12:21 AND 18/06/2015 00:00";
                Assert.AreEqual(proof, new TimeSlotBuilder(NoAliasProvider).Build(descriptor));
            }
        }