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

More unit conversions #1182

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions core/i18n/src/main/res/values/units.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@
<item quantity="one">millimeter</item>
<item quantity="other">millimeters</item>
</plurals>
<string name="unit_nanometer_symbol">mm</string>
<plurals name="unit_nanometer">
<item quantity="one">nanometer</item>
<item quantity="other">nanometers</item>
</plurals>
<string name="unit_picometer_symbol">mm</string>
TimoNotThy marked this conversation as resolved.
Show resolved Hide resolved
<plurals name="unit_picometer">
<item quantity="one">picometer</item>
<item quantity="other">picometers</item>
</plurals>
<string name="unit_inch_symbol">in</string>
<plurals name="unit_inch">
<item quantity="one">inch</item>
Expand All @@ -68,6 +78,11 @@
<item quantity="one">nautical mile</item>
<item quantity="other">nautical miles</item>
</plurals>
<string name="unit_planck_distance_symbol">lP</string>
<plurals name="unit_planck_distance">
<item quantity="one">Planck</item>
<item quantity="other">Planck</item>
</plurals>
<!-- UNITS OF AREA -->
<string name="unit_sqmeter_symbol">m²</string>
<plurals name="unit_sqmeter">
Expand Down Expand Up @@ -145,6 +160,11 @@
<item quantity="one">year</item>
<item quantity="other">years</item>
</plurals>
<string name="unit_planck_time_symbol">tP</string>
<plurals name="unit_planck_time">
<item quantity="one">Planck time</item>
<item quantity="other">Planck time</item>
</plurals>
<!-- UNITS OF DATA SIZES -->
<string name="unit_byte_symbol">B</string>
<plurals name="unit_byte">
Expand Down Expand Up @@ -278,6 +298,11 @@
<item quantity="one">short ton</item>
<item quantity="other">short tons</item>
</plurals>
<string name="unit_planck_mass_symbol">mP</string>
<plurals name="unit_planck_mass">
<item quantity="one">Planck mass</item>
<item quantity="other">Planck mass</item>
</plurals>
<!-- UNITS OF TEMPERATURE -->
<string name="unit_degree_celsius_symbol">°C</string>
<plurals name="unit_degree_celsius">
Expand All @@ -294,4 +319,9 @@
<item quantity="one">kelvin</item>
<item quantity="other">kelvins</item>
</plurals>
<string name="unit_planck_temperature_symbol">TP</string>
<plurals name="unit_planck_temperature">
<item quantity="one">Planck temperature</item>
<item quantity="other">Planck temperature</item>
</plurals>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ internal class LengthConverter(context: Context) : SimpleFactorConverter() {
context.getString(R.string.unit_millimeter_symbol),
R.plurals.unit_millimeter
),
MeasureUnitWithFactor(
1000_000.0,
context.getString(R.string.unit_nanometer_symbol),
R.plurals.unit_nanometer
),
MeasureUnitWithFactor(
1_000_000_000.0,
context.getString(R.string.unit_picometer_symbol),
R.plurals.unit_picometer
),
MeasureUnitWithFactor(
100 / 2.54,
context.getString(R.string.unit_inch_symbol),
Expand All @@ -57,6 +67,11 @@ internal class LengthConverter(context: Context) : SimpleFactorConverter() {
1 / 1852.0,
context.getString(R.string.unit_nautic_mile_symbol),
R.plurals.unit_nautic_mile
),
MeasureUnitWithFactor(
1.616255e-35,
context.getString(R.string.unit_planck_distance_symbol),
R.plurals.unit_planck_distance
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ internal class MassConverter(context: Context): SimpleFactorConverter() {
1000.0 / (453.59237 * 2000.0),
context.getString(R.string.unit_short_ton_symbol),
R.plurals.unit_short_ton
),
MeasureUnitWithFactor(
2.176434e-8,
context.getString(R.string.unit_planck_mass_symbol),
R.plurals.unit_planck_mass
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ internal class TemperatureConverter(context: Context) : Converter {
R.plurals.unit_kelvin,
TemperatureUnit.Kelvin
),
TemperatureMeasureUnit(
context.getString(R.string.unit_planck_temperature_symbol),
R.plurals.unit_planck_temperature,
TemperatureUnit.Planck
)
)

override suspend fun isValidUnit(symbol: String): Boolean {
Expand Down Expand Up @@ -91,13 +96,32 @@ internal class TemperatureConverter(context: Context) : Converter {
if (from === TemperatureUnit.DegreeFahrenheit && to == TemperatureUnit.DegreeCelsius) {
return (value - 32.0) * (5.0 / 9.0)
}

if (from === TemperatureUnit.Kelvin && to == TemperatureUnit.DegreeFahrenheit) {
return (value - 273.15) * (9.0 / 5.0) + 32.0
}
if (from === TemperatureUnit.DegreeFahrenheit && to == TemperatureUnit.Kelvin) {
return (value - 32.0) * (5.0 / 9.0) + 273.15
}
if (from === TemperatureUnit.Kelvin && to == TemperatureUnit.Planck) {
return value * 1.416833e32
}
if (from === TemperatureUnit.Planck && to == TemperatureUnit.Kelvin) {
return value / 1.416833e32
}
if (from === TemperatureUnit.DegreeCelsius && to == TemperatureUnit.Planck) {
return (value - 273.15) * 1.416833e32
}
if (from === TemperatureUnit.Planck && to == TemperatureUnit.DegreeCelsius) {
return value / 1.416833e32 + 273.15
}
if (from === TemperatureUnit.DegreeFahrenheit && to == TemperatureUnit.Planck) {
return ((value - 32.0) * (5.0 / 9.0) + 273.15) * 1.416833e32
}
if (from === TemperatureUnit.Planck && to == TemperatureUnit.DegreeFahrenheit) {
return ((value / 1.416833e32) - 273.15) * (9.0 / 5.0) + 32.0
}


throw IllegalArgumentException()
}

Expand All @@ -120,4 +144,5 @@ enum class TemperatureUnit {
DegreeCelsius,
DegreeFahrenheit,
Kelvin,
Planck,
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ internal class TimeConverter(context: Context) : SimpleFactorConverter() {
1.0 / (60 * 60 * 24 * 365),
context.getString(R.string.unit_year_symbol),
R.plurals.unit_year
),
MeasureUnitWithFactor(
5.391247e-44,
context.getString(R.string.unit_planck_time_symbol),
R.plurals.unit_planck_time
)
)

Expand Down