View Javadoc
1   package org.djunits.unit;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertTrue;
5   
6   import java.util.Locale;
7   
8   import org.djunits.unit.unitsystem.UnitSystem;
9   import org.junit.Before;
10  import org.junit.Test;
11  
12  /**
13   * <p>
14   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
16   * <p>
17   * @author <a href="https://tudelft.nl/pknoppers">Peter Knoppers</a>
18   */
19  public class TimeUnitTest extends AbstractOffsetUnitTest<TimeUnit>
20  {
21      /**
22       * Set the locale to "en" so we know what texts should be retrieved from the resources.
23       */
24      @Before
25      public final void setup()
26      {
27          Locale.setDefault(new Locale("en"));
28      }
29  
30      /**
31       * Verify conversion factors, English names and abbreviations.
32       */
33      @Test
34      public final void conversions()
35      {
36          assertEquals("s", TimeUnit.DEFAULT.getQuantity().getSiDimensions().toString(true, false));
37          checkUnitRatioOffsetNameAndAbbreviation(TimeUnit.BASE_SECOND, 1, 0.0, 0.00000001, "second", "s");
38          checkUnitRatioOffsetNameAndAbbreviation(TimeUnit.BASE_HOUR, 3600, 0.0, 0.0005, "hour", "h");
39          checkUnitRatioOffsetNameAndAbbreviation(TimeUnit.BASE_DAY, 86400, 0.0, 0.001, "day", "day");
40          // Check two conversions between non-standard units
41          assertEquals("one DAY is 24 HOUR", 24, getMultiplicationFactorTo(TimeUnit.BASE_DAY, TimeUnit.BASE_HOUR), 0.0001);
42          assertEquals("one HOUR is about 0.0417 DAY", 0.0417, getMultiplicationFactorTo(TimeUnit.BASE_HOUR, TimeUnit.BASE_DAY),
43                  0.0001);
44          // Check conversion factor to standard unit for all remaining time units
45          checkUnitRatioOffsetNameAndAbbreviation(TimeUnit.BASE_MILLISECOND, 0.001, 0.0, 0.00000001, "millisecond", "ms");
46          checkUnitRatioOffsetNameAndAbbreviation(TimeUnit.BASE_MINUTE, 60, 0.0, 0.000001, "minute", "min");
47          checkUnitRatioOffsetNameAndAbbreviation(TimeUnit.BASE_WEEK, 7 * 86400, 0.0, 0.1, "week", "wk");
48  
49          // TODO: shifted units
50      }
51  
52      /**
53       * Verify that we can create our own duration unit.
54       */
55      @Test
56      public final void createTimeUnit()
57      {
58          TimeUnit myTU = TimeUnit.BASE_SECOND.deriveLinearOffset(14.0 * 86400, 0.0, DurationUnit.SECOND, "fn", "Fortnight",
59                  UnitSystem.OTHER);
60          assertTrue("Can create a new TimeUnit", null != myTU);
61          checkUnitRatioOffsetNameAndAbbreviation(myTU, 14 * 86400, 0.0, 1, "Fortnight", "fn");
62          TimeUnit.BASE.unregister(myTU);
63      }
64  
65      /**
66       * Verify relative base unit.
67       */
68      @Test
69      public final void testRelative()
70      {
71          assertEquals(DurationUnit.BASE, TimeUnit.DEFAULT.getRelativeQuantity());
72      }
73  
74  }