sách gpt4 ai đã đi

swift - 从时间间隔中提取按天分组的日期范围

In lại 作者:塔克拉玛干 更新时间:2023-11-03 06:18:41 25 4
mua khóa gpt4 Nike

我想从 2 个时间间隔中提取按天分组的日期范围。例如,来自:

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

我想得到:

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

Đây là mã của tôi:

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)
}
}

你有什么建议吗?

Cảm ơn!

1 Câu trả lời

我会建议以下解决方案。

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]

sử dụng NSCalendar 是比直接操作 NSTimeInterval 更可靠的方法,因为日历会计算一些异常情况,如闰秒等。

关于swift - 从时间间隔中提取按天分组的日期范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36994654/

25 4 0
Bài viết được đề xuất: java - java中循环队列相对于栈的优势
Bài viết được đề xuất: javascript - 将并排的 Rects 连接到一个大 Rect
Bài viết được đề xuất: java - 不同起点的八皇后算法
Bài viết được đề xuất: Thuật toán - được sử dụng để đếm số lần các cặp
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com