Skip to content

Commit

Permalink
of()-Methode akzeptiert TimeUnit als zusätzlichen Parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Oli B committed Feb 27, 2024
1 parent a14712b commit 70b4ece
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/main/kotlin/de/jfachwert/zeit/Zeitpunkt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import java.time.format.DateTimeFormatterBuilder
import java.time.format.DateTimeParseException
import java.time.temporal.ChronoField
import java.util.*
import java.util.concurrent.TimeUnit
import java.util.logging.Level
import java.util.logging.Logger

Expand Down Expand Up @@ -326,14 +327,26 @@ constructor(t: BigInteger): AbstractFachwert<BigInteger, Zeitpunkt>(t) {
/**
* Liefert einen Zeitpunkt zurueck.
*
* @param code beliebige Zahl
* @param code Anzahl ns seit 1.1.1970
* @return der Zeitpunkt
*/
@JvmStatic
fun of(code: BigInteger): Zeitpunkt {
return WEAK_CACHE.computeIfAbsent(code) { n: BigInteger -> Zeitpunkt(n) }
}

/**
* Liefert einen Zeitpunkt zurueck.
*
* @param code Anzahl ns seit 1.1.1970
* @param unit Zeiteinheit
* @return der Zeitpunkt
*/
@JvmStatic
fun of(code: BigInteger, unit: TimeUnit): Zeitpunkt {
return of(code.multiply(BigInteger.valueOf(unit.toNanos(1))))
}

/**
* Liefert einen Zeitpunkt zurueck.
*
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/de/jfachwert/zeit/ZeitpunktTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.time.ZoneOffset;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
Expand Down Expand Up @@ -169,6 +170,12 @@ void testOfDate() {
assertEquals(Zeitpunkt.EPOCH, z);
}

@Test
void testOfTimeUnit() {
Zeitpunkt t1 = Zeitpunkt.of(BigInteger.ONE, TimeUnit.DAYS);
assertEquals(LocalDate.of(1970, 1, 2), t1.toLocalDate());
}

@Test
void testMin() {
String s = Zeitpunkt.MIN.toString();
Expand Down

0 comments on commit 70b4ece

Please sign in to comment.