Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Date / Time / Calendar / Time zone
Search
Lukasz Pikor
May 25, 2017
Programming
0
67
Date / Time / Calendar / Time zone
Presentation about classes from iOS SDK: Date / Time / Calendar / Time zone
Lukasz Pikor
May 25, 2017
Tweet
Share
More Decks by Lukasz Pikor
See All by Lukasz Pikor
Chat apps: my observations based on 2 projects and 3 chat services
pikor
1
37
Mobile app release strategies
pikor
0
110
Hashable in Swift
pikor
0
34
Other Decks in Programming
See All in Programming
All(?) About Point Sets
hole
0
240
connect-python: convenient protobuf RPC for Python
anuraaga
0
340
2025 컴포즈 마법사
jisungbin
1
170
モデル駆動設計をやってみよう Modeling Forum2025ワークショップ/Let’s Try Model-Driven Design
haru860
0
220
20251127_ぼっちのための懇親会対策会議
kokamoto01_metaps
2
380
[堅牢.py #1] テストを書かない研究者に送る、最初にテストを書く実験コード入門 / Let's start your ML project by writing tests
shunk031
11
6.6k
テストやOSS開発に役立つSetup PHP Action
matsuo_atsushi
0
130
手が足りない!兼業データエンジニアに必要だったアーキテクチャと立ち回り
zinkosuke
0
240
宅宅自以為的浪漫:跟 AI 一起為自己辦的研討會寫一個售票系統
eddie
0
430
レイトレZ世代に捧ぐ、今からレイトレを始めるための小径
ichi_raven
0
490
Full-Cycle Reactivity in Angular: SignalStore mit Signal Forms und Resources
manfredsteyer
PRO
0
170
Stay Hacker 〜九州で生まれ、Perlに出会い、コミュニティで育つ〜
pyama86
2
3.4k
Featured
See All Featured
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
960
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.8k
The World Runs on Bad Software
bkeepers
PRO
72
12k
What's in a price? How to price your products and services
michaelherold
246
12k
Music & Morning Musume
bryan
46
7k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
51k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
231
22k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.5k
Context Engineering - Making Every Token Count
addyosmani
9
450
Transcript
Date / Time / Calendar / Time zone Date /
Time / Calendar / Time zone
60, 3600, 86 400 Date / Time / Calendar /
Time zone
Date / Time / Calendar / Time zone
60, 3600, 86 400 60 * 60 (60 * 60
* 7, 60 * 60 * 17) [2, 3, 4, 5, 6]
60, 3600, 86 400 // extension Date var isTomorrow: Bool
{ let dayInterval = TimeInterval(60 * 60 * 24) let tomorrow = Date().addingTimeInterval(dayInterval) guard let endOfTomorrow = tomorrow.endOfDay else { return false } return isBetween(startDate: tomorrow.startOfDay, endDate: endOfTomorrow) }
60, 3600, 86 400 // extension Date var isTomorrow: Bool
{ let dayInterval = TimeInterval(60 * 60 * 24) let tomorrow = Date().addingTimeInterval(dayInterval) guard let endOfTomorrow = tomorrow.endOfDay else { return false } return isBetween(startDate: tomorrow.startOfDay, endDate: endOfTomorrow) } let dayInterval = TimeInterval(60 * 60 * 24)
60, 3600, 86 400 // extension Date var isTomorrow: Bool
{ let dayInterval = TimeInterval(60 * 60 * 24) let tomorrow = Date().addingTimeInterval(dayInterval) guard let endOfTomorrow = tomorrow.endOfDay else { return false } return isBetween(startDate: tomorrow.startOfDay, endDate: endOfTomorrow) } let tomorrow = Date().addingTimeInterval(dayInterval)
60, 3600, 86 400 // extension Date var isTomorrow: Bool
{ let dayInterval = TimeInterval(60 * 60 * 24) let tomorrow = Date().addingTimeInterval(dayInterval) guard let endOfTomorrow = tomorrow.endOfDay else { return false } return isBetween(startDate: tomorrow.startOfDay, endDate: endOfTomorrow) } guard let endOfTomorrow = tomorrow.endOfDay else { return false }
60, 3600, 86 400 // extension Date var isTomorrow: Bool
{ let dayInterval = TimeInterval(60 * 60 * 24) let tomorrow = Date().addingTimeInterval(dayInterval) guard let endOfTomorrow = tomorrow.endOfDay else { return false } return isBetween(startDate: tomorrow.startOfDay, endDate: endOfTomorrow) } return isBetween(startDate: tomorrow.startOfDay, endDate: endOfTomorrow)
System API’s to the rescue! System API’s to the rescue!
System API’s to the rescue! Session 124: Understanding Foundation, WWDC
2010
Date Date
Date Date() init(timeIntervalSinceNow: TimeInterval) init(timeInterval: TimeInterval, since: Date)
Date init(timeIntervalSinceReferenceDate: TimeInterval) // reference date: 00:00:00 UTC on 1
January 2001 init(timeIntervalSince1970: TimeInterval) // Unix Epoch: 00:00:00 UTC on 1 January 1970
Calendar Calendar
Calendar Calendar.current Calendar.autoupdatingCurrent
TimeZone TimeZone
TimeZone init?(secondsFromGMT seconds: Int) // 3600
TimeZone init?(abbreviation: String) // CET, CEST init?(identifier: String) // „Europe/Warsaw”
a.k.a tz database
DateComponents DateComponents
DateComponents let calendar = Calendar.current let timezone = TimeZone(identifier: "Europe/Warsaw")
var dateComps = DateComponents() dateComps.calendar = calendar dateComps.year = 2017 dateComps.month = 5 dateComps.day = 25 dateComps.hour = 19 dateComps.timeZone = timezone let dateToPrint = dateComps.date! print("Date: \(dateToPrint)”) // Date: 2017-05-25 17:00:00 +0000
DateComponents let calendar = Calendar.current let timezone = TimeZone(identifier: "Europe/Warsaw")
var dateComps = DateComponents() dateComps.calendar = calendar dateComps.year = 2017 dateComps.month = 5 dateComps.day = 25 dateComps.hour = 19 dateComps.timeZone = timezone let dateToPrint = dateComps.date! print("Date: \(dateToPrint)”) // Date: 2017-05-25 17:00:00 +0000 var dateComps = DateComponents() dateComps.calendar = calendar dateComps.year = 2017 dateComps.month = 5 dateComps.day = 25 dateComps.hour = 19 dateComps.timeZone = timezone
DateComponents let calendar = Calendar.current let timezone = TimeZone(identifier: "America/Los_Angeles")
var dateComps = DateComponents() dateComps.calendar = calendar dateComps.year = 2017 dateComps.month = 5 dateComps.day = 25 dateComps.hour = 19 dateComps.timeZone = timezone let dateToPrint = dateComps.date! print("Date: \(dateToPrint)”) // Date: 2017-05-26 02:00:00 +0000 let dateToPrint = dateComps.date! print("Date: \(dateToPrint)”) // Date: 2017-05-26 02:00:00 +0000
DateComponents let now = Date() var comps = DateComponents() comps.month
= -1 comps.day = -1 comps.hour = -1 comps.timeZone = timezone let calendar = Calendar.current let newDate = calendar.date(byAdding: comps, to: now) // Apr 24, 2017, 5:30 PM let now = Date() var comps = DateComponents() comps.month = -1 comps.day = -1 comps.hour = -1 comps.timeZone = timezone
DateComponents let now = Date() var comps = DateComponents() comps.month
= -1 comps.day = -1 comps.hour = -1 comps.timeZone = timezone let calendar = Calendar.current let newDate = calendar.date(byAdding: comps, to: now) // Apr 24, 2017, 5:30 PM let calendar = Calendar.current let newDate = calendar.date(byAdding: comps, to: now) // Apr 24, 2017, 5:30 PM
DateFormatter DateComponents
DateFormatter
DateFormatter let now = Date(timeIntervalSince1970: 1495737000) let formatter = DateFormatter()
formatter.locale = Locale.autoupdatingCurrent formatter.dateStyle = .medium formatter.timeStyle = .medium let output = formatter.string(from: now) // shorter version: DateFormatter.localizedString(from: now, dateStyle: .medium, timeStyle: .medium) // "May 25, 2017, 8:30:00 PM" // "2017年年5⽉月25⽇日 下午8:30:00"
So… DateComponents
So… let tuple = (60 * 60 * 7, 60
* 60 * 17) var sevenAM = DateComponents() sevenAM.hour = 7 sevenAM.minute = 00 var fivePM = DateComponents() sevenAM.hour = 17 sevenAM.minute = 00
So… let weekdays = [2, 3, 4, 5, 6] calendar.isDateInWeekend(myDate)
So… // extension Date var isTomorrow: Bool { let dayInterval
= TimeInterval(60 * 60 * 24) let tomorrow = Date().addingTimeInterval(dayInterval) guard let endOfTomorrow = tomorrow.endOfDay else { return false } return isBetween(startDate: tomorrow.startOfDay, endDate: endOfTomorrow) } calendar.isDateInTomorrow(myDate)
Pikor @pikorpikorpikor