我一直在寻找这个,但无法找到符合我需求的解决方案。我需要获得一个类型为NSDate的日期,而不是一个字符串,其中包含特定时区的当前时间,以便稍后比较它以查看时间是否在一个范围之间。
例如,我需要查看商店是否已打开。我在EST时区,现在是晚上11:30,所以我看到商店的时间表在11点关闭,但商店在PST,这意味着现在是在该区域的晚上10点,仍然打开。所以我需要将我的时区更改为商店时区,以便在现在打开或关闭时显示标志。
这是我尝试过的一个例子:
guard let tz = model?.timeZone else {return "EST"};
let tz = TimeZone.init(abbreviation: timeZone);
df.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ";
let dateString = df.string(from:Date()); //time is correct
let date = df.date(from: dateString); //ignore timeZone and transform to a different date.
但是当我转换包含正确设置时区的时间的String并将其解析回Date类型时,它只是忽略timeZone并显示不同的时间。 我想我将不得不做一些事情,比如获取时间之间的差异并添加到当前时间,但我不确定如何完成这项工作。因为这也是我一直在尝试做的事情:
let localTimeZoneOffset = myTimeZone.secondsFromGMT(for: Date());
let currentTime = Date().addingTimeInterval(TimeInterval(-
localTimeZoneOffset));
我仍然可以在任何区域,我想看看另一个区域的商店是否打开了两个不同的时区,设备的当前时间和商店的开放时间。我的位置可以在任何地方和我选择的商店,所以我想我需要得到两次或类似的东西
我们假设这是我得到的信息:Now = Date(); OpenTime = 2018-04-27 11:00:00 +0000; CloseTime = 2018-04-28 03:00:00 +0000; StoreTimeZone =“EST”如果我在一个与商店有不同时区的地方,如果商店是否开放,这个信息很容易获得?我已经有了比较的功能是时间是在OpenTime和CloseTime之间我只需要在商店中传递正确的时间,如果它现在是打开的话。
提前致谢。
答案 0 :(得分:-1)
这是我的解决方案。我在一个方便的地方为JSON添加了一个时区;您需要适应以允许在您的数据中定位时区。
struct Schedule: Codable {
var timezone: String
var storeHours: [DayHours]
}
struct DayHours: Codable {
var hours: String
var day: String
var date: String
}
struct OpeningHours {
let day: String
let openTime: Date
let closeTime: Date
func isOpen(on date: Date) -> Bool {
return date >= openTime && date < closeTime
}
}
struct StoreSchedule {
let openingHours:[OpeningHours]
let timezone: TimeZone
init?(_ schedule: Schedule) {
guard let timezone = TimeZone(abbreviation:schedule.timezone) else {
return nil
}
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
dateFormatter.timeZone = timezone
self.timezone = timezone
var openingHours = [OpeningHours]()
for dayHours in schedule.storeHours {
let components = dayHours.hours.components(separatedBy: "-")
guard components.count == 2 else {
return nil
}
let opening = components[0]
let closing = components[1]
guard let openTime = dateFormatter.date(from: "\(dayHours.date) \(opening)"),
let closeTime = dateFormatter.date(from: "\(dayHours.date) \(closing)") else {
return nil
}
openingHours.append(OpeningHours(day: dayHours.day, openTime: openTime, closeTime: closeTime))
}
self.openingHours = openingHours
}
func isOpen(on date: Date) -> Bool{
for openings in openingHours {
if openings.isOpen(on: date) {
return true
}
}
return false
}
}
let json = """
{
"timezone": "PST",
"storeHours": [{
"hours": "7:00 AM - 11:00 PM",
"day": "Friday",
"date": "2018-04-27"
}, {
"hours": "7:00 AM - 11:00 PM",
"day": "Saturday",
"date": "2018-04-28"
}, {
"hours": "7:00 AM - 11:00 PM",
"day": "Sunday",
"date": "2018-04-29"
}, {
"hours": "7:00 AM - 11:00 PM",
"day": "Monday",
"date": "2018-04-30"
}, {
"hours": "7:00 AM - 11:00 PM",
"day": "Tuesday",
"date": "2018-05-01"
}, {
"hours": "7:00 AM - 11:00 PM",
"day": "Wednesday",
"date": "2018-05-02"
}, {
"hours": "7:00 AM - 11:00 PM",
"day": "Thursday",
"date": "2018-05-03"
}]
}
"""
let schedule = try! JSONDecoder().decode(Schedule.self, from: json.data(using: .utf8)!)
if let storeSchedule = StoreSchedule(schedule) {
// print(storeSchedule)
let openClosed = storeSchedule.isOpen(on: Date()) ? "Open":"Closed"
print("Store is: \(openClosed)")
} else {
print("Unable to create schedule")
}
商店是:打开