-
Notifications
You must be signed in to change notification settings - Fork 1
/
Date.swift
577 lines (494 loc) · 16.8 KB
/
Date.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//
// SimpeSwift.swift
//
// Created by Nicola Zordan on 7/21/14.
// Copyright (c) 2014 CrosaraZordan. All rights reserved.
//
// a usable Date class for swift
// removing the need for unnecessary other objects to get and set date parts
import Foundation
//import UIKit
// Date by Nicola Zordan
class Date: Printable {
var nsdate:NSDate
//convenience
// init(dateString:String) {
// let dateStringFormatter = NSDateFormatter()
// dateStringFormatter.dateFormat = "yyyy-MM-dd"
// dateStringFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
// let d = dateStringFormatter.dateFromString(dateString)
// self.init(timeInterval:0, sinceDate:d)
//}
init() {
nsdate = NSDate()
}
init (nsdate:NSDate) {
self.nsdate=nsdate
}
init (date:Date) {
nsdate=date.nsdate
}
func asNSDate() -> NSDate {
return self.nsdate
}
//class var cal:NSCalendar {
// get {
// //return NSCalendar()
// //return NSCalendar(identifier:NSGregorianCalendar)
// return NSCalendar.currentCalendar()
//}
//}
class func cal() -> NSCalendar {
//return NSCalendar()
//return NSCalendar(identifier:NSGregorianCalendar)
return NSCalendar.currentCalendar()
}
///class func cal {
// return NSCalendar()
// //return NSCalendar(identifier:NSGregorianCalendar)
// //return NSCalendar.currentCalendar()
//}
//class var dateUnitComp: NSCalendarUnit {
// get {
// return NSCalendarUnit.CalendarUnitYear|NSCalendarUnit.CalendarUnitMonth|NSCalendarUnit.CalendarUnitDay|NSCalendarUnit.CalendarUnitHour|NSCalendarUnit.CalendarUnitMinute|NSCalendarUnit.CalendarUnitSecond|NSCalendarUnit.CalendarUnitNanosecond|NSCalendarUnit.CalendarUnitTimeZone|NSCalendarUnit.CalendarUnitWeekday|NSCalendarUnit.CalendarUnitWeekOfYear|NSCalendarUnit.CalendarUnitQuarter
//}
//}
class func dateUnitComp() -> NSCalendarUnit {
return NSCalendarUnit.CalendarUnitYear|NSCalendarUnit.CalendarUnitMonth|NSCalendarUnit.CalendarUnitDay|NSCalendarUnit.CalendarUnitHour|NSCalendarUnit.CalendarUnitMinute|NSCalendarUnit.CalendarUnitSecond|NSCalendarUnit.CalendarUnitNanosecond|NSCalendarUnit.CalendarUnitTimeZone|NSCalendarUnit.CalendarUnitWeekday|NSCalendarUnit.CalendarUnitWeekOfYear|NSCalendarUnit.CalendarUnitQuarter
}
class func now() -> Date {
return Date()
}
class func date0() -> Date {
return Date(year: 0, month: 0, day: 0, hour: 0, minute: 0, second: 0, nanosecond: 0)
}
init (string: String) {
nsdate = Date(yyyy_mm_dd: string).nsdate
}
init (yyyy_mm_dd: String) {
let dateStringFormatter = NSDateFormatter()
dateStringFormatter.dateFormat = "yyyy-MM-dd"
dateStringFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
nsdate = Date(nsdate: dateStringFormatter.dateFromString(yyyy_mm_dd)).nsdate
//return d
}
init (year:Int, month:Int, day:Int) {
nsdate = Date(year: year, month: month, day: day, hour: 0, minute: 0, second: 0, nanosecond: 0).nsdate
}
init (hour: Int, minute: Int, second: Int) {
nsdate = Date(year: 0, month: 0, day: 0, hour: hour, minute: minute, second: second, nanosecond: 0).nsdate
}
init (hour: Int, minute: Int, second: Int, nanosecond: Int) {
nsdate = Date(year: 0, month: 0, day: 0, hour: hour, minute: minute, second: second, nanosecond: 0).nsdate
}
init (year:Int, month:Int, day:Int, hour: Int, minute: Int, second: Int) {
nsdate = Date(year: year, month: month, day: day, hour: hour, minute: minute, second: second, nanosecond: 0).nsdate
}
init (year:Int, month:Int, day:Int, hour: Int, minute: Int, second: Int, nanosecond: Int) {
var c = NSDateComponents()
c.year = year
c.month = month
c.day = day
c.hour=hour
c.minute=minute
c.second=second
c.nanosecond=nanosecond
var cal = NSCalendar(identifier:NSGregorianCalendar)
var date = cal.dateFromComponents(c)
nsdate = date
//return Date(date: date)
}
func year0() -> Int {
var i: Int = 0
//var cal = NSCalendar()
//var dateComp = cal.components(NSCalendarUnit.CalendarUnitYear|NSCalendarUnit.CalendarUnitMonth|NSCalendarUnit.CalendarUnitDay, fromDate: self)
var dateComp = Date.cal().components(Date.dateUnitComp(), fromDate: self.nsdate)
i=dateComp.year
return i
}
class func componentsOf(nsdate:NSDate) -> NSDateComponents {
var dateComp = Date.cal().components(Date.dateUnitComp(), fromDate: nsdate)
return dateComp
}
func components() -> NSDateComponents {
return Date.componentsOf(nsdate)
}
var year: Int {
get {
var i: Int = 0
//var dateComp = Date.cal().components(Date.dateUnitComp(), fromDate: nsdate)
//var dateComp=Date.componentsOf(nsdate)
var dateComp=components()
i=dateComp.year
return i
}
set {
nsdate=Date(year: newValue, month: self.month, day: self.day, hour: self.hour, minute: self.minute, second: self.second, nanosecond: self.nanosecond).nsdate
}
}
var month: Int {
get {
var i: Int = 0
var dateComp=components()
i=dateComp.month
return i
}
set {
nsdate=Date(year: self.year, month: newValue, day: self.day, hour: self.hour, minute: self.minute, second: self.second, nanosecond: self.nanosecond).nsdate
}
}
var day: Int {
get {
var i: Int = 0
var dateComp=components()
i=dateComp.day
return i
}
set {
nsdate=Date(year: self.year, month: self.month, day: newValue, hour: self.hour, minute: self.minute, second: self.second, nanosecond: self.nanosecond).nsdate
}
}
var hour: Int {
get {
var i: Int = 0
var dateComp=components()
i=dateComp.hour
return i
}
set {
nsdate=Date(year: self.year, month: self.month, day: self.day, hour: newValue, minute: self.minute, second: self.second, nanosecond: self.nanosecond).nsdate
}
}
var minute: Int {
get {
var i: Int = 0
var dateComp=components()
i=dateComp.minute
return i
}
set {
nsdate=Date(year: self.year, month: self.month, day: self.day, hour: self.hour, minute: newValue, second: self.second, nanosecond: self.nanosecond).nsdate
}
}
var second: Int {
get {
var i: Int = 0
var dateComp=components()
i=dateComp.second
return i
}
set {
nsdate=Date(year: self.year, month: self.month, day: self.day, hour: self.hour, minute: self.minute, second: newValue, nanosecond: self.nanosecond).nsdate
}
}
var nanosecond: Int {
get {
var i: Int = 0
var dateComp=components()
i=dateComp.nanosecond
return i
}
set {
nsdate=Date(year: self.year, month: self.month, day: self.day, hour: self.hour, minute: self.minute, second: self.second, nanosecond: newValue).nsdate
}
}
var weekday: Int {
get {
var i: Int = 0
var dateComp=components()
i=dateComp.weekday
return i
}
}
var weekOfYear: Int {
get {
var i: Int = 0
var dateComp=components()
i=dateComp.weekOfYear
return i
}
}
var quarter: Int {
get {
var i: Int = 0
var dateComp=components()
i=dateComp.quarter
return i
}
}
func add (#years:Int, months:Int, days: Int, hours:Int, minutes:Int, seconds:Int, nanoseconds: Int) -> Date {
var year=self.year+years
var month=self.month+months
var day=self.day+days
var hour=self.hour+hours
var minute=self.minute+minutes
var second=self.second+seconds
var nano=self.nanosecond+nanoseconds
var r=Date(year: year, month: month, day: day, hour: hour, minute: minute, second: second, nanosecond: nano)
nsdate=r.nsdate
return self
//return r
}
func add (d: Date) -> Date {
return add(years:d.year, months:d.month, days:d.day, hours:d.hour, minutes:d.minute, seconds:d.second, nanoseconds:d.nanosecond)
}
func subtract (d: Date) -> Date {
return add(years: -d.year, months: -d.month, days: -d.day, hours: -d.hour, minutes: -d.minute, seconds: -d.second, nanoseconds: -d.nanosecond)
}
func add (#years: Int) -> Date {
return add(years:years, months:0, days:0, hours:0, minutes:0, seconds:0, nanoseconds:0)
}
func add (#months: Int) -> Date {
return add(years:0, months:months, days:0, hours:0, minutes:0, seconds:0, nanoseconds:0)
}
func add (#days: Int) -> Date {
return add(years:0, months:0, days:days, hours:0, minutes:0, seconds:0, nanoseconds:0)
}
func add (#hours: Int) -> Date {
return add(years:0, months:0, days:0, hours:hours, minutes:0, seconds:0, nanoseconds:0)
}
func add (#minutes: Int) -> Date {
return add(years:0, months:0, days:0, hours:0, minutes:minutes, seconds:0, nanoseconds:0)
}
func add (#seconds: Int) -> Date {
return add(years:0, months:0, days:0, hours:0, minutes:0, seconds:seconds, nanoseconds:0)
}
func add (#nanoseconds: Int) -> Date {
return add(years:0, months:0, days:0, hours:0, minutes:0, seconds:0, nanoseconds:nanoseconds)
}
func add (#seconds: Int, nanoseconds: Int) -> Date {
return add(years:0, months:0, days:0, hours:0, minutes:0, seconds:seconds, nanoseconds:nanoseconds)
}
func asDateOnly() -> Date {
var r=Date(year: self.year, month: self.month, day: self.day, hour: 0, minute: 0, second: 0, nanosecond: 0)
return r
}
func asTimeOnly() -> Date {
var r=Date(year: 0, month: 0, day: 0, hour: self.hour, minute: self.minute, second: self.second, nanosecond: 0)
return r
}
func asTimeOnlyNanosecond() -> Date {
var r=Date(year: 0, month: 0, day: 0, hour: self.hour, minute: self.minute, second: self.second, nanosecond: self.nanosecond)
return r
}
func asDays() -> Int {
var i: Int = 0
//NSUInteger unitFlags = NSDayCalendarUnit;
//NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
//NSDateComponents *components = [calendar components:unitFlags fromDate:dt1 toDate:dt2 options:0];
//return [components day]+1;
var op = Date.cal().components(NSCalendarUnit.DayCalendarUnit, fromDate: Date.date0().nsdate, toDate: self.nsdate, options: nil)
i=op.day
return i
}
//func asHours() -> Int {
// var i: Int = 0
// var op = cal.components(NSCalendarUnit.HourCalendarUnit, fromDate: NSDate.date0(), toDate: self, options: nil)
// i=op.day
// return i
//}
//func asSeconds() -> Int {
// var i: Int = 0
// var op = cal.components(NSCalendarUnit.SecondCalendarUnit, fromDate: NSDate.date0(), toDate: self, options: nil)
// i=op.day
// return i
//}
//func asWeeks() -> Int {
// var i: Int = 0
// var op = cal.components(NSCalendarUnit.WeekCalendarUnit, fromDate: NSDate.date0(), toDate: self, options: nil)
// i=op.day
// return i
//}
//func asMonths() -> Int {
// var i: Int = 0
// var op = cal.components(NSCalendarUnit.MonthCalendarUnit, fromDate: NSDate.date0(), toDate: self, options: nil)
// i=op.day
// return i
//}
//func asYears() -> Int {
// var i: Int = 0
// var op = cal.components(NSCalendarUnit.YearCalendarUnit, fromDate: NSDate.date0(), toDate: self, options: nil)
// i=op.day
// return i
//}
func asDouble() -> Double {
// returns milliseconds, and fraction part of ms
var ti = nsdate.timeIntervalSinceReferenceDate
var d: Double = ti
//var d: Double = ti * 100000
//var f: Float = Float(ti)
//var op = Date.cal.components(NSCalendarUnit.SecondCalendarUnit, fromDate: Date.date0().nsdate, toDate: self.nsdate, options: nil)
//f=op.second
return d
}
func isBefore(d: Date) -> Bool {
var dc: NSComparisonResult = ((self.nsdate).compare(d.nsdate))
//switch dc {
// case NSComparisonResult.OrderedSame:
// // =
// break
// case NSComparisonResult.OrderedAscending:
// // < : self < d
// break
// case NSComparisonResult.OrderedDescending:
// // > : self > d
// break
//}
var b:Bool = (dc==NSComparisonResult.OrderedAscending)
return b
}
func isAfter(d: Date) -> Bool {
var dc: NSComparisonResult = ((self.nsdate).compare(d.nsdate))
var b:Bool = (dc==NSComparisonResult.OrderedDescending)
return b
}
func isEqual (d:Date) -> Bool {
var dc: NSComparisonResult = ((self.nsdate).compare(d.nsdate))
var b:Bool = (dc==NSComparisonResult.OrderedSame)
return b
}
func equals (date:Date) -> Bool {
return self.isEqual(date)
}
// sorting
func lessThan(date: Date) -> Bool {
return self.isBefore(date)
}
class func forSorting (d1:Date, d2: Date) -> Bool {
return d1.lessThan(d2)
}
class func forSortingBackwards (d1:Date, d2: Date) -> Bool {
return !(d1.lessThan(d2))
}
var description: String {
return "\(self.nsdate)"
}
//
//func yearF() -> Int {
// var i: Int=0
// var cal = NSCalendar()
// var dateComp = cal.components(NSCalendarUnit.CalendarUnitYear|NSCalendarUnit.CalendarUnitMonth|NSCalendarUnit.CalendarUnitDay, fromDate: self)
// i=dateComp.year
// return i
//}
func isLeapYear() -> Bool {
var r:Bool=false
r = self.year % 4 == 0 && !(self.year % 100 == 0) || (self.year % 400 == 0)
return r
}
func dayName(dayNames:[String]) -> String {
var n:String = ""
if (dayNames.count>=7) {
n=dayNames[self.weekday-1]
}
return n
}
func monthName(dayNames:[String]) -> String {
var n:String = ""
if (dayNames.count>=12) {
n=dayNames[self.month-1]
}
return n
}
func dayName() -> String {
let dayNames:[String]=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
return dayName(dayNames)
}
func monthName() -> String {
let monthNames:[String]=["January","February","March","April","May","June","July","August","September","October","November","December"]
return monthName(monthNames)
}
class func n00 (n:Int) -> String {
var s=""
if n<10 {
s="0"
}
s+="\(n)"
return s
}
func stringMDY(separator:String) -> String {
//var s="\(self.month)"+separator+"\(self.day)"+separator+"\(self.year)"
var s=Date.n00(self.month)+separator+Date.n00(self.day)+separator+"\(self.year)"
return s
}
func stringMDY() -> String {
// 28/7/2014
let separator="/"
return stringMDY(separator)
}
func stringWMDY() -> String {
// Monday July 28 2014
var s=self.dayName()+" "+self.monthName()+" \(self.day) \(self.year)"
return s
}
}
// date addition and subtraction
func + (left: Date, right: Date) -> Date {
var d:Date = Date(date: left)
return d.add(right)
}
func - (left: Date, right: Date) -> Date {
var d:Date = Date(date: left)
return d.subtract(right)
}
/* test
var dt=Date()
dt
dt.year
dt.month
dt.day
dt.hour
dt.minute
dt.second
dt.nanosecond
dt.year=1967
dt.year
var dt2=Date.YMD(year: 1967, month: 2, day: 3)
dt2.year
dt2.month
dt2.day
dt2.nanosecond
dt.asDouble()
Date.now().day
var d4=Date()
d4.year
d4.month
d4.add(days:5)
d4.add(months: 3)
d4.add(months: 6)
var d5=Date(date: d4).add(days: 20)
var d6=Date(date: d4).add(days: -25)
d5.isBefore(d6)
d5.isAfter(d6)
d5.asDays()
var d0=Date.date0()
d6.asDays()
d5.asDays()
d0.asDays()
var dd=Date(date:d4).subtract(d6)
d4-d6
var ddn=Date(date:d4).subtract(d5)
var ddn2=Date(date:d5).subtract(d4)
dd.asDays()
ddn.asDays()
ddn2.asDays()
ddn2.asDouble() // ms
ddn2.asDouble()/20
ddn2.asDouble()/20/24/60/60/60 // ms
var ads=[dt,d4,d5,d6,d0,ddn,ddn2]
ads.count
var adsSorted = sorted(ads, Date.forSorting)
adsSorted.count
var dl=Date()
dl.isLeapYear()
var dl2=Date(date: dl)
dl2.year=2004
dl2.isLeapYear()
dl.day
dl.weekday
dl.dayName()
dl.month
dl.monthName()
dl.stringMDY()
dl.stringWMDY()
/test */