Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove implicit defaultLocale() from APIs, expose locale creation by … #116

Merged
merged 1 commit into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions core/src/commonMain/kotlin/io/islandtime/DayOfWeek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import io.islandtime.format.DateTimeTextProvider
import io.islandtime.format.TextStyle
import io.islandtime.internal.DAYS_PER_WEEK
import io.islandtime.locale.Locale
import io.islandtime.locale.defaultLocale
import io.islandtime.measures.IntDays
import io.islandtime.measures.LongDays
import io.islandtime.measures.days
Expand Down Expand Up @@ -43,15 +42,15 @@ enum class DayOfWeek {
fun number(locale: Locale): Int = (this - (locale.firstDayOfWeek.number - 1).days).number

/**
* The localized name of the day, if available for the locale in the specified style. The result depends on the
* The localized name of the day, if available for the [locale] in the specified style. The result depends on the
* configured [DateTimeTextProvider] and may differ between platforms.
*
* @param style the style of text
* @param locale the locale
* @return the localized name or `null` if unavailable for the specified locale
* @see displayName
*/
fun localizedName(style: TextStyle, locale: Locale = defaultLocale()): String? {
fun localizedName(style: TextStyle, locale: Locale): String? {
return DateTimeTextProvider.dayOfWeekTextFor(number.toLong(), style, locale)
}

Expand All @@ -66,7 +65,7 @@ enum class DayOfWeek {
* @return the localized name or [number] if unavailable for the specified locale
* @see localizedName
*/
fun displayName(style: TextStyle, locale: Locale = defaultLocale()): String {
fun displayName(style: TextStyle, locale: Locale): String {
return localizedName(style, locale) ?: number.toString()
}

Expand Down
7 changes: 3 additions & 4 deletions core/src/commonMain/kotlin/io/islandtime/Month.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.islandtime
import io.islandtime.format.DateTimeTextProvider
import io.islandtime.format.TextStyle
import io.islandtime.locale.Locale
import io.islandtime.locale.defaultLocale
import io.islandtime.measures.IntDays
import io.islandtime.measures.IntMonths
import io.islandtime.measures.LongMonths
Expand Down Expand Up @@ -99,15 +98,15 @@ enum class Month {
}

/**
* The localized name of the month, if available for the locale in the specified style. The result depends on the
* The localized name of the month, if available for the [locale] in the specified style. The result depends on the
* configured [DateTimeTextProvider] and may differ between platforms.
*
* @param style the style of text
* @param locale the locale
* @return the localized name or `null` if unavailable for the specified locale
* @see displayName
*/
fun localizedName(style: TextStyle, locale: Locale = defaultLocale()): String? {
fun localizedName(style: TextStyle, locale: Locale): String? {
return DateTimeTextProvider.monthTextFor(number.toLong(), style, locale)
}

Expand All @@ -122,7 +121,7 @@ enum class Month {
* @return the localized name or [number] if unavailable for the specified locale
* @see localizedName
*/
fun displayName(style: TextStyle, locale: Locale = defaultLocale()): String {
fun displayName(style: TextStyle, locale: Locale): String {
return localizedName(style, locale) ?: number.toString()
}

Expand Down
9 changes: 4 additions & 5 deletions core/src/commonMain/kotlin/io/islandtime/TimeZone.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import io.islandtime.format.TimeZoneTextProvider
import io.islandtime.format.TimeZoneTextStyle
import io.islandtime.internal.systemDefaultTimeZone
import io.islandtime.locale.Locale
import io.islandtime.locale.defaultLocale
import io.islandtime.measures.nanoseconds
import io.islandtime.measures.seconds
import io.islandtime.parser.*
Expand Down Expand Up @@ -56,7 +55,7 @@ sealed class TimeZone : Comparable<TimeZone> {
fun validated(): TimeZone = apply { validate() }

/**
* The localized name of this time zone, if available for the locale in the specified style. The result depends on
* The localized name of this time zone, if available for the [locale] in the specified style. The result depends on
* the configured [TimeZoneTextProvider] and may differ between platforms.
*
* Example output for the "America/New_York" ID and "en-US" locale:
Expand All @@ -69,13 +68,13 @@ sealed class TimeZone : Comparable<TimeZone> {
*
* @see displayName
*/
fun localizedName(style: TimeZoneTextStyle, locale: Locale = defaultLocale()): String? {
fun localizedName(style: TimeZoneTextStyle, locale: Locale): String? {
return TimeZoneTextProvider.timeZoneTextFor(this, style, locale)
}

/**
* A textual representation of this time zone, suitable for display purposes. The localized name will be returned,
* if available for the locale in the specified style. If not, the [id] will be returned instead.
* if available for the [locale] in the specified style. If not, the [id] will be returned instead.
*
* The result depends on the configured [TimeZoneTextProvider] and may differ between platforms.
*
Expand All @@ -90,7 +89,7 @@ sealed class TimeZone : Comparable<TimeZone> {
* @see localizedName
* @see id
*/
fun displayName(style: TimeZoneTextStyle, locale: Locale = defaultLocale()): String {
fun displayName(style: TimeZoneTextStyle, locale: Locale): String {
return localizedName(style, locale) ?: id
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.islandtime.format
import io.islandtime.locale.Locale

/**
* Defines the set of characters that should be used when parsing or formatting numbers.
* The set of characters that should be used when parsing or formatting numbers.
*
* @property zeroDigit The character that represents zero.
* @property plusSign A list of allowed plus sign characters. The first element will be used when formatting.
Expand Down
9 changes: 7 additions & 2 deletions core/src/commonMain/kotlin/io/islandtime/locale/Locale.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ package io.islandtime.locale
expect class Locale

/**
* Get the current [Locale].
* Gets the current [Locale].
*
* On the JVM, the `Category` is not used in order to support older Android versions.
*/
expect fun defaultLocale(): Locale

internal expect fun localeOf(identifier: String): Locale
/**
* Converts an IETF BCP 47 language tag, such as "en-US" or "de-DE", to a [Locale].
*/
expect fun String.toLocale(): Locale
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.islandtime.Time.Companion.MIDNIGHT
import io.islandtime.calendar.WeekSettings
import io.islandtime.calendar.WeekSettings.Companion.ISO
import io.islandtime.calendar.WeekSettings.Companion.SUNDAY_START
import io.islandtime.locale.localeOf
import io.islandtime.locale.toLocale
import io.islandtime.measures.hours
import io.islandtime.measures.days
import io.islandtime.measures.weeks
Expand All @@ -18,7 +18,7 @@ import kotlin.test.assertTrue

class DatePropertiesTest : AbstractIslandTimeTest() {
@Suppress("PrivatePropertyName")
private val en_US = localeOf("en-US")
private val en_US = "en-US".toLocale()

private val nyZone = TimeZone("America/New_York")

Expand Down
8 changes: 4 additions & 4 deletions core/src/commonTest/kotlin/io/islandtime/DayOfWeekTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.islandtime

import io.islandtime.calendar.WeekSettings
import io.islandtime.format.TextStyle
import io.islandtime.locale.localeOf
import io.islandtime.locale.toLocale
import io.islandtime.measures.days
import io.islandtime.test.AbstractIslandTimeTest
import kotlin.test.Test
Expand All @@ -11,9 +11,9 @@ import kotlin.test.assertFailsWith

@Suppress("PrivatePropertyName")
class DayOfWeekTest : AbstractIslandTimeTest() {
private val en_US = localeOf("en-US")
private val de_DE = localeOf("de-DE")
private val ar_EG = localeOf("ar-EG")
private val en_US = "en-US".toLocale()
private val de_DE = "de-DE".toLocale()
private val ar_EG = "ar-EG".toLocale()

@Test
fun `Int_toDayOfWeek() throws an exception when the number is out of range`() {
Expand Down
4 changes: 2 additions & 2 deletions core/src/commonTest/kotlin/io/islandtime/MonthTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.islandtime

import io.islandtime.format.TextStyle
import io.islandtime.locale.localeOf
import io.islandtime.locale.toLocale
import io.islandtime.measures.days
import io.islandtime.measures.months
import io.islandtime.test.AbstractIslandTimeTest
Expand Down Expand Up @@ -78,7 +78,7 @@ class MonthTest : AbstractIslandTimeTest() {

@Test
fun `localizedName() and displayName() get localized text from the provider`() {
@Suppress("LocalVariableName") val en_US = localeOf("en-US")
@Suppress("LocalVariableName") val en_US = "en-US".toLocale()
assertEquals("April", Month.APRIL.localizedName(TextStyle.FULL_STANDALONE, en_US))
assertEquals("Jun", Month.JUNE.displayName(TextStyle.SHORT_STANDALONE, en_US))
}
Expand Down
10 changes: 5 additions & 5 deletions core/src/commonTest/kotlin/io/islandtime/TimeZoneTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.islandtime

import io.islandtime.format.TimeZoneTextStyle
import io.islandtime.locale.localeOf
import io.islandtime.locale.toLocale
import io.islandtime.measures.hours
import io.islandtime.test.AbstractIslandTimeTest
import io.islandtime.zone.TimeZoneRulesException
Expand Down Expand Up @@ -91,27 +91,27 @@ class TimeZoneTest : AbstractIslandTimeTest() {
fun `localizedName() and displayName() get localized text from the provider`() {
assertEquals(
"Greenwich Mean Time",
TimeZone("Europe/London").localizedName(TimeZoneTextStyle.STANDARD, localeOf("en-GB"))
TimeZone("Europe/London").localizedName(TimeZoneTextStyle.STANDARD, "en-GB".toLocale())
)
assertEquals(
"Greenwich Mean Time",
TimeZone("Europe/London").displayName(TimeZoneTextStyle.STANDARD, localeOf("en-GB"))
TimeZone("Europe/London").displayName(TimeZoneTextStyle.STANDARD, "en-GB".toLocale())
)
}

@Test
fun `displayName() returns the ID on a fixed offset zone`() {
assertEquals(
"+01:00",
TimeZone("+01:00").displayName(TimeZoneTextStyle.STANDARD, localeOf("en-GB"))
TimeZone("+01:00").displayName(TimeZoneTextStyle.STANDARD, "en-GB".toLocale())
)
}

@Test
fun `displayName() returns the ID on an invalid zone`() {
assertEquals(
"America/Buffalo",
TimeZone("America/Buffalo").displayName(TimeZoneTextStyle.STANDARD, localeOf("en-US"))
TimeZone("America/Buffalo").displayName(TimeZoneTextStyle.STANDARD, "en-US".toLocale())
)
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/commonTest/kotlin/io/islandtime/WeekDateTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.islandtime

import io.islandtime.calendar.WeekSettings.Companion.SUNDAY_START
import io.islandtime.locale.localeOf
import io.islandtime.locale.toLocale
import io.islandtime.test.AbstractIslandTimeTest
import io.islandtime.test.TestData
import kotlin.test.Test
Expand All @@ -10,7 +10,7 @@ import kotlin.test.assertFailsWith

class WeekDateTest : AbstractIslandTimeTest() {
@Suppress("PrivatePropertyName")
private val en_US = localeOf("en-US")
private val en_US = "en-US".toLocale()

@Test
fun `Date_toWeekDate() converts to ISO week date`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package io.islandtime.format

import io.islandtime.DateTimeException
import io.islandtime.base.DateTimeField
import io.islandtime.locale.localeOf
import io.islandtime.locale.toLocale
import io.islandtime.test.AbstractIslandTimeTest
import kotlin.test.*

@Suppress("PrivatePropertyName")
class DateTimeTextProviderTest : AbstractIslandTimeTest() {
private val en_US = localeOf("en-US")
private val de_DE = localeOf("de-DE")
private val pl_PL = localeOf("pl-PL")
private val en_US = "en-US".toLocale()
private val de_DE = "de-DE".toLocale()
private val pl_PL = "pl-PL".toLocale()

@Test
fun `textFor() throws an exception when value is out of range`() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package io.islandtime.format

import io.islandtime.locale.localeOf
import io.islandtime.locale.toLocale
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.todo

@Suppress("PrivatePropertyName")
class NumberStyleTest {
private val en_US = localeOf("en-US")
private val de_DE = localeOf("de-DE")
private val hi_IN_u_nu_native = localeOf("hi-IN-u-nu-native")
private val en_US = "en-US".toLocale()
private val de_DE = "de-DE".toLocale()
private val hi_IN_u_nu_native = "hi-IN-u-nu-native".toLocale()

@Test
fun `throws an exception when given any empty list`() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package io.islandtime.format

import io.islandtime.TimeZone
import io.islandtime.locale.localeOf
import io.islandtime.locale.toLocale
import io.islandtime.test.AbstractIslandTimeTest
import kotlin.test.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNull

@Suppress("PrivatePropertyName")
class TimeZoneTextProviderTest : AbstractIslandTimeTest() {
private val en_US = localeOf("en-US")
private val de_DE = localeOf("de-DE")
private val en_US = "en-US".toLocale()
private val de_DE = "de-DE".toLocale()

@Test
fun `timeZoneTextFor() returns null when given a fixed offset time zone`() {
Expand Down
Loading