Skip to content

Commit 461f12f

Browse files
committed
Add getExpiresAt(), getRegisteredAt(), getUpdatedAt(), and getExpiresInDays() to Dates
1 parent 80ccc05 commit 461f12f

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/main/java/io/rdapapi/client/responses/Dates.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.rdapapi.client.responses;
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import java.time.Instant;
5+
import java.time.temporal.ChronoUnit;
46

57
@JsonIgnoreProperties(ignoreUnknown = true)
68
public final class Dates {
@@ -28,4 +30,43 @@ public String getExpires() {
2830
public String getUpdated() {
2931
return updated;
3032
}
33+
34+
/** Parse the registered date into an Instant, or null if absent or unparseable. */
35+
public Instant getRegisteredAt() {
36+
return parseInstant(registered);
37+
}
38+
39+
/** Parse the expiry date into an Instant, or null if absent or unparseable. */
40+
public Instant getExpiresAt() {
41+
return parseInstant(expires);
42+
}
43+
44+
/** Parse the updated date into an Instant, or null if absent or unparseable. */
45+
public Instant getUpdatedAt() {
46+
return parseInstant(updated);
47+
}
48+
49+
/**
50+
* Days until expiration, or null if no expiry date is available.
51+
*
52+
* <p>Returns a negative number if the domain has already expired.
53+
*/
54+
public Long getExpiresInDays() {
55+
Instant exp = getExpiresAt();
56+
if (exp == null) {
57+
return null;
58+
}
59+
return ChronoUnit.DAYS.between(Instant.now(), exp);
60+
}
61+
62+
private static Instant parseInstant(String value) {
63+
if (value == null) {
64+
return null;
65+
}
66+
try {
67+
return Instant.parse(value);
68+
} catch (Exception e) {
69+
return null;
70+
}
71+
}
3172
}

src/test/java/io/rdapapi/client/ResponsesTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,27 @@ void parseBulkDomainResponse() throws Exception {
117117
assertThat(failure.getData()).isNull();
118118
}
119119

120+
@Test
121+
void datesConvenienceMethods() throws Exception {
122+
DomainResponse r = RdapClient.MAPPER.readValue(Fixtures.domainResponse(), DomainResponse.class);
123+
assertThat(r.getDates().getRegisteredAt()).isNotNull();
124+
assertThat(r.getDates().getRegisteredAt().toString()).isEqualTo("1997-09-15T04:00:00Z");
125+
assertThat(r.getDates().getExpiresAt()).isNotNull();
126+
assertThat(r.getDates().getExpiresInDays()).isGreaterThan(0);
127+
assertThat(r.getDates().getUpdatedAt()).isNull();
128+
}
129+
130+
@Test
131+
void datesNullFieldsReturnNull() throws Exception {
132+
String json =
133+
"{\"domain\":\"test.com\",\"registrar\":{},\"dates\":{},\"entities\":{},\"meta\":{"
134+
+ "\"rdap_server\":\"x\",\"raw_rdap_url\":\"x\",\"cached\":false,\"cache_expires\":\"x\"}}";
135+
DomainResponse r = RdapClient.MAPPER.readValue(json, DomainResponse.class);
136+
assertThat(r.getDates().getRegisteredAt()).isNull();
137+
assertThat(r.getDates().getExpiresAt()).isNull();
138+
assertThat(r.getDates().getExpiresInDays()).isNull();
139+
}
140+
120141
@Test
121142
void unknownFieldsIgnored() throws Exception {
122143
String json =

0 commit comments

Comments
 (0)