Skip to content

Commit

Permalink
Deprecate String.toTimeZone() (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikc5000 authored Jul 15, 2020
1 parent 194c920 commit e5ddf36
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 36 deletions.
8 changes: 5 additions & 3 deletions core/src/commonMain/kotlin/io/islandtime/TimeZone.kt
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,11 @@ fun TimeZone(id: String): TimeZone {
*/
fun UtcOffset.asTimeZone(): TimeZone = TimeZone.FixedOffset(this)

/**
* Convert a string to a [TimeZone].
*/
@Deprecated(
"Use TimeZone() instead.",
ReplaceWith("TimeZone(this)"),
DeprecationLevel.WARNING
)
fun String.toTimeZone() = TimeZone(this)

internal const val MAX_TIME_ZONE_STRING_LENGTH = 50
Expand Down
2 changes: 1 addition & 1 deletion core/src/commonMain/kotlin/io/islandtime/ZonedDateTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ internal fun DateTimeParseResult.toZonedDateTime(): ZonedDateTime? {
val offset = this.toUtcOffset()

return if (dateTime != null && offset != null) {
val zone = timeZoneId?.toTimeZone() ?: offset.asTimeZone()
val zone = timeZoneId?.let { TimeZone(it) } ?: offset.asTimeZone()

// Check if the offset is valid for the time zone as we understand it and if not, adjust the date-time and
// offset to valid values while preserving the instant of the parsed value
Expand Down
12 changes: 6 additions & 6 deletions core/src/commonTest/kotlin/io/islandtime/ZonedDateTimeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ZonedDateTimeTest : AbstractIslandTimeTest() {
assertFailsWith<TimeZoneRulesException> {
ZonedDateTime(
DateTime(2019, 5, 30, 18, 0),
"America/Boston".toTimeZone()
TimeZone("America/Boston")
)
}
}
Expand Down Expand Up @@ -277,22 +277,22 @@ class ZonedDateTimeTest : AbstractIslandTimeTest() {
fun `DEFAULT_SORT_ORDER compares based on instant, then date and time, and then zone`() {
assertTrue {
ZonedDateTime.DEFAULT_SORT_ORDER.compare(
Date(1969, 365) at Time(23, 0) at "America/Chicago".toTimeZone(),
Date(1969, 365) at Time(23, 0) at TimeZone("America/Chicago"),
Date(1970, 1) at Time(0, 0) at nyZone
) < 0
}

assertTrue {
ZonedDateTime.DEFAULT_SORT_ORDER.compare(
Date(1970, 1) at Time(0, 0) at "Etc/GMT+5".toTimeZone(),
Date(1970, 1) at Time(0, 0) at TimeZone("Etc/GMT+5"),
Date(1970, 1) at Time(0, 0) at nyZone
) > 0
}

assertTrue {
ZonedDateTime.DEFAULT_SORT_ORDER.compare(
Date(1969, 365) at Time(23, 0) at "Etc/GMT+5".toTimeZone(),
Date(1969, 365) at Time(23, 0) at "Etc/GMT+5".toTimeZone()
Date(1969, 365) at Time(23, 0) at TimeZone("Etc/GMT+5"),
Date(1969, 365) at Time(23, 0) at TimeZone("Etc/GMT+5")
) == 0
}
}
Expand Down Expand Up @@ -587,7 +587,7 @@ class ZonedDateTimeTest : AbstractIslandTimeTest() {
"2019-11-03T01:30Z[Etc/UTC]",
ZonedDateTime(
DateTime(2019, 11, 3, 1, 30),
"Etc/UTC".toTimeZone()
TimeZone("Etc/UTC")
).toString()
)

Expand Down
20 changes: 10 additions & 10 deletions core/src/commonTest/kotlin/io/islandtime/clock/NowTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class NowTest : AbstractIslandTimeTest() {

@Test
fun `Date_now() with offset`() {
val clock = FixedClock((-1).days, "Etc/GMT+1".toTimeZone())
val clock = FixedClock((-1).days, TimeZone("Etc/GMT+1"))
assertEquals(Date(1969, Month.DECEMBER, 30), Date.now(clock))
}

Expand All @@ -65,7 +65,7 @@ class NowTest : AbstractIslandTimeTest() {

@Test
fun `DateTime_now() with offset`() {
val clock = FixedClock((-1).days, "Etc/GMT+1".toTimeZone())
val clock = FixedClock((-1).days, TimeZone("Etc/GMT+1"))
assertEquals(
DateTime(1969, Month.DECEMBER, 30, 23, 0),
DateTime.now(clock)
Expand All @@ -92,25 +92,25 @@ class NowTest : AbstractIslandTimeTest() {

@Test
fun `Time_now() with offset`() {
val clock1 = FixedClock((-1).days, "Etc/GMT+4".toTimeZone())
val clock1 = FixedClock((-1).days, TimeZone("Etc/GMT+4"))
assertEquals(Time(20, 0), Time.now(clock1))

val clock2 = FixedClock((-1).days, "Etc/GMT-4".toTimeZone())
val clock2 = FixedClock((-1).days, TimeZone("Etc/GMT-4"))
assertEquals(Time(4, 0), Time.now(clock2))
}

@Test
fun `OffsetTime_now()`() {
val clock1 = FixedClock((-1).days, "Etc/GMT+4".toTimeZone())
val clock1 = FixedClock((-1).days, TimeZone("Etc/GMT+4"))
assertEquals(OffsetTime(Time(20, 0), (-4).hours.asUtcOffset()), OffsetTime.now(clock1))

val clock2 = FixedClock((-1).days, "Etc/GMT-4".toTimeZone())
val clock2 = FixedClock((-1).days, TimeZone("Etc/GMT-4"))
assertEquals(OffsetTime(Time(4, 0), 4.hours.asUtcOffset()), OffsetTime.now(clock2))
}

@Test
fun `OffsetDateTime_now()`() {
val clock = FixedClock((-1).days, "Etc/GMT+1".toTimeZone())
val clock = FixedClock((-1).days, TimeZone("Etc/GMT+1"))
assertEquals(
OffsetDateTime(
DateTime(1969, Month.DECEMBER, 30, 23, 0),
Expand All @@ -131,11 +131,11 @@ class NowTest : AbstractIslandTimeTest() {

@Test
fun `ZonedDateTime_now()`() {
val clock = FixedClock((-1).days, "Etc/GMT+1".toTimeZone())
val clock = FixedClock((-1).days, TimeZone("Etc/GMT+1"))
assertEquals(
ZonedDateTime(
DateTime(1969, Month.DECEMBER, 30, 23, 0),
"Etc/GMT+1".toTimeZone()
TimeZone("Etc/GMT+1")
),
ZonedDateTime.now(clock)
)
Expand All @@ -144,7 +144,7 @@ class NowTest : AbstractIslandTimeTest() {
assertEquals(
ZonedDateTime(
DateTime(1969, Month.DECEMBER, 31, 0, 0),
"Etc/GMT+1".toTimeZone()
TimeZone("Etc/GMT+1")
),
ZonedDateTime.now(clock)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals

class PreviousNextTest : AbstractIslandTimeTest() {
private val nyZone = "America/New_York".toTimeZone()
private val nyZone = TimeZone("America/New_York")

@Test
fun `Date_next() returns the next date with a particular day of week`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals

class StartEndTest : AbstractIslandTimeTest() {
private val nyZone = "America/New_York".toTimeZone()
private val nyZone = TimeZone("America/New_York")

@Test
fun `Date_startOfYear returns the date at the start of this date's year`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class OffsetDateTimeIntervalTest : AbstractIslandTimeTest() {
val end = DateTime.MAX at UtcOffset((-4).hours)

assertTrue { start in start..end }
assertTrue { DateTime.MAX at "Etc/UTC".toTimeZone() in start..end }
assertTrue { DateTime.MAX at "America/Denver".toTimeZone() in start..end }
assertTrue { DateTime.MAX at TimeZone("Etc/UTC") in start..end }
assertTrue { DateTime.MAX at TimeZone("America/Denver") in start..end }
assertTrue { Instant.MAX in start..end }
}

Expand All @@ -72,8 +72,8 @@ class OffsetDateTimeIntervalTest : AbstractIslandTimeTest() {

assertTrue { start in start..end }
assertTrue { end in start..end }
assertTrue { DateTime.MIN at "Etc/UTC".toTimeZone() in start..end }
assertTrue { DateTime.MIN at "America/Denver".toTimeZone() in start..end }
assertTrue { DateTime.MIN at TimeZone("Etc/UTC") in start..end }
assertTrue { DateTime.MIN at TimeZone("America/Denver") in start..end }
assertTrue { Instant.MIN in start..end }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class ZonedDateTimeIntervalTest : AbstractIslandTimeTest() {
val end = DateTime.MAX at nyZone

assertTrue { start in start..end }
assertTrue { DateTime.MAX at "Etc/UTC".toTimeZone() in start..end }
assertTrue { DateTime.MAX at "America/Denver".toTimeZone() in start..end }
assertTrue { DateTime.MAX at TimeZone("Etc/UTC") in start..end }
assertTrue { DateTime.MAX at TimeZone("America/Denver") in start..end }
assertTrue { Instant.MAX in start..end }
}

Expand All @@ -70,8 +70,8 @@ class ZonedDateTimeIntervalTest : AbstractIslandTimeTest() {

assertTrue { start in start..end }
assertTrue { end in start..end }
assertTrue { DateTime.MIN at "Etc/UTC".toTimeZone() in start..end }
assertTrue { DateTime.MIN at "America/Denver".toTimeZone() in start..end }
assertTrue { DateTime.MIN at TimeZone("Etc/UTC") in start..end }
assertTrue { DateTime.MIN at TimeZone("America/Denver") in start..end }
assertTrue { Instant.MIN in start..end }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ fun NSTimeZone.toIslandUtcOffsetAt(date: NSDate): UtcOffset {
/**
* Convert an NSTimeZone` to an Island Time [TimeZone] with the same identifier.
*/
fun NSTimeZone.toIslandTimeZone(): TimeZone = name.toTimeZone()
fun NSTimeZone.toIslandTimeZone(): TimeZone = TimeZone(name)

/**
* Convert an Island Time [TimeZone] to an `NSTimeZone`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class ConversionsTest : AbstractIslandTimeTest() {

@Test
fun `convert NSDate to ZonedDateTime`() {
val zone = "America/New_York".toTimeZone()
val zone = TimeZone("America/New_York")

assertEquals(
Instant.UNIX_EPOCH at zone,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.islandtime.extensions.parcelize

import android.os.Parcel
import io.islandtime.TimeZone
import io.islandtime.toTimeZone
import kotlinx.android.parcel.Parceler

object TimeZoneParceler : Parceler<TimeZone> {
Expand All @@ -17,7 +16,7 @@ object TimeZoneParceler : Parceler<TimeZone> {

object NullableTimeZoneParceler : Parceler<TimeZone?> {
override fun create(parcel: Parcel): TimeZone? {
return parcel.readString()?.toTimeZone()
return parcel.readString()?.let { TimeZone(it) }
}

override fun TimeZone?.write(parcel: Parcel, flags: Int) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.islandtime.extensions.serialization

import io.islandtime.TimeZone
import io.islandtime.toTimeZone
import kotlinx.serialization.*

object TimeZoneSerializer : KSerializer<TimeZone> {
Expand All @@ -13,6 +12,6 @@ object TimeZoneSerializer : KSerializer<TimeZone> {
}

override fun deserialize(decoder: Decoder): TimeZone {
return decoder.decodeString().toTimeZone()
return TimeZone(decoder.decodeString())
}
}

0 comments on commit e5ddf36

Please sign in to comment.