コード例 #1
0
        /// <inheritdoc />
        public int GetWeekYear(LocalDate date)
        {
            YearMonthDay           yearMonthDay           = date.YearMonthDay;
            YearMonthDayCalculator yearMonthDayCalculator = date.Calendar.YearMonthDayCalculator;

            unchecked
            {
                // Let's guess that it's in the same week year as calendar year, and check that.
                int calendarYear    = yearMonthDay.Year;
                int startOfWeekYear = GetWeekYearDaysSinceEpoch(yearMonthDayCalculator, calendarYear);
                int daysSinceEpoch  = yearMonthDayCalculator.GetDaysSinceEpoch(yearMonthDay);
                if (daysSinceEpoch < startOfWeekYear)
                {
                    // No, the week-year hadn't started yet. For example, we've been given January 1st 2011...
                    // and the first week of week-year 2011 starts on January 3rd 2011. Therefore the local instant
                    // must belong to the last week of the previous week-year.
                    return(calendarYear - 1);
                }

                // By now, we know it's either calendarYear or calendarYear + 1. Check using the number of
                // weeks in the year. Note that this will fetch the start of the calendar year and the week year
                // again, so could be optimized by copying some logic here - but only when we find we need to.
                int weeksInWeekYear = GetWeeksInWeekYear(calendarYear, date.Calendar);

                // We assume that even for the maximum year, we've got just about enough leeway to get to the
                // start of the week year. (If not, we should adjust the maximum.)
                int startOfNextWeekYear = startOfWeekYear + weeksInWeekYear * 7;
                return(daysSinceEpoch < startOfNextWeekYear ? calendarYear : calendarYear + 1);
            }
        }
コード例 #2
0
        /// <inheritdoc />
        public int GetWeekOfWeekYear(LocalDate date)
        {
            YearMonthDay           yearMonthDay           = date.YearMonthDay;
            YearMonthDayCalculator yearMonthDayCalculator = date.Calendar.YearMonthDayCalculator;
            // This is a bit inefficient, as we'll be converting forms several times. However, it's
            // understandable... we might want to optimize in the future if it's reported as a bottleneck.
            int weekYear               = GetWeekYear(date);
            int startOfWeekYear        = GetWeekYearDaysSinceEpoch(yearMonthDayCalculator, weekYear);
            int daysSinceEpoch         = yearMonthDayCalculator.GetDaysSinceEpoch(yearMonthDay);
            int zeroBasedDayOfWeekYear = daysSinceEpoch - startOfWeekYear;
            int zeroBasedWeek          = zeroBasedDayOfWeekYear / 7;

            return(zeroBasedWeek + 1);
        }
コード例 #3
0
        /// <inheritdoc />
        public int GetWeekOfWeekYear(LocalDate date)
        {
            YearMonthDay           yearMonthDay           = date.YearMonthDay;
            YearMonthDayCalculator yearMonthDayCalculator = date.Calendar.YearMonthDayCalculator;
            // This is a bit inefficient, as we'll be converting forms several times. However, it's
            // understandable... we might want to optimize in the future if it's reported as a bottleneck.
            int weekYear = GetWeekYear(date);
            // Even if this is before the *real* start of the week year due to the rule
            // having short weeks, that doesn't change the week-of-week-year, as we've definitely
            // got the right week-year to start with.
            int startOfWeekYear        = GetWeekYearDaysSinceEpoch(yearMonthDayCalculator, weekYear);
            int daysSinceEpoch         = yearMonthDayCalculator.GetDaysSinceEpoch(yearMonthDay);
            int zeroBasedDayOfWeekYear = daysSinceEpoch - startOfWeekYear;
            int zeroBasedWeek          = zeroBasedDayOfWeekYear / 7;

            return(zeroBasedWeek + 1);
        }
コード例 #4
0
 internal int GetDayOfWeek([Trusted] YearMonthDay yearMonthDay) => GetDayOfWeek(yearMonthDayCalculator.GetDaysSinceEpoch(yearMonthDay));