Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove joda time dependency #711

Merged
merged 9 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.12.2</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
Expand Down
50 changes: 33 additions & 17 deletions java/src/main/java/com/genexus/specific/java/GXutil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
Expand All @@ -23,39 +25,53 @@
import com.genexus.util.CacheAPI;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.joda.time.DateTimeZone;

public class GXutil implements IExtensionGXutil {
private static Logger logger = LogManager.getLogger(GXutil.class);

private static DateTimeZone Java2JodaTimeZone(TimeZone tz) {
DateTimeZone jodaTZ = DateTimeZone.getDefault();
private ZonedDateTime getZonedDateTime(Date value, TimeZone tz){
ZonedDateTime zdt;
try {
jodaTZ = DateTimeZone.forID(tz.getID());
} catch (IllegalArgumentException _) {
try {
jodaTZ = DateTimeZone.forTimeZone(tz);
} catch (IllegalArgumentException e) {
logger.error(String.format("Failed to find TimeZone: %s. Using default Timezone", tz.getID()), e);
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(value);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int nanoOfSecond = calendar.get(Calendar.MILLISECOND) * 1000000;

zdt = ZonedDateTime.of(LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond), tz.toZoneId());
}
return jodaTZ;
catch(Exception e) {
zdt = ZonedDateTime.ofInstant(value.toInstant(), ZoneId.systemDefault());
logger.error(String.format("Failed to find TimeZone: %s. Using default Timezone", tz.getID()), e);
}
return zdt;
}

@Override
public Date DateTimeToUTC(Date value, TimeZone tz) {
org.joda.time.DateTime ret = new org.joda.time.DateTime(value.getTime());
ret = new org.joda.time.DateTime(Java2JodaTimeZone(tz).convertLocalToUTC(ret.getMillis(), false));
return ret.toDate();
if (tz.getID() == "GMT")
return value;

ZonedDateTime zdt = getZonedDateTime(value, tz);
Timestamp t = Timestamp.valueOf(zdt.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime());
return new Date(t.getTime());
}

@Override
public Date DateTimeFromUTC(Date value, TimeZone tz) {
if (tz.getID() == "GMT")
return value;

if (com.genexus.CommonUtil.emptyDate(value))
return value;
org.joda.time.DateTime ret = new org.joda.time.DateTime(value.getTime());
ret = new org.joda.time.DateTime(Java2JodaTimeZone(tz).convertUTCToLocal(ret.getMillis()));
return ret.toDate();

ZonedDateTime zdtUTC = getZonedDateTime(value, TimeZone.getTimeZone("UTC"));
Timestamp t = Timestamp.valueOf(zdtUTC.withZoneSameInstant(ZoneId.of(tz.getID())).toLocalDateTime());
return new Date(t.getTime());
}

@Override
Expand Down
68 changes: 43 additions & 25 deletions java/src/test/java/com/genexus/util/TestDateMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,34 @@ public void testCtotex() {
}

@Test
public void testValidTimezone() {
public void testDateTimeToUTC() {
Connect.init();

TimeZone timezone = TimeZone.getTimeZone("America/Montevideo");

String dateTime = "2023-03-22 15:00:00"; // input DateTime
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long expectedDiff = 10800000;
ConvertDateTime(dateTime, timezone, expectedDiff, true);

dateTime = "2011-02-20 1:00:00"; // input DateTime during summer time
expectedDiff = 7200000;
ConvertDateTime(dateTime, timezone, expectedDiff, true);
}

@Test
public void DateTimeFromUTC() {
Connect.init();

TimeZone timezone = TimeZone.getTimeZone("America/Montevideo");

try {
Date mvdDate = dateFormat.parse(dateTime); // convert String to Date
Date utcDate = CommonUtil.DateTimeToUTC(mvdDate, timezone);
String dateTime = "2023-02-22 18:00:00"; // input DateTime
long expectedDiff = -10800000;
ConvertDateTime(dateTime, timezone, expectedDiff, false);

long diff = utcDate.getTime() - mvdDate.getTime();
long expectedDiff = 10800000;
Assert.assertEquals("Timezone offset invalid", expectedDiff, diff);
} catch (Exception e) {
Assert.fail();
}
}
dateTime = "2011-02-20 3:00:00"; // input DateTime during summer time
expectedDiff = -7200000;
ConvertDateTime(dateTime, timezone, expectedDiff, false);
}

/**
* DateTimeToUTC must not fail if the Timezone does not exists.
Expand All @@ -110,20 +119,29 @@ public void testValidTimezone() {
public void testInvalidTimezone() {
Connect.init();

String dateTime = "2023-03-22 15:00:00"; // input DateTime
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone timezone = TimeZone.getTimeZone("America/DoesnotExists");

TimeZone timezone = TimeZone.getTimeZone("America/DoesnotExists");
String dateTime = "2023-03-22 15:00:00"; // input DateTime
long expectedDiff = 0;
ConvertDateTime(dateTime, timezone, expectedDiff, true);
ConvertDateTime(dateTime, timezone, expectedDiff, false);
}

try {
Date mvdDate = dateFormat.parse(dateTime); // convert String to Date
Date utcDate = CommonUtil.DateTimeToUTC(mvdDate, timezone);
private void ConvertDateTime(String dateTime, TimeZone timezone, long expectedDiff, boolean toUTC) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date mvdDate = dateFormat.parse(dateTime); // convert String to Date
Date dateConverted;
if (toUTC)
dateConverted = CommonUtil.DateTimeToUTC(mvdDate, timezone);
else
dateConverted = CommonUtil.DateTimeFromUTC(mvdDate, timezone);

long diff = dateConverted.getTime() - mvdDate.getTime();
Assert.assertEquals("Timezone offset invalid", expectedDiff, diff);
} catch (Exception e) {
Assert.fail();
}
}

long diff = utcDate.getTime() - mvdDate.getTime();
long expectedDiff = 0;
Assert.assertEquals("Timezone offset invalid", expectedDiff, diff);
} catch (Exception e) {
Assert.fail();
}
}
}