Algorithm 计算给定日历月内每两周发生的事件数的算法

Algorithm 计算给定日历月内每两周发生的事件数的算法,algorithm,homebrew,Algorithm,Homebrew,我正在寻找最聪明的算法来确定特定系列中给定日历月内每两周发生的事件数 i、 e.鉴于该系列为“自2010年10月7日起每2个星期四”,因此“事件”将在(2010年10月7日、10月21日、11月4日、11月18日、12月2日、12月16日、12月30日……) 所以我想要的是一个函数 function(seriesDefinition, month) -> integer where: - seriesDefinition is some date that is a valid

我正在寻找最聪明的算法来确定特定系列中给定日历月内每两周发生的事件数

i、 e.鉴于该系列为“自2010年10月7日起每2个星期四”,因此“事件”将在(2010年10月7日、10月21日、11月4日、11月18日、12月2日、12月16日、12月30日……)

所以我想要的是一个函数

function(seriesDefinition, month) -> integer 

where:
    - seriesDefinition is some date that is a valid date in the series,
    - month indicates a month and a year
因此,它准确地表示:每月发生的事件数

示例:

每月(2010年10月7日、2010年10月)的每周事件数->2

每月(2010年10月7日、2010年11月)的每周事件数->2

每月(2010年10月7日、2010年12月)的每周事件数->3

请注意,10月有2个事件,11月有2个事件,但12月有3个事件


伪代码优先


除了潜在的通用库之外,我不想依赖于查找表、web服务调用或任何其他外部资源。例如,我认为我们可以有把握地假设大多数编程语言都有一些可用的日期操作函数。

处理日期时没有“聪明”的算法,只有一个乏味的算法。也就是说,你必须明确列出每个月有多少天,处理闰年(每四年一次,除100年外,除400年外),等等。

处理日期时没有“聪明”的算法,只有单调乏味的算法。也就是说,你必须明确列出每个月有多少天,处理闰年(每四年,除100年外,除400年外),等等。

好吧,因为你所说的算法通常的解决方案是计算从某个固定日期开始的天数。(天数加上上上个月累计天数加上年数*365减(年数/4)加(年数/100)减(年数/400))


有了这些,您就可以轻松地实现您需要的功能。您需要计算一周中的哪一天是1月1日。然后你就可以很容易地看到从那一天到2010年10月1日和2010年12月1日的“每两个星期四”的数量。它们的差异就是你要寻找的值。

好吧,因为你所说的算法,通常的解决方案是计算从某个固定日期开始的天数。(天数加上上上个月累计天数加上年数*365减(年数/4)加(年数/100)减(年数/400))

有了这些,您就可以轻松地实现您需要的功能。您需要计算一周中的哪一天是1月1日。然后你就可以很容易地看到从那一天到2010年10月1日和2010年12月1日的“每两个星期四”的数量。它们的不同之处在于您正在寻找的价值。

我的解决方案

Public Function NumberFortnightlyEventsInMonth(seriesDefinition As Date, month As String) As Integer

    Dim monthBeginDate As Date
    monthBeginDate = DateValue("1 " + month)
    Dim lastDateOfMonth  As Date
    lastDateOfMonth = DateAdd("d", -1, DateAdd("m", 1, monthBeginDate))

    ' Step 1 - How many days between seriesDefinition and the 1st of [month]
    Dim daysToMonthBegin As Integer
    daysToMonthBegin = DateDiff("d", seriesDefinition, monthBeginDate)

    ' Step 2 - How many fortnights (14 days) fit into the number from Step 1?  Round up to the nearest whole number.
    Dim numberFortnightsToFirstOccurenceOfSeriesInMonth As Integer
    numberFortnightsToFirstOccurenceOfSeriesInMonth = (daysToMonthBegin \ 14) + IIf(daysToMonthBegin Mod 14 > 0, 1, 0)

    ' Step 3 - The date of the first date of this series inside that month is seriesDefinition + the number of fortnights from Step 2
    Dim firstDateOfSeriesInMonth As Date
    firstDateOfSeriesInMonth = DateAdd("d", (14 * numberFortnightsToFirstOccurenceOfSeriesInMonth), seriesDefinition)

    ' Step 4 - How many fortnights fit between the date from Step 3 and the last date of the [month]?
    NumberFortnightlyEventsInMonth = 1 + (DateDiff("d", firstDateOfSeriesInMonth, lastDateOfMonth) \ 14)

End Function
我的解决方案

Public Function NumberFortnightlyEventsInMonth(seriesDefinition As Date, month As String) As Integer

    Dim monthBeginDate As Date
    monthBeginDate = DateValue("1 " + month)
    Dim lastDateOfMonth  As Date
    lastDateOfMonth = DateAdd("d", -1, DateAdd("m", 1, monthBeginDate))

    ' Step 1 - How many days between seriesDefinition and the 1st of [month]
    Dim daysToMonthBegin As Integer
    daysToMonthBegin = DateDiff("d", seriesDefinition, monthBeginDate)

    ' Step 2 - How many fortnights (14 days) fit into the number from Step 1?  Round up to the nearest whole number.
    Dim numberFortnightsToFirstOccurenceOfSeriesInMonth As Integer
    numberFortnightsToFirstOccurenceOfSeriesInMonth = (daysToMonthBegin \ 14) + IIf(daysToMonthBegin Mod 14 > 0, 1, 0)

    ' Step 3 - The date of the first date of this series inside that month is seriesDefinition + the number of fortnights from Step 2
    Dim firstDateOfSeriesInMonth As Date
    firstDateOfSeriesInMonth = DateAdd("d", (14 * numberFortnightsToFirstOccurenceOfSeriesInMonth), seriesDefinition)

    ' Step 4 - How many fortnights fit between the date from Step 3 and the last date of the [month]?
    NumberFortnightlyEventsInMonth = 1 + (DateDiff("d", firstDateOfSeriesInMonth, lastDateOfMonth) \ 14)

End Function

您的回答是“真的评论”,但感谢您富有洞察力的评论。您的回答是“真的评论”,但感谢您富有洞察力的评论。。。。弗拉德,我想我知道你要说什么了,但那看起来非常像一个,我想要的是更优雅一点的东西。而且,算法有点复杂incomplete@Jason字体这不是蛮力,毕竟是O(1)。我的回答只是一个可以做什么的概要。这个解决方案似乎更优雅,因为所有的复杂性都只在天数计算上。嗯。。。。弗拉德,我想我知道你要说什么了,但那看起来非常像一个,我想要的是更优雅一点的东西。而且,算法有点复杂incomplete@Jason字体这不是蛮力,毕竟是O(1)。我的回答只是一个可以做什么的概要。这个解决方案似乎更加优雅,因为所有的复杂性都只是在日数计算上。