Created
March 18, 2015 03:08
-
-
Save natecook1000/43976a66fa04e3fdb3c7 to your computer and use it in GitHub Desktop.
Swift-friendly NSCalendar methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NSCalendar+Swift.swift | |
// A set of Swift-idiomatic methods for NSCalendar | |
// | |
// (c) 2015 Nate Cook, licensed under the MIT license | |
extension NSCalendar { | |
/// Returns the hour, minute, second, and nanoseconds of a given date. | |
func getTimeFromDate(date: NSDate) -> (hour: Int, minute: Int, second: Int, nanosecond: Int) { | |
var (hour, minute, second, nanosecond) = (0, 0, 0, 0) | |
getHour(&hour, minute: &minute, second: &second, nanosecond: &nanosecond, fromDate: date) | |
return (hour, minute, second, nanosecond) | |
} | |
/// Returns the era, year, month, and day of a given date. | |
func getDateItemsFromDate(date: NSDate) -> (era: Int, year: Int, month: Int, day: Int) { | |
var (era, year, month, day) = (0, 0, 0, 0) | |
getEra(&era, year: &year, month: &month, day: &day, fromDate: date) | |
return (era, year, month, day) | |
} | |
/// Returns the era, year for week-of-year calculations, week of year, and weekday of a given date. | |
func getWeekItemsFromDate(date: NSDate) -> (era: Int, yearForWeekOfYear: Int, weekOfYear: Int, weekday: Int) { | |
var (era, yearForWeekOfYear, weekOfYear, weekday) = (0, 0, 0, 0) | |
getEra(&era, yearForWeekOfYear: &yearForWeekOfYear, weekOfYear: &weekOfYear, weekday: &weekday, fromDate: date) | |
return (era, yearForWeekOfYear, weekOfYear, weekday) | |
} | |
/// Returns the start and length of the next weekend after the given date. Returns nil if the | |
/// calendar or locale don't support weekends. | |
func nextWeekendAfterDate(date: NSDate) -> (startDate: NSDate, interval: NSTimeInterval)? { | |
var startDate: NSDate? | |
var interval: NSTimeInterval = 0 | |
if nextWeekendStartDate(&startDate, interval: &interval, options: nil, afterDate: date), | |
let startDate = startDate | |
{ | |
return (startDate, interval) | |
} | |
return nil | |
} | |
/// Returns the start and length of the weekend before the given date. Returns nil if the | |
/// calendar or locale don't support weekends. | |
func nextWeekendBeforeDate(date: NSDate) -> (startDate: NSDate, interval: NSTimeInterval)? { | |
var startDate: NSDate? | |
var interval: NSTimeInterval = 0 | |
if nextWeekendStartDate(&startDate, interval: &interval, options: .SearchBackwards, afterDate: date), | |
let startDate = startDate | |
{ | |
return (startDate, interval) | |
} | |
return nil | |
} | |
/// Returns the start and length of the weekend containing the given date. Returns nil if the | |
/// given date isn't in a weekend or if the calendar or locale don't support weekends. | |
func rangeOfWeekendContainingDate(date: NSDate) -> (startDate: NSDate, interval: NSTimeInterval)? { | |
var startDate: NSDate? | |
var interval: NSTimeInterval = 0 | |
if rangeOfWeekendStartDate(&startDate, interval: &interval, containingDate: date), | |
let startDate = startDate | |
{ | |
return (startDate, interval) | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fork updated for Swift 3 - https://gist.github.com/tomaskraina/4dabc4bdfa0f570050fbd81cc98b72f3