Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
CRON Question
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Miscellaneous
Thread ID:
01518445
Message ID:
01518457
Views:
55
>>>I need to implement a schedule-based process using a CRON. Anyone know of a function or any code where I can pass in a CRON and get back a bool if it's time to run?
>>
>>I thought CRON was a Unix daemon for task scheduling. What do you mean by 'pass in a CRON' ?
>
>It is a unix thing but for some reason we're using it here in our C# apps. So I have to use it.
>
>I want to pass in a CRON expression, ie "* * * * * " or something like it, and get back a bool if the expression evaluates to DateTime.Now or past DateTime.Now.

Out of boredom here's a starting point:
string sampleCronString = "1,3,7,12-15 18 * * *";
            string[] fields = sampleCronString.Split(' ');
            List<int> minutes = GetValues(fields[0],60);
            if (minutes.Contains(DateTime.Now.Minute))
            {
                //Possible match - go on and do the same for hours (call GetValues() again with fields[1] and '24'
            }
with
List<int> GetValues(string s,int itemcount)
        {
            List<int> list = new List<int>();

            List<string> items = s.Split(',').ToList();

            foreach (string s2 in items)
            {
                if (s2 == "*")
                {
                    //All items
                    for (int i = 1; i <= itemcount; i++ )
                    {
                        list.Add(i);
                    }
                }
                else if (s2.Contains('-'))
                {
                    //Range
                    string[] s3 = s2.Split('-');
                    int start =  Int32.Parse( s3[0]);
                    int end = Int32.Parse(s3[1]);
                    for (int i = start; i <= end; i++)
                    {
                        list.Add(i);
                    }
                }
                else
                {
                    //Single Item
                    list.Add(Int32.Parse(s2));
                }
            }
            return list;
        }
Needs a lot of work but....
Previous
Reply
Map
View

Click here to load this message in the networking platform