@@ -8,12 +8,18 @@ import platform.Foundation.NSCalendarUnitYear
8
8
import platform.Foundation.NSDate
9
9
import platform.Foundation.NSDateFormatter
10
10
11
- actual class LocalDateTime (private val date : NSDate ) {
11
+ actual class LocalDateTime (private val datetime : NSDate ) {
12
12
private val calendar = NSCalendar .currentCalendar
13
13
14
- actual val year: Int get() = calendar.component(NSCalendarUnitYear , date).toInt()
15
- actual val monthValue: Int get() = calendar.component(NSCalendarUnitMonth , date).toInt()
16
- actual val dayOfMonth: Int get() = calendar.component(NSCalendarUnitDay , date).toInt()
14
+ actual val year: Int get() = calendar.component(NSCalendarUnitYear , datetime).toInt()
15
+ actual val monthValue: Int get() = calendar.component(NSCalendarUnitMonth , datetime).toInt()
16
+ actual val dayOfMonth: Int get() = calendar.component(NSCalendarUnitDay , datetime).toInt()
17
+
18
+ actual override fun toString (): String {
19
+ val formatter = NSDateFormatter ()
20
+ formatter.dateFormat = " yyyy-MM-dd'T'HH:mm"
21
+ return formatter.stringFromDate(datetime)
22
+ }
17
23
18
24
actual companion object {
19
25
actual fun now (): LocalDateTime {
@@ -24,14 +30,14 @@ actual class LocalDateTime(private val date: NSDate) {
24
30
val formatter = NSDateFormatter ().apply {
25
31
dateFormat = " yyyy-MM-dd'T'HH:mm:ss"
26
32
}
27
- return LocalDateTime (formatter.dateFromString(date)!! )
33
+ return LocalDateTime (formatter.dateFromString(date) ? : NSDate () )
28
34
}
29
35
30
36
actual fun parse (date : String , pattern : String ): LocalDateTime {
31
37
val formatter = NSDateFormatter ().apply {
32
38
dateFormat = pattern
33
39
}
34
- return LocalDateTime (formatter.dateFromString(date)!! )
40
+ return LocalDateTime (formatter.dateFromString(date) ? : NSDate () )
35
41
}
36
42
}
37
43
}
0 commit comments