【发布时间】:2021-01-23 01:16:50
【问题描述】:
我有一个数据库,其中包含基于周数(ISO8601 定义)的产品几年的价格值。
现在我需要按月汇总这些数据,但还要考虑每个月一周的权重。例如,如果当月一周只有 3 天,我需要给本周赋予 3/7 的权重,因此价格值为 (3/7)*price。
我已经开始编写此代码,但我完全不知道如何继续。 有没有一种简单的方法可以做到这一点,或者我需要逐个循环遍历所有年月,然后以这种方式计算重量? (可能直接使用 LINQ 或 SQL)
此外,我无法获得我正在循环的月份中包含的周数(ISO-8601 定义,1 到 53)
例如,在 2021 年 1 月,我们有几个星期:
- 2020 年 53 月(部分,仅 3 天)
- 2021 年 01 月
- 2021 年 02 月
- 2021 年 03 月 等等。
public static decimal GetWeightedMonthlyPrice(int year, int week, int month, decimal priceValue)
{
DateTime startDate = FirstDateOfWeekISO8601(year,week);
//get weight of the week in the month
int daysInTheMonth=0;
for(int i=0;i<7;i++){
if(startDate.AddDays(i).Month==month){
daysInTheMonth++;
}
}
decimal weightedPrice= (daysInTheMonth/7m)*priceValue; //the weighted price for the current month
return weightedPrice;
}
和
public static DateTime FirstDateOfWeekISO8601(int year, int weekOfYear)
{
DateTime jan1 = new DateTime(year, 1, 1);
int daysOffset = DayOfWeek.Thursday - jan1.DayOfWeek;
DateTime firstThursday = jan1.AddDays(daysOffset);
var cal = CultureInfo.CurrentCulture.Calendar;
int firstWeek = cal.GetWeekOfYear(firstThursday, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
var weekNum = weekOfYear;
if (firstWeek == 1)
{
weekNum -= 1;
}
var result = firstThursday.AddDays(weekNum * 7);
return result.AddDays(-3);
}
【问题讨论】:
-
我已经看到一个错误:
(daysInTheMonth/7)是一个整数除法。你想要(daysInTheMonth/7m),否则8/7 = 1,或者你可以直接daysInTheMonth * priceValue / 7(先乘再除),priceValue是decimal,所以乘法有保证的提升。 -
@xanatos 谢谢纠正。 (这里的代码是动态写的,VS中还没有。我需要找到正确的算法)
-
我看到了另一个问题... 一个跨度为 1 月底和 2 月初的一周...您会在 1 月计算一块,在 2 月计算一块...但是您的方法有输入的月份,但无法与调用者沟通同一周必须在下个月“重复使用”。可能将
month设置为ref以便方法可以更改它。 -
@xantos,是的,这正是我在我的问题中要问的,获取一个月中的部分天数以计算周的重量。它们可以在开头,也可以在结尾。我怎样才能得到它们?