From d43d91b6cc84c64ce702ea585ef03836642650b9 Mon Sep 17 00:00:00 2001 From: dankito Date: Sat, 12 Dec 2020 00:24:36 +0100 Subject: [PATCH] Implemented setting time on Date; fixed formatting time tests --- .../kotlin/net/dankito/utils/multiplatform/Date.kt | 4 ++-- .../kotlin/net/dankito/utils/multiplatform/Date.kt | 10 +++++++--- .../kotlin/net/dankito/utils/multiplatform/Date.kt | 4 ++-- .../net/dankito/banking/fints/util/FinTsUtilsTest.kt | 8 ++++---- 4 files changed, 15 insertions(+), 11 deletions(-) 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 55d56f11..87e0df1b 100644 --- a/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/Date.kt +++ b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/Date.kt @@ -23,9 +23,9 @@ expect class Date(millisSinceEpoch: Long) { constructor() - constructor(year: Int, month: Int, day: Int) + constructor(year: Int, month: Int, day: Int, hour: Int = 0, minute: Int = 0, second: Int = 0) - actual constructor(year: Int, month: Month, day: Int) + constructor(year: Int, month: Month, day: Int, hour: Int = 0, minute: Int = 0, second: Int = 0) val millisSinceEpoch: Long 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 7fce5964..d40d01b7 100644 --- a/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/Date.kt +++ b/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/Date.kt @@ -23,13 +23,17 @@ actual class Date(val date: NSDate) { // cannot subclass NSDate as it's a class val DiffBetweenEpochTimeAndReferenceDate = (NSDate.timeIntervalSinceReferenceDate - NSTimeIntervalSince1970).toMillis() - fun from(year: Int, month: Int, day: Int): NSDate { + fun from(year: Int, month: Int, day: Int, hour: Int = 0, minute: Int = 0, second: Int = 0): NSDate { val dateComponents = NSDateComponents() dateComponents.year = year.toLong() dateComponents.month = month.toLong() dateComponents.day = day.toLong() + dateComponents.hour = hour.toLong() + dateComponents.minute = minute.toLong() + dateComponents.second = second.toLong() + val calendar = NSCalendar.currentCalendar val todayInUtc = calendar.dateFromComponents(dateComponents) !! @@ -54,9 +58,9 @@ 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: Int, day: Int, hour: Int, minute: Int, second: Int) : this(from(year, month, day, hour, minute, second)) - actual constructor(year: Int, month: Month, day: Int) : this(year, month.month, day) + actual constructor(year: Int, month: Month, day: Int, hour: Int, minute: Int, second: Int) : this(year, month.month, day, hour, minute, second) actual val millisSinceEpoch: Long 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 1204ad20..eca7f31b 100644 --- a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/Date.kt +++ b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/Date.kt @@ -31,9 +31,9 @@ actual class Date actual constructor(millisSinceEpoch: Long) : java.util.Date(mi 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: Int, day: Int, hour: Int, minute: Int, second: Int) : this(java.util.Date(year - 1900, month - 1, day, hour, minute, second).time) - actual constructor(year: Int, month: Month, day: Int) : this(year, month.month, day) + actual constructor(year: Int, month: Month, day: Int, hour: Int, minute: Int, second: Int) : this(year, month.month, day, hour, minute, second) actual val millisSinceEpoch: Long diff --git a/fints4k/src/commonTest/kotlin/net/dankito/banking/fints/util/FinTsUtilsTest.kt b/fints4k/src/commonTest/kotlin/net/dankito/banking/fints/util/FinTsUtilsTest.kt index 5ae71b8f..4425c1c3 100644 --- a/fints4k/src/commonTest/kotlin/net/dankito/banking/fints/util/FinTsUtilsTest.kt +++ b/fints4k/src/commonTest/kotlin/net/dankito/banking/fints/util/FinTsUtilsTest.kt @@ -41,7 +41,7 @@ class FinTsUtilsTest { fun formatTime_AM() { // given - val time = Date(8, 2, 1) + val time = Date(0, 0, 0, 8, 2, 1) // when val result = underTest.formatTime(time) @@ -54,7 +54,7 @@ class FinTsUtilsTest { fun formatTime_PM() { // given - val time = Date(18, 22, 51) + val time = Date(0, 0, 0, 18, 22, 51) // when val result = underTest.formatTime(time) @@ -67,7 +67,7 @@ class FinTsUtilsTest { fun formatTimeAsInt_AM() { // given - val time = Date(8, 2, 1) + val time = Date(0, 0, 0, 8, 2, 1) // when val result = underTest.formatTimeAsInt(time) @@ -80,7 +80,7 @@ class FinTsUtilsTest { fun formatTimeAsInt_PM() { // given - val time = Date(18, 22, 51) + val time = Date(0, 0, 0, 18, 22, 51) // when val result = underTest.formatTimeAsInt(time)