muScript: use date/time from respackopts
This commit is contained in:
parent
51db36f53f
commit
cf49056104
|
@ -3,18 +3,17 @@ package io.gitlab.jfronny.muscript;
|
|||
import io.gitlab.jfronny.muscript.data.Scope;
|
||||
import io.gitlab.jfronny.muscript.data.dynamic.*;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalTime;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static io.gitlab.jfronny.muscript.data.dynamic.DFinal.of;
|
||||
|
||||
public class StandardLib {
|
||||
private static final Random rnd = new Random();
|
||||
|
||||
public static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
|
||||
public static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd. MM. yyyy");
|
||||
|
||||
public static Scope createScope() {
|
||||
return addTo(new Scope());
|
||||
}
|
||||
|
@ -22,8 +21,29 @@ public class StandardLib {
|
|||
public static Scope addTo(Scope scope) {
|
||||
return scope
|
||||
.set("PI", Math.PI)
|
||||
.set("time", timeFormat.format(new Date()))
|
||||
.set("date", dateFormat.format(new Date()))
|
||||
.set("E", Math.E)
|
||||
.set("date", new DCallableObject(Map.of(
|
||||
"today", new DDate(LocalDate::now)
|
||||
), DFinal.of(args -> {
|
||||
// Constructor
|
||||
if (args.size() == 1) return new DDate(() -> LocalDate.ofEpochDay(args.get(0).asNumber().getValue().longValue()));
|
||||
if (args.size() != 3) throw new IllegalArgumentException("Expected 3 arguments for full date constructor");
|
||||
int a0 = args.get(0).asNumber().getValue().intValue();
|
||||
int a1 = args.get(1).asNumber().getValue().intValue();
|
||||
int a2 = args.get(2).asNumber().getValue().intValue();
|
||||
return new DDate(() -> LocalDate.of(a0, a1, a2));
|
||||
})))
|
||||
.set("time", new DCallableObject(Map.of(
|
||||
"now", new DTime(LocalTime::now)
|
||||
), DFinal.of(args -> {
|
||||
// Constructor
|
||||
if (args.size() == 1) return new DTime(() -> LocalTime.ofSecondOfDay(args.get(0).asNumber().getValue().intValue()));
|
||||
if (args.size() != 3) throw new IllegalArgumentException("Expected 3 arguments for full time constructor");
|
||||
int a0 = args.get(0).asNumber().getValue().intValue();
|
||||
int a1 = args.get(1).asNumber().getValue().intValue();
|
||||
int a2 = args.get(2).asNumber().getValue().intValue();
|
||||
return new DTime(() -> LocalTime.of(a0, a1, a2));
|
||||
})))
|
||||
|
||||
.set("round", StandardLib::round)
|
||||
.set("floor", StandardLib::floor)
|
||||
|
@ -47,6 +67,58 @@ public class StandardLib {
|
|||
.set("forEach", StandardLib::forEach);
|
||||
}
|
||||
|
||||
private record DDate(Supplier<LocalDate> date) implements DObject {
|
||||
@Override
|
||||
public Map<String, Dynamic<?>> getValue() {
|
||||
return Map.of(
|
||||
"year", DFinal.of(date.get().getYear()),
|
||||
"month", DFinal.of(date.get().getMonthValue()),
|
||||
"day", DFinal.of(date.get().getDayOfMonth())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DString asString() {
|
||||
return DFinal.of(toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public DNumber asNumber() {
|
||||
return DFinal.of(date.get().toEpochDay());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return date.get().toString();
|
||||
}
|
||||
}
|
||||
|
||||
private record DTime(Supplier<LocalTime> time) implements DObject {
|
||||
@Override
|
||||
public Map<String, Dynamic<?>> getValue() {
|
||||
return Map.of(
|
||||
"hour", DFinal.of(time.get().getHour()),
|
||||
"minute", DFinal.of(time.get().getMinute()),
|
||||
"second", DFinal.of(time.get().getSecond())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DString asString() {
|
||||
return DFinal.of(toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public DNumber asNumber() {
|
||||
return DFinal.of(time.get().toSecondOfDay());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return time.get().toString();
|
||||
}
|
||||
}
|
||||
|
||||
// Numbers
|
||||
public static DNumber round(DList args) {
|
||||
if (args.size() == 1) {
|
||||
|
@ -113,7 +185,8 @@ public class StandardLib {
|
|||
|
||||
public static DNumber len(DList args) {
|
||||
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for len: expected 1 but got " + args.size());
|
||||
return of(args.get(0).asList().size());
|
||||
Dynamic<?> d = args.get(0);
|
||||
return of(d instanceof DString ds ? ds.getValue().length() : d.asList().size());
|
||||
}
|
||||
|
||||
public static DBool isEmpty(DList args) {
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
package io.gitlab.jfronny.muscript.data.dynamic;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public record DCallableObject(Map<String, Dynamic<?>> value, DCallable callable) implements DObject {
|
||||
@Override
|
||||
public Map<String, Dynamic<?>> getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DCallable asCallable() {
|
||||
return callable;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue