问题描述
我有一个正在 Swift 中构建的应用程序,需要确定商店/餐厅当前是否营业.它查询我可以控制的数据库,因此我可以随意设置开放时间.目前,我将一周中的每一天的 openTime/closeTime 列设置为时间戳.例如:MonOpen = 11:00,MonClose = 19:00.
I have an app that I'm building in Swift that needs to determine if a store/restaurant is currently open. It queries a database that I have control of, so I can set the open hours however I'd like. Currently I have an openTime/closeTime column for each day of the week set as a timestamp. For example: MonOpen = 11:00, MonClose = 19:00.
我如何使用 swift 确定营业地点当前是否营业?我想像 if currentTime > 之类的东西MonOpen &当前时间
How can I use swift to determine if the place of business is currently open? I'm imagining something like if currentTime > MonOpen & currentTime < MonClose {...
iOS Starbucks 应用就是一个例子.如果您前往地点,则每个地点都会列出营业至 22:00"或营业至 23:00".
An example of this is the iOS Starbucks app. If you go to locations, each location is listed with an "Open until 22:00" or "Open until 23:00."
推荐答案
这只是玩弄时区的问题,无论您是使用用户系统的时区还是让他们在应用设置中选择另一个时区:
It's just a matter of playing with the timezone, whether you use the user system's timezone or let them choose another one in the app's settings:
let tz = NSTimeZone.defaultTimeZone()
let now = NSCalendar.currentCalendar().componentsInTimeZone(tz, fromDate: NSDate())
if now.weekDay == 2 && now.hour > MonOpen && now.hour < MonClose {
// The store is open
}
这篇关于如何根据营业时间确定企业是否营业(Swift-iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!