Skip to content

Commit 33f79df

Browse files
committed
added support for time conversion using timezone/convert API
1 parent 04e83e0 commit 33f79df

File tree

3 files changed

+458
-0
lines changed

3 files changed

+458
-0
lines changed

src/main/java/io/ipgeolocation/api/IPGeolocationAPI.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,37 @@ public Timezone getTimezone(TimezoneParams params) {
193193
return timezone;
194194
}
195195

196+
/**
197+
* Converts a time from one timezone to another using the IP Geolocation API.
198+
*
199+
* @param params The parameters for timezone conversion, including the source and target timezones,
200+
* as well as the time to convert.
201+
* @return A {@code TimezoneConvert} object representing the converted time.
202+
* @throws IPGeolocationError If an error occurs during the timezone conversion process,
203+
* such as an invalid API response or parameters.
204+
* @see TimezoneConvertParams
205+
* @see TimezoneConvert
206+
*/
207+
public TimezoneConvert convertTimeZone(TimezoneConvertParams params) {
208+
final JSONObject apiResponse =
209+
(JSONObject)
210+
callAPIEndpoint(
211+
"timezone/convert",
212+
buildTimezoneConvertUrlParams(params),
213+
"GET",
214+
null,
215+
false);
216+
final TimezoneConvert timezoneConvert;
217+
218+
try {
219+
timezoneConvert = new TimezoneConvert(apiResponse);
220+
} catch (IllegalArgumentException e) {
221+
throw new IPGeolocationError(e);
222+
}
223+
224+
return timezoneConvert;
225+
}
226+
196227
/**
197228
* Retrieves user-agent information based on the provided user-agent string.
198229
*
@@ -398,6 +429,51 @@ private String buildTimezoneUrlParams(final TimezoneParams params) {
398429
return urlParams.toString();
399430
}
400431

432+
private String buildTimezoneConvertUrlParams(final TimezoneConvertParams params) {
433+
final StringBuilder urlParams = new StringBuilder();
434+
435+
urlParams.append("apiKey=");
436+
urlParams.append(apiKey);
437+
438+
if (!Objects.isNull(params)) {
439+
if ((!Strings.isNullOrEmpty(params.getTimezoneFrom())) && (!Strings.isNullOrEmpty(params.getTimezoneTo()))) {
440+
urlParams.append("&tz_from=");
441+
urlParams.append(params.getTimezoneFrom());
442+
443+
urlParams.append("&tz_to=");
444+
urlParams.append(params.getTimezoneTo());
445+
}
446+
447+
if ((!Objects.isNull(params.getLatitudeFrom()) && !Objects.isNull(params.getLongitudeFrom()))
448+
&& ((!Objects.isNull(params.getLatitudeTo()) && !Objects.isNull(params.getLongitudeTo())))) {
449+
urlParams.append("&lat_from=");
450+
urlParams.append(params.getLatitudeFrom());
451+
urlParams.append("&long_from=");
452+
urlParams.append(params.getLongitudeFrom());
453+
454+
urlParams.append("&lat_to=");
455+
urlParams.append(params.getLatitudeTo());
456+
urlParams.append("&long_to=");
457+
urlParams.append(params.getLongitudeTo());
458+
}
459+
460+
if ((!Strings.isNullOrEmpty(params.getLocationFrom())) && (!Strings.isNullOrEmpty(params.getLocationTo()))) {
461+
urlParams.append("&location_from=");
462+
urlParams.append(params.getLocationFrom());
463+
464+
urlParams.append("&location_to=");
465+
urlParams.append(params.getLocationTo());
466+
}
467+
468+
if (!Strings.isNullOrEmpty(params.getTime())) {
469+
urlParams.append("&time=");
470+
urlParams.append(params.getTime());
471+
}
472+
}
473+
474+
return urlParams.toString();
475+
}
476+
401477
private String buildAstronomyUrlParams(final AstronomyParams params) {
402478
final StringBuilder urlParams = new StringBuilder();
403479

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package io.ipgeolocation.api;
2+
3+
import org.json.JSONObject;
4+
5+
import java.math.BigDecimal;
6+
import java.util.Objects;
7+
8+
/**
9+
* The TimezoneConvert class represents the result of a timezone conversion, including the original time, converted time,
10+
* and the difference in hours and minutes between the two timezones.
11+
*
12+
* <p>The TimezoneConvert class provides methods to access the information stored in the TimezoneConvert object.
13+
* For example, you can use the {@link #getConvertedTime()} method to get the converted time.
14+
*/
15+
public class TimezoneConvert {
16+
private final String originalTime;
17+
private final String convertedTime;
18+
private final BigDecimal diffHour;
19+
private final BigDecimal diffMin;
20+
private final JSONObject json;
21+
22+
/**
23+
* Constructs a new TimezoneConvert object with the given JSON data.
24+
*
25+
* @param json The JSON object containing the timezone conversion information.
26+
* @throws IllegalArgumentException If the provided JSON object is null or empty.
27+
*/
28+
TimezoneConvert(JSONObject json) {
29+
if (Objects.isNull(json)) {
30+
throw new IllegalArgumentException("'json' must not be null");
31+
}
32+
33+
if (json.isEmpty()) {
34+
throw new IllegalArgumentException("'json' must not be empty");
35+
}
36+
37+
this.originalTime = json.optString("original_time");
38+
this.convertedTime = json.optString("converted_time");
39+
this.diffHour = json.getBigDecimal("diff_hour");
40+
this.diffMin = json.getBigDecimal("diff_min");
41+
42+
this.json = json;
43+
}
44+
45+
/**
46+
* Returns the DateTime for which conversion is performed, if the time is not provided,
47+
* the time of request will return.
48+
*
49+
* @return Queried time or request time as a string.
50+
*/
51+
public String getOriginalTime() {
52+
return originalTime;
53+
}
54+
55+
/**
56+
* Returns the dateTime after conversion to the target timezone/location.
57+
*
58+
* @return The converted dateTime as a string.
59+
*/
60+
public String getConvertedTime() {
61+
return convertedTime;
62+
}
63+
64+
/**
65+
* Gets the difference in hours between the original and converted timezones.
66+
*
67+
* @return The difference in hours as a {@code BigDecimal}.
68+
*/
69+
public BigDecimal getDiffHour() {
70+
return diffHour;
71+
}
72+
73+
/**
74+
* Gets the difference in minutes between the original and converted timezones.
75+
*
76+
* @return The difference in minutes as a {@code BigDecimal}.
77+
*/
78+
public BigDecimal getDiffMin() {
79+
return diffMin;
80+
}
81+
82+
/**
83+
* Returns a string representation of the TimezoneConvert object, formatted JSON.
84+
*
85+
* @return A string containing the JSON representation of the timezone conversion result.
86+
*/
87+
@Override
88+
public String toString() {
89+
return json.toString(2);
90+
}
91+
}

0 commit comments

Comments
 (0)