Skip to content

Instantly share code, notes, and snippets.

@tomaskraina
Forked from natecook1000/NSCalendar+Swift.swift
Last active May 20, 2020 09:56
Show Gist options
  • Save tomaskraina/4dabc4bdfa0f570050fbd81cc98b72f3 to your computer and use it in GitHub Desktop.
Save tomaskraina/4dabc4bdfa0f570050fbd81cc98b72f3 to your computer and use it in GitHub Desktop.
Swift-friendly NSCalendar methods
// NSCalendar+Swift.swift
// A set of Swift-idiomatic methods for NSCalendar
// Updated for Swift 3.1
//
// (c) 2015 Nate Cook, licensed under the MIT license
// (c) 2017 Tom Kraina, licensed under the MIT license
public extension Calendar {
/// Returns the hour, minute, second, and nanoseconds of a given date.
func getTimeFromDate(_ date: Date) -> (hour: Int, minute: Int, second: Int, nanosecond: Int) {
let components = dateComponents([.hour, .minute, .second, .nanosecond], from: date)
return (components.hour ?? 0, components.minute ?? 0, components.second ?? 0, components.nanosecond ?? 0)
}
/// Returns the era, year, month, and day of a given date.
func getDateItemsFromDate(_ date: Date) -> (era: Int, year: Int, month: Int, day: Int) {
let components = dateComponents([.era, .year, .month, .day], from: date)
return (components.era ?? 0, components.year ?? 0, components.month ?? 0, components.day ?? 0)
}
/// Returns the era, year for week-of-year calculations, week of year, and weekday of a given date.
func getWeekItemsFromDate(_ date: Date) -> (era: Int, yearForWeekOfYear: Int, weekOfYear: Int, weekday: Int) {
let components = dateComponents([.era, .yearForWeekOfYear, .weekOfYear, .weekday], from: date)
return (components.era ?? 0, components.yearForWeekOfYear ?? 0, components.weekOfYear ?? 0, components.weekday ?? 0)
}
/// 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: Date) -> (startDate: Date, interval: TimeInterval)? {
var startDate: Date = .distantFuture
var interval: TimeInterval = 0
if nextWeekend(startingAfter: date, start: &startDate, interval: &interval, direction: .forward) {
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: Date) -> (startDate: Date, interval: TimeInterval)? {
var startDate: Date = .distantPast
var interval: TimeInterval = 0
if nextWeekend(startingAfter: date, start: &startDate, interval: &interval, direction: .backward) {
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: Date) -> (startDate: Date, interval: TimeInterval)? {
var startDate: Date = .distantPast
var interval: TimeInterval = 0
if dateIntervalOfWeekend(containing: date, start: &startDate, interval: &interval)
{
return (startDate, interval)
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment