Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing serial terms using the complex bit masks

Tags:

c#

I need to make a method for displaying serial terms based on the number associated with these days.

So, I base myself for each day (term), set a specific number.

Sun = 1, Mon = 2, Tues = 4, Wed = 8, Thu = 16, Fri = 32, Sat = 64

If the user has chosen all eg working days, Mon - Fri = 62, or weekend Sat - Sun = 65.

I have to start the method to show each week for those days of that term.

current code to display the term in the calendar

        var terminDates = dataAccess.GetTerminDate(personal.LPE_Nr, date, DateTime.DaysInMonth(date.Year, date.Month));

        for (int i = 0; i < terminDates.Count; i++)
        {
            T_TERMINE _TERMINE = new T_TERMINE();
            List<T_TERMINE> terminList = dataAccess.GetListTermins(personal.LPE_Nr, terminDates[i]);
            string serId = null;
            foreach (var termin in terminList)
            {
                _TERMINE.TER_Betreff = termin.TER_Betreff;
                _TERMINE.TER_ID = termin.TER_ID;
                serId = termin.TER_TRS_ID;
            }
            List<T_TERMINSERIE> serieTermins = dataAccess.GetSerieTermins(serId);
            T_TERMINSERIE _TERMINSERIE = new T_TERMINSERIE();
            foreach (var ser_termin in serieTermins)
            {
                _TERMINSERIE.TRS_StartZeit = ser_termin.TRS_StartZeit;
                _TERMINSERIE.TRS_EndZeit = ser_termin.TRS_EndZeit;
                _TERMINSERIE.TRS_TagWoche = ser_termin.TRS_TagWoche; //(16)here I get the number of the bit of the mask
                _TERMINSERIE.TRS_StartDatum = ser_termin.TRS_StartDatum;
            }
           /* how now to include method to make that custom event appear 
            whenever the method renders, me now CustomEvent is only 
           displayed for the first time, how to include it and Event for each time the method finds that day in a single week?*/
           //FindNextDay()?
            CustomEvent ce = new CustomEvent() { IgnoreTimeComponent = true,
                Date = terminDates[i],
                EventText = _TERMINE.TER_Betreff,
                EventColor = Color.BlueViolet };
            if (!generatedEvents.Any(x => x.Date == ce.Date))
            {
                calendar1.AddEvent(ce);
                generatedEvents.Add(ce);
            }
        }

Now the most important is the method that will be based on the number I will be reading from the base, I mean numbers in the enum. Repeat this term for every week.

   [Flags]
        enum DaysBitMask { Sun = 1, Mon = 2, Tues = 4, Wed = 8, Thu = 16, Fri = 32, Sat = 64 }
        static DayOfWeek FindNextDay(DaysBitMask mask, DayOfWeek currentDay)
        {
            DayOfWeek dayCursor = currentDay;
            int counter = 0;
            do
            {
                int cursorMask = 0x01 << (int)dayCursor;
                int operationResult = (cursorMask & (int)mask);
                if (operationResult > 0)
                    return dayCursor;

                dayCursor = (DayOfWeek)(((int)dayCursor + 1) % 7);
                counter++;
            }
            while (counter < 7);

            throw new InvalidOperationException("Wrong flow. We shouldn't be here");
        }

This is my currently designed code, which does not work, please if you can correct my method "FindNextDay" and tell me how to apply it to get to the correct display of the term. Thank you all for the time.

like image 837
lasta Avatar asked Nov 06 '22 17:11

lasta


1 Answers

Try the code below

    [Flags]
    enum DaysBitMask { Sun = 1, Mon = 2, Tues = 4, Wed = 8, Thu = 16, Fri = 32, Sat = 64 }

    static DayOfWeek FindNextDay(DaysBitMask mask, DayOfWeek currentDay)
    {
        DayOfWeek dayCursor = currentDay;

        for (int counter = 0; counter < 7; counter++)
        {
            int cursorMask = 0x01 << (int)dayCursor;
            int operationResult = (cursorMask & (int)mask);
            if (operationResult > 0)
                return dayCursor;

            dayCursor = (DayOfWeek)(((int)dayCursor + 1) % 7);
        }

        throw new InvalidOperationException("We shouldn't be here");
    }
like image 79
Sergey Prosin Avatar answered Nov 15 '22 13:11

Sergey Prosin