Implemented accessing year, month and day components and added constructor to create a Date from year, month and day
This commit is contained in:
parent
e59fd7b107
commit
194c996ec0
|
@ -14,7 +14,19 @@ expect class Date(millisSinceEpoch: Long) {
|
|||
|
||||
constructor()
|
||||
|
||||
constructor(year: Int, month: Int, day: Int)
|
||||
|
||||
actual constructor(year: Int, month: Month, day: Int)
|
||||
|
||||
|
||||
val millisSinceEpoch: Long
|
||||
|
||||
fun year(): Int
|
||||
|
||||
fun month(): Month
|
||||
|
||||
fun monthInt(): Int
|
||||
|
||||
fun day(): Int
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package net.dankito.utils.multiplatform
|
||||
|
||||
|
||||
enum class Month(val month: Int) {
|
||||
|
||||
January(1),
|
||||
|
||||
February(2),
|
||||
|
||||
March(3),
|
||||
|
||||
April(4),
|
||||
|
||||
May(5),
|
||||
|
||||
June(6),
|
||||
|
||||
July(7),
|
||||
|
||||
August(8),
|
||||
|
||||
September(9),
|
||||
|
||||
October(10),
|
||||
|
||||
November(11),
|
||||
|
||||
December(12);
|
||||
|
||||
|
||||
companion object {
|
||||
|
||||
fun fromInt(monthInt: Int): Month {
|
||||
return Month.values().first { it.month == monthInt }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -11,7 +11,19 @@ fun NSTimeInterval.toMillis(): Long {
|
|||
actual class Date(val date: NSDate) { // cannot subclass NSDate as it's a class cluster
|
||||
|
||||
companion object {
|
||||
|
||||
val DiffBetweenEpochTimeAndReferenceDate = (NSDate.timeIntervalSinceReferenceDate - NSTimeIntervalSince1970).toMillis()
|
||||
|
||||
fun from(year: Int, month: Int, day: Int): NSDate {
|
||||
val dateComponents = NSDateComponents()
|
||||
|
||||
dateComponents.year = year.toLong()
|
||||
dateComponents.month = month.toLong()
|
||||
dateComponents.day = day.toLong()
|
||||
|
||||
return NSCalendar.currentCalendar.dateFromComponents(dateComponents) !!
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -19,8 +31,32 @@ actual class Date(val date: NSDate) { // cannot subclass NSDate as it's a class
|
|||
|
||||
actual constructor() : this(NSDate())
|
||||
|
||||
actual constructor(year: Int, month: Int, day: Int) : this(from(year, month, day))
|
||||
|
||||
actual constructor(year: Int, month: Month, day: Int) : this(year, month.month, day)
|
||||
|
||||
|
||||
actual val millisSinceEpoch: Long
|
||||
get() = date.timeIntervalSince1970.toMillis()
|
||||
|
||||
|
||||
actual fun year(): Int {
|
||||
val components = NSCalendar.currentCalendar.components(NSCalendarUnitYear, date)
|
||||
return components.year.toInt()
|
||||
}
|
||||
|
||||
actual fun month(): Month {
|
||||
return Month.fromInt(monthInt())
|
||||
}
|
||||
|
||||
actual fun monthInt(): Int {
|
||||
val components = NSCalendar.currentCalendar.components(NSCalendarUnitMonth, date)
|
||||
return components.month.toInt()
|
||||
}
|
||||
|
||||
actual fun day(): Int {
|
||||
val components = NSCalendar.currentCalendar.components(NSCalendarUnitDay, date)
|
||||
return components.day.toInt()
|
||||
}
|
||||
|
||||
}
|
|
@ -1,12 +1,41 @@
|
|||
package net.dankito.utils.multiplatform
|
||||
|
||||
import java.text.DateFormat
|
||||
|
||||
|
||||
actual class Date actual constructor(millisSinceEpoch: Long) : java.util.Date(millisSinceEpoch) {
|
||||
|
||||
actual constructor() : this(System.currentTimeMillis())
|
||||
|
||||
actual constructor(year: Int, month: Int, day: Int) : this(java.util.Date(year - 1900, month - 1, day).time)
|
||||
|
||||
actual constructor(year: Int, month: Month, day: Int) : this(year, month.month, day)
|
||||
|
||||
|
||||
actual val millisSinceEpoch: Long
|
||||
get() = time
|
||||
|
||||
|
||||
actual fun year(): Int {
|
||||
return formatDate(DateFormat.YEAR_FIELD)
|
||||
}
|
||||
|
||||
actual fun month(): Month {
|
||||
return Month.fromInt(monthInt())
|
||||
}
|
||||
|
||||
actual fun monthInt(): Int {
|
||||
return formatDate(DateFormat.MONTH_FIELD)
|
||||
}
|
||||
|
||||
actual fun day(): Int {
|
||||
return formatDate(DateFormat.DAY_OF_YEAR_FIELD)
|
||||
}
|
||||
|
||||
private fun formatDate(dateFormatStyle: Int): Int {
|
||||
val dateStringString = DateFormat.getDateInstance(dateFormatStyle).format(this)
|
||||
|
||||
return dateStringString.toInt()
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue