Implemented setting time on Date; fixed formatting time tests

This commit is contained in:
dankito 2020-12-12 00:24:36 +01:00
parent 095d9c467a
commit d43d91b6cc
4 changed files with 15 additions and 11 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)