Reputation: 271794
Given a unix timestamp, how can my iOS code know if it is before or after 6am (6am = the most recent 6am that occurred)? Are there time zones involved?
Upvotes: 2
Views: 150
Reputation: 318814
Convert the unix timestamp to a Date
. Then use Calendar
to get the DateComponents
from the Date
. By default, these components will be interpreted in the user's current timezone. If you wish to interpret the date in a different timezone, set the calendar's timezone before getting the components from the date.
By looking at the desired components you can make your decision about the hour.
let date = Date(timeIntervalSince1970: someUnixTimeStamp)
let components = Calendar.current.dateComponents(in: TimeZone.current, from: date)
// look at hour as needed
There are other Calendar
APIs if you just want a single component or just a smaller subset of components instead of all components from the date.
Upvotes: 1