2016-05-03 34 views
0

2つの時間間隔から1日ごとにグループ化した日付範囲を抽出します。私が取得したい日付範囲を時間間隔で1日ごとにグループ化します。

let startDateTimeInterval: NSTimeInterval = 1462060790 // "2016-04-30 23:59:50 +0000\n" 
let endDateTimeInterval: NSTimeInterval = 1462183200 // "2016-05-02 00:00:10 +0000\n" 

:からexempleについては 、

9 seconds // "[2016-04-30 23:59:50 => 2016-04-30 23:59:59] 
86399 seconds // "[2016-05-01 00:00:00 => 2016-05-01 23:59:59] 
10 seconds // "[2016-05-02 00:00:00 => 2016-05-01 00:09:59] 

私はそれを成し遂げたが、よりよい解決策がある場合、私は疑問に思って。 私の方法は次のとおりです。ここで

1- Create NSDate from startDateTimeInterval 
2- Create NSDate for end of day (23:59:59) from startDate (previous step) 
3- Get difference between these 2 dates. 
4- increment startDateTimeInterval with difference in seconds (previous step) and go back to step 1 with updated startDateTimeInterval. 

Repeat steps while startDate < endDate 

私のコードは次のとおりです。あなたが提案を持っていますか

func createItemsPerDay(startDateTimeInterval: NSTimeInterval, endDateTimeInterval: NSTimeInterval) 
{ 
    var currentTimeInterval = startDateTimeInterval 
    let currentDate = NSDate(timeIntervalSince1970: currentTimeInterval) 
    var currentDateEndOfDay = currentDate.endOfDay(NSTimeZone(forSecondsFromGMT: 0)) 

    if (currentDateEndOfDay.timeIntervalSince1970 > endDateTimeInterval) { 
     currentDateEndOfDay = NSDate(timeIntervalSince1970: endDateTimeInterval) 
    } 

    let numberOfElapsedSecondsForEvent = currentDateEndOfDay.timeIntervalSinceDate(currentDate) 

    print("event: \(numberOfElapsedSecondsForEvent) seconds") 

    currentTimeInterval += (numberOfElapsedSecondsForEvent + 1) 

    if (currentTimeInterval < endDateTimeInterval) { 
     createItemsPerDay(startDateTimeInterval: currentTimeInterval, endDateTimeInterval: endDateTimeInterval) 
    } 
} 

ありがとうございます!

+1

など、NSTimeInterval上で動作指示し、より信頼性の高いアプローチで、最後のタイムスタンプは、に対応していませんコメントの日付。また、最初の間隔は実際には9秒ではなく10秒でなければなりません(今日は23:59:59.999が今日でも24:00:00と23:59:59で終わらない)。 – werediver

+0

ありがとう@werediver私は私の質問を編集しました。問題は、今日が終わるときです。 SOのいくつかの投稿が23:59:59を示しています... – thierryb

+0

次に23:59:59.001は翌日ですか?それは私には意味がありません。 – werediver

答えて

1

以下の解決策を提案します。カレンダーはうるう秒のようないくつかの異常をカウントするためNSCalendarを使用して

import Foundation 

func timeIntervalsPerDay(start: NSDate, _ end: NSDate, calendar: NSCalendar) -> [NSTimeInterval] { 
    var timeIntervals = [NSTimeInterval]() 
    let dayStart = NSDateComponents() 
    dayStart.hour = 0 
    dayStart.minute = 0 
    dayStart.second = 0 
    var prevDate = start 
    calendar.enumerateDatesStartingAfterDate(start, matchingComponents: dayStart, options: [.MatchStrictly]) { date, exactMatch, _stop in 
     let stop = date.map { $0.compare(end) != .OrderedAscending } ?? false 
     if let date = date where !stop { 
      timeIntervals.append(date.timeIntervalSinceDate(prevDate)) 
      prevDate = date 
     } 
     _stop.memory = ObjCBool(stop) 
    } 
    timeIntervals.append(end.timeIntervalSinceDate(prevDate)) 
    return timeIntervals 
} 

let df = NSDateFormatter() 
df.dateFormat = "yyyy-MM-dd HH:mm:ss Z" 
let start = df.dateFromString("2016-04-30 23:59:50 +0000")! 
let end = df.dateFromString("2016-05-02 00:00:10 +0000")! 

let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)! 
calendar.timeZone = NSTimeZone(name: "UTC")! 

print(timeIntervalsPerDay(start, end, calendar: calendar)) // [10.0, 86400.0, 10.0] 

はあなたの例の入力パラメータをチェック

関連する問題