mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-09 18:29:39 +02:00
Add test and documentation to DateExtensions
This commit is contained in:
parent
070780675e
commit
71168d2091
2 changed files with 54 additions and 1 deletions
|
@ -14,7 +14,6 @@ import java.util.*
|
||||||
* Since '0' is a special date value in the OpenPGP specification (e.g. '0' means no expiration for
|
* Since '0' is a special date value in the OpenPGP specification (e.g. '0' means no expiration for
|
||||||
* expiration dates), this method will return 'null' if seconds is 0.
|
* expiration dates), this method will return 'null' if seconds is 0.
|
||||||
*
|
*
|
||||||
* @param date date
|
|
||||||
* @param seconds number of seconds to be added
|
* @param seconds number of seconds to be added
|
||||||
* @return date plus seconds or null if seconds is '0'
|
* @return date plus seconds or null if seconds is '0'
|
||||||
*/
|
*/
|
||||||
|
@ -25,9 +24,19 @@ fun Date.plusSeconds(seconds: Long): Date? {
|
||||||
return if (seconds == 0L) null else Date(this.time + 1000 * seconds)
|
return if (seconds == 0L) null else Date(this.time + 1000 * seconds)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** This date in seconds since epoch. */
|
||||||
val Date.asSeconds: Long
|
val Date.asSeconds: Long
|
||||||
get() = time / 1000
|
get() = time / 1000
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the number of seconds that would need to be added to this date in order to reach the later
|
||||||
|
* date. Note: This method requires the [later] date to be indeed greater or equal to this date,
|
||||||
|
* otherwise an [IllegalArgumentException] is thrown.
|
||||||
|
*
|
||||||
|
* @param later later date
|
||||||
|
* @return difference between this and [later] in seconds
|
||||||
|
* @throws IllegalArgumentException if later is not greater or equal to this date
|
||||||
|
*/
|
||||||
fun Date.secondsTill(later: Date): Long {
|
fun Date.secondsTill(later: Date): Long {
|
||||||
require(this <= later) { "Timestamp MUST be before the later timestamp." }
|
require(this <= later) { "Timestamp MUST be before the later timestamp." }
|
||||||
return later.asSeconds - this.asSeconds
|
return later.asSeconds - this.asSeconds
|
||||||
|
|
44
pgpainless-core/src/test/java/openpgp/DateExtensionTest.java
Normal file
44
pgpainless-core/src/test/java/openpgp/DateExtensionTest.java
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
// SPDX-FileCopyrightText: 2025 Paul Schaub <vanitasvitae@fsfe.org>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package openpgp;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
public class DateExtensionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDatePlusSecondsBaseCase() {
|
||||||
|
Date t0 = DateExtensionsKt.parseUTC("2025-05-12 10:36:53 UTC");
|
||||||
|
Date t1 = DateExtensionsKt.plusSeconds(t0, 7);
|
||||||
|
assertEquals("2025-05-12 10:37:00 UTC", DateExtensionsKt.formatUTC(t1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDatePlusZeroReturnsNull() {
|
||||||
|
Date t0 = DateExtensionsKt.parseUTC("2025-05-12 10:36:53 UTC");
|
||||||
|
Date t1 = DateExtensionsKt.plusSeconds(t0, 0);
|
||||||
|
assertNull(t1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDatePlusSecondsOverflowing() {
|
||||||
|
Date now = new Date();
|
||||||
|
// expect IAE because of time field overflowing
|
||||||
|
assertThrows(IllegalArgumentException.class, () ->
|
||||||
|
DateExtensionsKt.plusSeconds(now, Long.MAX_VALUE - 10000));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParsingMalformedUTCTimestampThrows() {
|
||||||
|
assertThrows(IllegalArgumentException.class, () ->
|
||||||
|
DateExtensionsKt.parseUTC("2025-05-12 10:36:XX UTC"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue