Fan.Sys.DateTime.weekdayInMonth C# (CSharp) Method

weekdayInMonth() public static method

public static weekdayInMonth ( int year, int mon, int weekday, int pos ) : int
year int
mon int
weekday int
pos int
return int
        public static int weekdayInMonth(int year, int mon, int weekday, int pos)
        {
            // argument checking
              checkYear(year);
              if (pos == 0) throw ArgErr.make("Pos is zero").val;

              // compute the weekday of the 1st of this month (0-6)
              int fw = firstWeekday(year, mon);

              // get number of days in this month
              int numDays = numDaysInMonth(year, mon);

              if (pos > 0)
              {
            int day = weekday - fw + 1;
            if (day <= 0) day = 8 - fw + weekday;
            day += (pos-1)*7;
            if (day > numDays) throw ArgErr.make("Pos out of range " + pos).val;
            return day;
              }
              else
              {
            int lastWeekday = (fw + numDays - 1) % 7;
            int off = lastWeekday - weekday;
            if (off < 0) off = 7 + off;
            off -= (pos+1)*7;
            int day = numDays - off;
            if (day < 1) throw ArgErr.make("Pos out of range " + pos).val;
            return day;
              }
        }

Same methods

DateTime::weekdayInMonth ( long year, Month mon, Weekday weekday, long pos ) : long

Usage Example

Example #1
0
        /// <summary>
        /// Compare on day.
        ///     'd'  5        the fifth of the month
        ///     'l'  lastSun  the last Sunday in the month
        ///     'l'  lastMon  the last Monday in the month
        ///     '>'  Sun>=8   first Sunday on or after the eighth
        ///     '<'  Sun<=25  last Sunday on or before the 25th (not used)
        /// </summary>
        static int compareOnDay(Rule rule, DstTime x, int year, int mon, int day)
        {
            // universal atTime might push us into the previous day
            if (x.atMode == 'u' && rule.offset + x.atTime < 0)
            {
                ++day;
            }

            switch (x.onMode)
            {
            case (byte)'d':
                if (x.onDay < day)
                {
                    return(-1);
                }
                if (x.onDay > day)
                {
                    return(+1);
                }
                return(0);

            case (byte)'l':
                int last = DateTime.weekdayInMonth(year, mon, x.onWeekday, -1);
                if (last < day)
                {
                    return(-1);
                }
                if (last > day)
                {
                    return(+1);
                }
                return(0);

            case (byte)'>':
                int start = DateTime.weekdayInMonth(year, mon, x.onWeekday, 1);
                while (start < x.onDay)
                {
                    start += 7;
                }
                if (start < day)
                {
                    return(-1);
                }
                if (start > day)
                {
                    return(+1);
                }
                return(0);

            default:
                throw new Exception("" + (char)x.onMode);
            }
        }