1

2010.12.27 12:33:58 などの DateTime があり、最後の境界線を除いて 2 秒の間隔フレームがあるとします。

だから、私は次のフレームを持っています:

12:33:58(含む)-12:34:00(除く) - インターバル1とする

12:34:00(含む)-12:34:02(含まない) - インターバル 2 とする

12:34:02(含む)-12:34:04(含まない) - インターバル3とする

等々。

ランダムな DateTime 値が与えられ、上記のルールに従ってその値を関連付ける必要があります。

例)値 "12:33:58" は間隔 1 に、"12:33:59" は間隔 1 に、"12:34:00" は間隔 2 に、というように続きます。

コードでは、次のようになります。

var dt = DateTime.Now;
DateTime intervalStart = apply_the_algorythm(dt);

float などを使用した単純な算術アクションのようですが、どんな決定でも大歓迎です!

4

5 に答える 5

2

If the interval is only second resolution and always divided 86400, then take the number of seconds that have passed today, divide it by the interval, round it to an integer value, multiply it, and add it back to today. Something like dateinquestion.Subtract(dateinquestion.Date).TotalSeconds, ((int)seconds/interval)*interval, dateinquestion.Date.AddSeconds(...)

于 2010-12-27T08:12:35.917 に答える
2

すべての間隔の範囲を数日、場合によっては長時間にまたがらせたい場合は、DateTime値をUNIX 秒(1970 年 1 月 1 日からの秒数) で表すことができます。次に、最初の間隔がいつ開始されたかを調べ、それから経過した秒数を計算し、2 で割ります。

int secondsSinceFirstInterval = <currDate in UNIX time>
                                 - <start of first interval in UNIX time>;
int intervalIndex = secondsSinceFirstInterval / 2;

それ以外の場合は、真夜中から数えたほうがよいでしょう。

于 2010-12-27T08:28:44.837 に答える
1

結果を使用TimeSpan.TotalSecondsして、間隔のサイズで割ります。

const long intervalSize = 2;
DateTime start = new DateTime(2010, 12, 27, 12, 33, 58);

TimeSpan timeSpan = DateTime.Now - start;
long intervalInSeconds = (long)timeSpan.TotalSeconds;
long intervalNumber = 1 + intervalInSeconds / intervalSize;
于 2010-12-27T08:33:11.613 に答える
0
 DateTime start = new DateTime(2010, 12, 31, 12, 0, 0);
 TimeSpan frameLength = new TimeSpan(0, 0, 3);
 DateTime testTime = new DateTime(2010, 12, 31, 12, 0, 4);

 int frameIndex = 0;
 while (testTime >= start)
 {
     frameIndex++;
     start = start.Add(frameLength);
 }

 Console.WriteLine(frameIndex);
于 2010-12-27T08:20:36.770 に答える
0
dates = new List<DateTime>
            {
                DateTime.Now.AddHours(-1),
                DateTime.Now.AddHours(-2),
                DateTime.Now.AddHours(-3)
            };
            dates.Sort((x, y) => DateTime.Compare(x.Date, y.Date)); 
            DateTime dateToCheck = DateTime.Now.AddMinutes(-120);
            int place = apply_the_algorythm(dateToCheck);
            Console.WriteLine(dateToCheck.ToString() + " is in interval :" +(place+1).ToString());

private int apply_the_algorythm(DateTime date)
        {
            if (dates.Count == 0)
                return -1;
            for (int i = 0; i < dates.Count; i++)
            {
                // check if the given date does not fall into any range.
                if (date < dates[0] || date > dates[dates.Count - 1])
                {
                    return -1;
                }
                else
                {
                    if (date >= dates[i]
                        && date < dates[i + 1])
                        return i;
                }
            }
            return dates.Count-1;
        }
于 2010-12-27T08:33:10.117 に答える