Make input more lenient. Allow specifying TZ other than UTC.

Also remove 'none' and cleanup LocationFlag.
This commit is contained in:
wizjany 2019-03-30 20:07:35 -04:00
parent 9c5eee90da
commit 874e429779
2 changed files with 26 additions and 9 deletions

View File

@ -47,9 +47,9 @@ public Location parseInput(FlagContext context) throws InvalidFlagFormat {
Location loc = null;
if ("here".equalsIgnoreCase(input)) {
loc = player.getLocation();
} else if ("none".equalsIgnoreCase(input)) {
return null;
Location playerLoc = player.getLocation();
loc = new LazyLocation(((World) playerLoc.getExtent()).getName(),
loc.toVector(), loc.getYaw(), loc.getPitch());
} else {
String[] split = input.split(",");
if (split.length >= 3) {

View File

@ -21,15 +21,24 @@
import javax.annotation.Nullable;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
/**
* Stores a timestamp.
*/
public class TimestampFlag extends Flag<Instant> {
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneOffset.UTC);
private static final DateTimeFormatter SERIALIZER = DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneOffset.UTC);
private static final DateTimeFormatter PARSER = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart().appendOffsetId()
.toFormatter();
public TimestampFlag(String name, @Nullable RegionGroup defaultGroup) {
super(name, defaultGroup);
}
@ -45,7 +54,15 @@ public Instant parseInput(FlagContext context) throws InvalidFlagFormat {
return Instant.now();
} else {
try {
return Instant.from(FORMATTER.parse(input));
TemporalAccessor parsed = PARSER.parseBest(input, ZonedDateTime::from, LocalDateTime::from);
// convert whatever input into UTC for storage
if (parsed instanceof LocalDateTime) {
return ((LocalDateTime) parsed).atZone(ZoneOffset.UTC).toInstant();
} else if (parsed instanceof ZonedDateTime) {
return ((ZonedDateTime) parsed).toInstant();
} else {
throw new InvalidFlagFormat("Unrecognized input.");
}
} catch (DateTimeParseException ignored) {
throw new InvalidFlagFormat("Expected 'now' or ISO 8601 formatted input.");
}
@ -56,7 +73,7 @@ public Instant parseInput(FlagContext context) throws InvalidFlagFormat {
public Instant unmarshal(@Nullable Object o) {
if (o instanceof String) {
try {
return Instant.from(FORMATTER.parse((String) o));
return Instant.from(SERIALIZER.parse((String) o));
} catch(DateTimeParseException ignored) {
return null;
}
@ -66,6 +83,6 @@ public Instant unmarshal(@Nullable Object o) {
@Override
public Object marshal(Instant o) {
return FORMATTER.format(o);
return SERIALIZER.format(o);
}
}