2

特定のシリーズ内で、特定の暦月に隔週で発生するイベントの数を決定するための最も賢いアルゴリズムを探しています。

つまり、シリーズが「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

正確に生成されるように:numberFortnightlyEventsInSeriesThatFallInCalendarMonth

例:

NumberFortnightlyEventsInMonth( '2010年10月7日、'2010年10月')-> 2

NumberFortnightlyEventsInMonth( '2010年10月7日、'2010年11月')-> 2

NumberFortnightlyEventsInMonth( '2010年10月7日、'2010年12月')-> 3

10月には2つのイベントがあり、11月には2つのイベントがありますが、12月には3つのイベントがあることに注意してください。


擬似コードが望ましい。

ルックアップテーブルやWebサービスの呼び出し、またはユニバーサルライブラリ以外の外部リソースに依存したくありません。たとえば、ほとんどのプログラミング言語では、いくつかの日付操作関数が使用可能であると安全に想定できると思います。

4

3 に答える 3

0

日付を処理するときに「賢い」アルゴリズムはなく、面倒なアルゴリズムしかありません。つまり、毎月の日数を具体的にリストし、うるう年(4年ごと、100年ごと、400年ごとを除く)などを処理する必要があります。

于 2010-10-28T20:07:47.533 に答える
0

私の解決策...

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
于 2010-11-07T19:40:24.990 に答える
0

さて、あなたが話しているアルゴリズムの通常の解決策は、ある決まった日付から始まる日数を計算することです。(日数+前月の累積日数+年数* 365マイナス(年数/ 4)プラス(年数/ 100)マイナス(年数/ 400))

これにより、必要なものを簡単に実装できます。1月1日がどの曜日であったかを計算する必要があります。そうすると、その日から2010年10月1日および2010年12月1日までの「隔週の木曜日」の数を簡単に確認できます。それらの差は、探している値です。 。

于 2010-10-28T20:26:57.167 に答える