Skip to content

Commit

Permalink
Remove joda time dependency
Browse files Browse the repository at this point in the history
In order to convert dates to UTC dates and UTC dates to dates, remove joda time dependency and use JDK 8 ZonedDateTime data type.
Issue:102003
  • Loading branch information
iroqueta committed Apr 20, 2023
1 parent 34eb152 commit 97c4dcb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
20 changes: 11 additions & 9 deletions java/src/main/java/com/genexus/specific/java/GXutil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.time.*;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.time.ZonedDateTime;
import java.time.ZoneId;

import com.genexus.CommonUtil;
import com.genexus.ModelContext;
Expand Down Expand Up @@ -46,18 +43,23 @@ private ZonedDateTime getZonedDateTime(Date value, TimeZone tz){

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

ZonedDateTime zdt = getZonedDateTime(value, tz);
return Date.from(zdt.plusSeconds(-1 * zdt.getOffset().getTotalSeconds()).toInstant());
return Timestamp.valueOf(zdt.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime());
}

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

if (com.genexus.CommonUtil.emptyDate(value))
return value;

ZonedDateTime zdtUTC = ZonedDateTime.ofInstant(value.toInstant(), ZoneId.of("UTC"));
ZonedDateTime zdt = getZonedDateTime(value, tz);
return Date.from(zdtUTC.plusSeconds(zdt.getOffset().getTotalSeconds()).toInstant());
ZonedDateTime zdtUTC = ZonedDateTime.of(value.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(), ZoneId.of("UTC"));
return Timestamp.valueOf(zdtUTC.withZoneSameInstant(ZoneId.of(tz.getID())).toLocalDateTime());
}

@Override
Expand Down
24 changes: 14 additions & 10 deletions java/src/test/java/com/genexus/util/TestDateMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,33 @@ public void testCtotex() {
public void testDateTimeToUTC() {
Connect.init();

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

String dateTime = "2023-02-22 15:00:00"; // input DateTime
long expectedDiff = 18000000;
String dateTime = "2023-03-22 15:00:00"; // input DateTime
long expectedDiff = 10800000;
ConvertDateTime(dateTime, timezone, expectedDiff, true);

dateTime = "2012-10-07 1:00:00";
expectedDiff = 7200000;
ConvertDateTime(dateTime, timezone, expectedDiff, true);

dateTime = "2023-07-22 15:00:00"; // input DateTime during summer time
expectedDiff = 14400000;
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/New_York");
TimeZone timezone = TimeZone.getTimeZone("America/Montevideo");

String dateTime = "2023-02-22 20:00:00"; // input DateTime
long expectedDiff = -18000000;
String dateTime = "2023-02-22 18:00:00"; // input DateTime
long expectedDiff = -10800000;
ConvertDateTime(dateTime, timezone, expectedDiff, false);

dateTime = "2023-07-22 19:00:00"; // input DateTime during summer time
expectedDiff = -14400000;
dateTime = "2011-02-20 3:00:00"; // input DateTime during summer time
expectedDiff = -7200000;
ConvertDateTime(dateTime, timezone, expectedDiff, false);
}

Expand Down

0 comments on commit 97c4dcb

Please sign in to comment.