From 194c996ec0f28000468bac7ea868fe351441bd22 Mon Sep 17 00:00:00 2001 From: dankito Date: Sun, 12 Jul 2020 11:17:14 +0200 Subject: [PATCH] Implemented accessing year, month and day components and added constructor to create a Date from year, month and day --- .../net/dankito/utils/multiplatform/Date.kt | 12 ++++++ .../net/dankito/utils/multiplatform/Month.kt | 39 +++++++++++++++++++ .../net/dankito/utils/multiplatform/Date.kt | 36 +++++++++++++++++ .../net/dankito/utils/multiplatform/Date.kt | 29 ++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 common/src/commonMain/kotlin/net/dankito/utils/multiplatform/Month.kt diff --git a/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/Date.kt b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/Date.kt index c2977776..0b318d79 100644 --- a/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/Date.kt +++ b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/Date.kt @@ -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 + } \ No newline at end of file diff --git a/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/Month.kt b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/Month.kt new file mode 100644 index 00000000..3ad47091 --- /dev/null +++ b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/Month.kt @@ -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 } + } + + } + +} \ No newline at end of file diff --git a/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/Date.kt b/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/Date.kt index dfb6ffea..7d61e7a4 100644 --- a/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/Date.kt +++ b/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/Date.kt @@ -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() + } + } \ No newline at end of file diff --git a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/Date.kt b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/Date.kt index 09416b05..0db9b3ae 100644 --- a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/Date.kt +++ b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/Date.kt @@ -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() + } + } \ No newline at end of file