Skip to content

Instantly share code, notes, and snippets.

View malcommac's full-sized avatar
👋
Nice to meet u

Daniele Margutti malcommac

👋
Nice to meet u
View GitHub Profile
@malcommac
malcommac / DateInRegion_DateComponents.swift
Created September 24, 2016 14:58
New DateInRegion from DateComponents
var cmp = DateComponents()
cmp.timeZone = TimeZoneName.europeOslo.timeZone
cmp.calendar = CalendarName.gregorian.calendar
cmp.calendar?.locale = LocaleName.englishNorway.locale
cmp.year = 2005
cmp.month = 4
cmp.day = 15
cmp.hour = 20
cmp.minute = 30
@malcommac
malcommac / DateInRegion_CalendarComponent.swift
Created September 24, 2016 15:09
Create new DateInRegion from Calendar.Component
let c: [Calendar.Component : Int] = [.year: 2002, .month: 3, .hour: 5, .day: 4, .minute: 6, .second: 7, .nanosecond: 87654321]
// create a new DateInRegion on 2002-03-04 at 05:06:07.87654321 (+/-10) in Region.Local()
let date = try! DateInRegion(components: c, fromRegion: nil)
@malcommac
malcommac / DateInRegion_Parse.swift
Created September 24, 2016 16:00
Parse DateInRegion from String
let rome = Region(tz: TimeZoneName.europeRome, cal: CalendarName.gregorian, loc: LocaleName.italian)
// Parse a string which a custom format
let date1 = try! DateInRegion(string: "1999-12-31 23:30:00", format: .custom("yyyy-MM-dd HH:mm:ss"), fromRegion: regionRome)
// Parse an AltRSS date from string using String extension
let date2 = try! "3 feb 2001 15:30:00 +0100".date(format: .rss(alt: true), fromRegion: regionRome)
// ...it's the same of:
let date3 = try! DateInRegion(string: "3 feb 2001 15:30:00 +0100", format: .ss(alt: true)), fromRegion: regionRome)
@malcommac
malcommac / SwiftDate_MathOperations.swift
Created September 27, 2016 08:19
Math Operations with Dates in SwiftDate
// Create a new date in Rome on 2015-01-03 @ 15:10:00
let regionRome = Region(tz: TimeZoneName.europeRome, cal: CalendarName.gregorian, loc: LocaleName.italianItaly)
var date = try! DateInRegion(components: [.year: 2015, .month: 1, .day: 3, .hour: 15, .minute: 10, .second: 0], fromRegion: regionRome)
// Let's go with some math operations
date = date + 3.seconds + 15.minutes // -> 03 gen 2015, 15:25:03 CET
date = date - 120.seconds // minus 2 minutes -> 03 gen 2015, 15:23:03 CET
date = date + 3.weeks // 24 gen 2015, 15:23:03 CET
// you can also add an absolute amount of seconds and it will modify directly the date
@malcommac
malcommac / SwiftDate_Compare.swift
Created September 27, 2016 08:34
Compare two DateInRegion
// 03 gen 2015, 15:10:00 CET
let date1 = try! DateInRegion(components: [.year: 2015, .month: 1, .day: 3, .hour: 15, .minute: 10, .second: 0], fromRegion: regionRome)
let date2 = try! DateInRegion(components: [.year: 2015, .month: 2, .day: 5, .hour: 15, .minute: 10, .second: 0], fromRegion: regionRome)
if date2 > date1 { // DateInRegion supports Equatable protocol you can use >=, <=, < or >
print("'\(date2)' date is later than '\(date1)'")
}
if date2 == date1 {
// simple equality
@malcommac
malcommac / SwiftDate_startOf().swift
Created September 27, 2016 08:52
Date at start of time components in SwiftDate
// Create a Region in Rome TimeZone with Gregorian Calendar and Italy locale settings
let regionRome = Region(tz: TimeZoneName.europeRome, cal: CalendarName.gregorian, loc: LocaleName.italianItaly)
// 03 gen 2015, 15:10:00 CET
let date = try! DateInRegion(components: [.year: 2015, .month: 1, .day: 3, .hour: 15, .minute: 10, .second: 0], fromRegion: regionRome)
let startOfHour = date.startOf(component: .hour) // 03 gen 2015, 15:00:00 CET
let startOfMonth = date.startOf(component: .month) // 01 gen 2015, 00:00:00 CET
let startOfWeek = date.startOf(component: .weekOfMonth) // 28 dic 2014, 00:00:00 CET
@malcommac
malcommac / SwiftDate_endOf().swift
Created September 27, 2016 08:54
Date at the end of time component in SwiftDate
// Create a Region in Rome TimeZone with Gregorian Calendar and Italy locale settings
let regionRome = Region(tz: TimeZoneName.europeRome, cal: CalendarName.gregorian, loc: LocaleName.italianItaly)
// 03 gen 2015, 15:10:00 CET
let date = try! DateInRegion(components: [.year: 2015, .month: 1, .day: 3, .hour: 15, .minute: 10, .second: 0], fromRegion: regionRome)
let startOfHour = date.endOf(component: .hour) // 03 gen 2015, 15:59:59 CET
let startOfMonth = date.endOf(component: .month) // 31 gen 2015, 23:59:59 CET
let startOfWeek = date.endOf(component: .weekOfMonth) // 03 gen 2015, 23:59:59 CET
@malcommac
malcommac / SwiftDate_AtTime.swift
Last active September 27, 2016 11:34
Set time at custom value in SwiftDate
// Create a Region in Rome TimeZone with Gregorian Calendar and Italy locale settings
// 03 gen 2015, 15:10:00 CET
let regionRome = Region(tz: TimeZoneName.europeRome, cal: CalendarName.gregorian, loc: LocaleName.italianItaly)
let date = try! DateInRegion(components: [.year: 2015, .month: 1, .day: 3, .hour: 15, .minute: 10, .second: 0], fromRegion: regionRome)
// Set custom time
let newDateAtGivenTime = try! date.atTime(hour: 09, minute: 10, second: 22) // 03 gen 2015, 09:10:22 CET
@malcommac
malcommac / SwiftDate_AtComponent.swift
Created September 27, 2016 11:35
SwiftDate alter single time component
// Create a Region in Rome TimeZone with Gregorian Calendar and Italy locale settings
// 03 gen 2015, 15:10:00 CET
let regionRome = Region(tz: TimeZoneName.europeRome, cal: CalendarName.gregorian, loc: LocaleName.italianItaly)
let date = try! DateInRegion(components: [.year: 2015, .month: 1, .day: 3, .hour: 15, .minute: 10, .second: 0], fromRegion: regionRome)
let atGivenYear = try! date.at(unit: .year, value: 2020) // 01 gen 2020, 00:00:00 CET. day is altered
@malcommac
malcommac / SwiftDate_atComponents.swift
Created September 27, 2016 11:41
Alert set of components in SwiftDate
// Create a Region in Rome TimeZone with Gregorian Calendar and Italy locale settings
// 03 gen 2015, 15:10:00 CET
let regionRome = Region(tz: TimeZoneName.europeRome, cal: CalendarName.gregorian, loc: LocaleName.italianItaly)
let date = try! DateInRegion(components: [.year: 2015, .month: 1, .day: 3, .hour: 15, .minute: 10, .second: 0], fromRegion: regionRome)
let time = try! date.at(unitsWithValues: [.year: 2016, .month: 7]) // 01 gen 2016, 00:00:00 CET