11package com .genexus .db .cosmosdb ;
22
33import com .genexus .CommonUtil ;
4- import com .genexus .ModelContext ;
54import com .genexus .db .service .IOServiceContext ;
65import com .genexus .db .service .ServiceResultSet ;
76
87import com .azure .cosmos .CosmosException ;
98import java .io .InputStream ;
109import java .math .BigDecimal ;
10+ import java .sql .Date ;
1111import java .sql .SQLException ;
1212import java .sql .Time ;
1313import java .sql .Timestamp ;
14- import java .time .ZoneOffset ;
15- import java .time .Instant ;
16- import java .time .LocalDate ;
17- import java .time .LocalDateTime ;
14+ import java .text .DateFormat ;
15+ import java .text .SimpleDateFormat ;
16+ import java .time .*;
1817import java .time .format .DateTimeFormatter ;
1918import java .time .format .DateTimeFormatterBuilder ;
2019import java .time .format .DateTimeParseException ;
21- import java .time .temporal .TemporalAccessor ;
22- import java .util .TimeZone ;
2320import static java .lang .Boolean .valueOf ;
2421
2522public class CosmosDBResultSet extends ServiceResultSet <Object >
@@ -50,7 +47,8 @@ public boolean next()
5047 }
5148
5249 private static final IOServiceContext SERVICE_CONTEXT = null ;
53-
50+ private static final Timestamp TIMESTAMP_NULL_VALUE = java .sql .Timestamp .from (CommonUtil .nullDate ().toInstant ());
51+ private static final java .util .Date DATE_NULL_VALUE = Date .from (CommonUtil .nullDate ().toInstant ());
5452 private boolean lastWasNull ;
5553 @ Override
5654 public boolean wasNull ()
@@ -180,47 +178,23 @@ public BigDecimal getBigDecimal(int columnIndex)
180178 return BigDecimal .ZERO ;
181179 }
182180
183- private Instant getInstant ( int columnIndex )
184- {
185- String value = getString (columnIndex );
186- if ( value == null || value . trim ().isEmpty ( ))
181+ @ Override
182+ public java . sql . Date getDate ( int columnIndex ) throws SQLException {
183+ Timestamp ts = getTimestamp (columnIndex );
184+ if (! ts . toString ().equals ( TIMESTAMP_NULL_VALUE . toString () ))
187185 {
188- lastWasNull = true ;
189- return CommonUtil .nullDate ().toInstant ();
186+ return java .sql .Date .valueOf (ts .toInstant ().atOffset (ZoneOffset .UTC ).toLocalDate ());
190187 }
191-
192- TemporalAccessor accessor = null ;
193-
194- try
195- {
196- accessor = ISO_DATE_TIME_OR_DATE .parseBest (value , LocalDateTime ::from , LocalDate ::from );
197- }catch (DateTimeParseException dtpe )
198- {
199- for (DateTimeFormatter dateTimeFormatter :DATE_TIME_FORMATTERS )
200- {
201- try
202- {
203- accessor = dateTimeFormatter .parseBest (value , LocalDateTime ::from , LocalDate ::from );
204- break ;
205- }catch (Exception ignored ){ }
188+ String strDate = getString (columnIndex );
189+ for (String dateFormatter :DATE_FORMATTERS ) {
190+ try {
191+ DateFormat dateFormat = new SimpleDateFormat (dateFormatter );
192+ java .util .Date date = dateFormat .parse (strDate );
193+ return new java .sql .Date (date .getTime ());
194+ } catch (Exception ignored ) {
206195 }
207- if (accessor == null )
208- {
209- return CommonUtil .resetTime (CommonUtil .nullDate ()).toInstant ();
210- }
211- }
212- if (accessor instanceof LocalDateTime )
213- {
214- ModelContext ctx = ModelContext .getModelContext ();
215- TimeZone tz = ctx != null ? ctx .getClientTimeZone () : TimeZone .getDefault ();
216- return ((LocalDateTime ) accessor ).atZone (tz .toZoneId ()).toInstant ();
217196 }
218- else return LocalDate .from (accessor ).atStartOfDay ().toInstant (ZoneOffset .UTC );
219- }
220- @ Override
221- public java .sql .Date getDate (int columnIndex )
222- {
223- return java .sql .Date .valueOf (getTimestamp (columnIndex ).toInstant ().atOffset (ZoneOffset .UTC ).toLocalDate ());
197+ return new java .sql .Date (DATE_NULL_VALUE .getTime ());
224198 }
225199
226200 @ Override
@@ -232,7 +206,33 @@ public Time getTime(int columnIndex)
232206 @ Override
233207 public Timestamp getTimestamp (int columnIndex )
234208 {
235- return java .sql .Timestamp .from (getInstant (columnIndex ));
209+ String datetimeString = getString (columnIndex );
210+ if (datetimeString == null || datetimeString .trim ().isEmpty ())
211+ {
212+ lastWasNull = true ;
213+ return TIMESTAMP_NULL_VALUE ;
214+ }
215+ LocalDateTime localDateTime = null ;
216+ try {
217+ localDateTime = LocalDateTime .parse (datetimeString , ISO_DATE_TIME_OR_DATE );
218+ ZonedDateTime zonedDateTime = ZonedDateTime .of (localDateTime , ZoneOffset .UTC );
219+ return Timestamp .valueOf (zonedDateTime .toLocalDateTime ());
220+ }
221+ catch (DateTimeParseException dateTimeParseException )
222+ {
223+ for (DateTimeFormatter dateTimeFormatter :DATE_TIME_FORMATTERS ) {
224+ try {
225+ localDateTime = LocalDateTime .parse (datetimeString , dateTimeFormatter );
226+ ZonedDateTime zonedDateTime = ZonedDateTime .of (localDateTime , ZoneOffset .UTC );
227+ return Timestamp .valueOf (zonedDateTime .toLocalDateTime ());
228+ }
229+ catch (Exception ignored )
230+ {}
231+ }
232+ if (localDateTime == null )
233+ {return TIMESTAMP_NULL_VALUE ;}
234+ }
235+ return TIMESTAMP_NULL_VALUE ;
236236 }
237237
238238 @ Override
@@ -280,4 +280,27 @@ public <T> T getObject(String columnLabel, Class<T> type)
280280 DateTimeFormatter .ofPattern ("yyyy-M-d[ H:mm:ss.S]" ),
281281 DateTimeFormatter .ofPattern ("yyyy-M-d[ H:mm:ss.S a]" )
282282 };
283+ private static final String [] DATE_FORMATTERS = new String []
284+ {
285+ "yyyy-MM-dd" ,
286+ "yyyy-dd-MM" ,
287+ "dd-MM-yyyy" ,
288+ "MM/dd/yyyy" ,
289+ "dd/MM/yyyy" ,
290+ "yyyy/MM/dd" ,
291+ "EEE, dd MMM yyyy" ,
292+ "EEEE, dd MMMM yyyy" ,
293+ "Month D, Yr" ,
294+ "Yr, Month D" ,
295+ "D Month, Yr" ,
296+ "M/D/YY" ,
297+ "D/M/YY" ,
298+ "YY/M/D" ,
299+ "Mon-DD-YYYY" ,
300+ "DD-Mon-YYYY" ,
301+ "YYYYY-Mon-DD" ,
302+ "Mon DD, YYYY" ,
303+ "DD Mon, YYYY" ,
304+ "YYYY, Mon DD"
305+ };
283306}
0 commit comments