Skip to content

Commit

Permalink
fixed the datetime NYIs and bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenvinju committed Dec 18, 2024
1 parent 8c04832 commit 91fcd88
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/org/rascalmpl/library/Prelude.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public IValue incrementDays(IDateTime dt, IInteger n)
return incrementDate(dt, Calendar.DAY_OF_MONTH, "days", n);
}

private String getTZString(int hourOffset, int minuteOffset) {
public static String getTZString(int hourOffset, int minuteOffset) {
String tzString = "GMT" +
((hourOffset < 0 || (0 == hourOffset && minuteOffset < 0)) ? "-" : "+") +
String.format("%02d",hourOffset >= 0 ? hourOffset : hourOffset * -1) +
Expand Down Expand Up @@ -629,7 +629,7 @@ private Calendar getCalendarForTime(IDateTime inputTime) {
}
}

private Calendar getCalendarForDateTime(IDateTime inputDateTime) {
public static Calendar getCalendarForDateTime(IDateTime inputDateTime) {
if (inputDateTime.isDateTime()) {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(getTZString(inputDateTime.getTimezoneOffsetHours(),inputDateTime.getTimezoneOffsetMinutes())),Locale.getDefault());
cal.setLenient(false);
Expand Down
8 changes: 4 additions & 4 deletions src/org/rascalmpl/library/lang/json/IO.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ First the expected type is used as a literal lookup, and then each value is test
java &T readJSON(
type[&T] expected,
loc src,
str dateTimeFormat = "yyyy-MM-dd\'T\'HH:mm:ssZZZZZ",
str dateTimeFormat = "yyyy-MM-dd\'T\'HH:mm:ssZ",
bool lenient=false,
bool trackOrigins=false,
JSONParser[value] parser = (type[value] _, str _) { throw ""; },
Expand Down Expand Up @@ -74,7 +74,7 @@ In general the translation behaves as the same as for ((readJSON)).}
java &T parseJSON(
type[&T] expected,
str src,
str dateTimeFormat = "yyyy-MM-dd\'T\'HH:mm:ssZZZZZ",
str dateTimeFormat = "yyyy-MM-dd\'T\'HH:mm:ssZ",
bool lenient=false,
bool trackOrigins=false,
JSONParser[value] parser = (type[value] _, str _) { throw ""; },
Expand Down Expand Up @@ -106,7 +106,7 @@ For `real` numbers that are larger than JVM's double you get "negative infinity"
}
java void writeJSON(loc target, value val,
bool unpackedLocations=false,
str dateTimeFormat="yyyy-MM-dd\'T\'HH:mm:ssZZZZZ",
str dateTimeFormat="yyyy-MM-dd\'T\'HH:mm:ssZ",
bool dateTimeAsInt=false,
int indent=0,
bool dropOrigins=true,
Expand All @@ -121,7 +121,7 @@ java void writeJSON(loc target, value val,
@description{
This function uses `writeJSON` and stores the result in a string.
}
java str asJSON(value val, bool unpackedLocations=false, str dateTimeFormat="yyyy-MM-dd\'T\'HH:mm:ssZZZZZ", bool dateTimeAsInt=false, int indent = 0, bool dropOrigins=true, JSONFormatter[value] formatter = str (value _) { fail; }, bool explicitConstructorNames=false, bool explicitDataTypes=false);
java str asJSON(value val, bool unpackedLocations=false, str dateTimeFormat="yyyy-MM-dd\'T\'HH:mm:ssZ", bool dateTimeAsInt=false, int indent = 0, bool dropOrigins=true, JSONFormatter[value] formatter = str (value _) { fail; }, bool explicitConstructorNames=false, bool explicitDataTypes=false);

@synopsis{((writeJSON)) and ((asJSON)) uses `Formatter` functions to flatten structured data to strings, on-demand}
@description{
Expand Down
15 changes: 12 additions & 3 deletions src/org/rascalmpl/library/lang/json/internal/JsonValueWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
package org.rascalmpl.library.lang.json.internal;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.Map.Entry;

import org.rascalmpl.exceptions.RuntimeExceptionFactory;
import org.rascalmpl.exceptions.Throw;
import org.rascalmpl.library.Prelude;
import org.rascalmpl.values.functions.IFunction;
import org.rascalmpl.values.maybe.UtilMaybe;

import com.google.gson.stream.JsonWriter;
import com.ibm.icu.text.SimpleDateFormat;

import io.usethesource.vallang.IBool;
import io.usethesource.vallang.IConstructor;
Expand Down Expand Up @@ -375,11 +377,18 @@ public Void visitExternal(IExternalValue externalValue) throws IOException {
public Void visitDateTime(IDateTime o) throws IOException {
if (datesAsInts) {
out.value(o.getInstant());
return null;
}
else {
throw new IOException("Dates as strings not yet implemented: " + format.get().toPattern());
try {
com.ibm.icu.text.SimpleDateFormat sd = format.get();
com.ibm.icu.util.Calendar cal = Prelude.getCalendarForDateTime(o);
sd.setCalendar(cal);
out.value(sd.format(cal.getTime()));
} catch (IllegalArgumentException iae) {
throw RuntimeExceptionFactory.dateTimePrintingError("Cannot print datetime " + o + " using format string: " + format.get());
}
}
return null;
}
});
}
Expand Down

0 comments on commit 91fcd88

Please sign in to comment.