Skip to content

Commit

Permalink
409: Missing symbol for some derived units
Browse files Browse the repository at this point in the history
Task-Url: #409
  • Loading branch information
keilw committed Jan 7, 2024
1 parent b3738a2 commit b81cac0
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 15 deletions.
13 changes: 9 additions & 4 deletions src/main/java/tech/units/indriya/format/SimpleUnitFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
* @author <a href="mailto:[email protected]">Werner Keil</a>
* @author Eric Russell
* @author Andi Huber
* @version 2.10, June 6, 2023
* @version 2.11, Jan 7, 2024
* @since 1.0
*/
public abstract class SimpleUnitFormat extends AbstractUnitFormat {
Expand Down Expand Up @@ -446,16 +446,21 @@ private DefaultFormat init() {
alias(Units.CELSIUS.prefix(MetricPrefix.values()[i]), METRIC_PREFIX_SYMBOLS[i] + "°C");
}

// Additional cases and aliases
label(Units.PERCENT, "%");
label(Units.METRE, "m");
label(Units.SECOND, "s");
label(Units.MINUTE, "min");
label(Units.HOUR, "h");
label(Units.DAY, "day");
alias(Units.DAY, "d");
label(Units.DAY, "d");
alias(Units.DAY, "day");
label(Units.WEEK, "week");
label(Units.YEAR, "year");
alias(Units.WEEK, "wk");
label(Units.YEAR, "yr");
alias(Units.YEAR, "y");
alias(Units.YEAR, "year");
alias(Units.YEAR, "days365");
alias(Units.YEAR, "a");
label(Units.MONTH, "mo");
alias(Units.MONTH, "mon");
alias(Units.MONTH, "month");
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/tech/units/indriya/unit/Units.java
Original file line number Diff line number Diff line change
Expand Up @@ -522,19 +522,19 @@ public String getName() {
new TransformedUnit<>("d", "Day", SECOND, SECOND, MultiplyConverter.ofRational(24 * 60 * 60, 1)));

/**
* A unit of duration equal to 7 {@link #DAY} (standard name <code>wk</code>).
* A unit of duration equal to 7 {@link #DAY} (common name <code>wk</code>).
*/
public static final Unit<Time> WEEK = AbstractSystemOfUnits.Helper.addUnit(INSTANCE.units,
DAY.multiply(7), "Week", "wk");

/**
* A time unit accepted for use with SI units (standard name <code>y</code> ).
* A time unit accepted for use with SI units (standard name <code>yr</code> ).
*/
public static final Unit<Time> YEAR = AbstractSystemOfUnits.Helper.addUnit(INSTANCE.units,
Units.DAY.multiply(365.2425), "Year", "y");
Units.DAY.multiply(365.2425), "Year", "yr");

/**
* A unit of duration equal to 1/12 {@link #YEAR} (standard name <code>mo</code>).
* A unit of duration equal to 1/12 {@link #YEAR} (common name <code>mo</code>).
* @since 2.3
*/
public static final Unit<Time> MONTH = AbstractSystemOfUnits.Helper.addUnit(INSTANCE.units,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public void testFormatCompDelimsMultipleQuantities() {
final Quantity<Time>[] timeQuants = new Quantity[] { Quantities.getQuantity(3, Units.DAY), Quantities.getQuantity(4, Units.HOUR),
Quantities.getQuantity(48, Units.MINUTE)};
final MixedQuantity<Time> t1 = MixedQuantity.of(timeQuants);
assertEquals("3_day 4_h 48_min", format1.format(t1));
assertEquals("3_d 4_h 48_min", format1.format(t1));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public void testFormatPrefix() {
@Test
public void testFormatDay() {
logger.log(LOG_LEVEL, format.format(DAY));
assertEquals("day", format.format(DAY));
assertEquals("d", format.format(DAY));
}

@Test
Expand All @@ -319,6 +319,7 @@ public void testFormatMonth() {
@Test
public void testParseDay() {
logger.log(LOG_LEVEL, format.format(DAY));
assertEquals(DAY, format.parse("d"));
assertEquals(DAY, format.parse("day"));
}

Expand Down
22 changes: 20 additions & 2 deletions src/test/java/tech/units/indriya/format/SimpleUnitFormatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import static tech.units.indriya.unit.Units.DAY;
import static tech.units.indriya.unit.Units.WEEK;
import static tech.units.indriya.unit.Units.MONTH;
import static tech.units.indriya.unit.Units.YEAR;

import java.math.BigInteger;
import java.util.logging.Level;
Expand Down Expand Up @@ -311,7 +312,7 @@ public void testPrefix() {
@Test
public void testFormatDay() {
logger.log(LOG_LEVEL, format.format(DAY));
assertEquals("day", format.format(DAY));
assertEquals("d", format.format(DAY));
}

@Test
Expand All @@ -320,6 +321,12 @@ public void testFormatWeek() {
assertEquals("week", format.format(WEEK));
}

@Test
public void testFormatYear() {
logger.log(LOG_LEVEL, format.format(YEAR));
assertEquals("yr", format.format(YEAR));
}

@Test
public void testFormatMonth() {
logger.log(LOG_LEVEL, format.format(MONTH));
Expand All @@ -328,19 +335,30 @@ public void testFormatMonth() {

@Test
public void testParseDay() {
logger.log(LOG_LEVEL, format.format(DAY));
logger.log(LOG_LEVEL, format.format(DAY));
assertEquals(DAY, format.parse("d"));
assertEquals(DAY, format.parse("day"));
}

@Test
public void testParseWeek() {
logger.log(LOG_LEVEL, format.format(WEEK));
assertEquals(WEEK, format.parse("week"));
assertEquals(WEEK, format.parse("wk"));
}

@Test
public void testParseYear() {
logger.log(LOG_LEVEL, format.format(YEAR));
assertEquals(YEAR, format.parse("yr"));
assertEquals(YEAR, format.parse("y"));
assertEquals(YEAR, format.parse("year"));
}

@Test
public void testParseMonth() {
logger.log(LOG_LEVEL, format.format(MONTH));
assertEquals(MONTH, format.parse("month"));
assertEquals(MONTH, format.parse("mo"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ public void testTimes() {
Quantities.getQuantity(4, Units.HOUR), Quantities.getQuantity(48, Units.MINUTE) };
final MixedQuantity<Time> time = MixedQuantity.of(quants);

assertEquals("[day, h, min]", time.getUnits().toString());
assertEquals("3 day 4 h 48 min", time.toString());
assertEquals("[d, h, min]", time.getUnits().toString());
assertEquals("3 d 4 h 48 min", time.toString());

assertNumberEquals(3.2d, time.to(Units.DAY).getValue(), 1E-28);
assertNumberEquals(4608, time.to(Units.MINUTE).getValue(), 1E-28);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/tech/units/indriya/unit/ASUnitsTimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void testByNameHourIgnoreCase() {

@Test
public void testByStringDay() {
final Unit<?> u = sou.getUnit("day");
final Unit<?> u = sou.getUnit("d");
assertNotNull(u);
assertEquals(DAY, u);
}
Expand Down

0 comments on commit b81cac0

Please sign in to comment.