Skip to content

Timezone Support #7887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprAllTimezones.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

import java.time.ZoneId;

public class ExprAllTimezones extends SimpleExpression<String> {

static {
Skript.registerExpression(ExprAllTimezones.class, String.class, ExpressionType.SIMPLE, "all time[ ]zones");
}

@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
return true;
}

@Override
@Nullable
protected String[] get(Event event) {
return ZoneId.getAvailableZoneIds().toArray(new String[0]);
}

@Override
public boolean isSingle() {
return false;
}
@Override
public Class<? extends String> getReturnType() {
return String.class;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "all timezones";
}

}
47 changes: 41 additions & 6 deletions src/main/java/ch/njol/skript/expressions/ExprNow.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,56 @@
import ch.njol.skript.util.Date;
import ch.njol.util.Kleenean;

import java.time.DateTimeException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

@Name("Now")
@Description("The current <a href='classes.html#date'>system time</a> of the server. Use <a href='#ExprTime'>time</a> to get the <a href='classes.html#time'>Minecraft time</a> of a world.")
@Examples({"broadcast \"Current server time: %now%\""})
@Since("1.4")
@Since("1.4, INSERT VERSION (timezones)")
public class ExprNow extends SimpleExpression<Date> {

static {
Skript.registerExpression(ExprNow.class, Date.class, ExpressionType.SIMPLE, "now");
Skript.registerExpression(ExprNow.class, Date.class, ExpressionType.SIMPLE, "now [timezone:in time[ ]zone %-string%]");
}

private boolean usingTimezone;
private Expression<String> timezone;

@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
usingTimezone = parseResult.hasTag("timezone");
timezone = (Expression<String>) exprs[0];
return true;
}

@Override
protected Date[] get(final Event e) {
return new Date[] {new Date()};
protected Date[] get(Event event) {
if (usingTimezone) {
String timezone = this.timezone.getSingle(event);
if (timezone == null) {
return new Date[0];
}

ZoneId targetZoneId;
try {
targetZoneId = ZoneId.of(timezone);
} catch (DateTimeException e) { // invalid zone format
return new Date[0];
}

ZoneId localZoneId = ZoneId.systemDefault();
Instant shiftedNow = ZonedDateTime.now(targetZoneId)
.toLocalDateTime()
.atZone(localZoneId)
.toInstant();
java.util.Date javaDate = java.util.Date.from(shiftedNow);
Date date = Date.fromJavaDate(javaDate);
return new Date[]{ date };
}
return new Date[]{ new Date() };
}

@Override
Expand All @@ -46,7 +78,10 @@ public Class<? extends Date> getReturnType() {
}

@Override
public String toString(final @Nullable Event e, final boolean debug) {
public String toString(@Nullable Event e, boolean debug) {
if (usingTimezone) {
return "now in timezone " + timezone.toString(e, debug);
}
return "now";
}

Expand Down