-
-
Notifications
You must be signed in to change notification settings - Fork 391
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
base: dev/feature
Are you sure you want to change the base?
Timezone Support #7887
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package ch.njol.skript.expressions; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.doc.Description; | ||
import ch.njol.skript.doc.Example; | ||
import ch.njol.skript.doc.Name; | ||
import ch.njol.skript.doc.Since; | ||
import ch.njol.skript.lang.Expression; | ||
import ch.njol.skript.lang.ExpressionType; | ||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
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; | ||
|
||
@Name("All Timezones") | ||
@Description("Returns a list of all timezones that can be used in the <a href='#ExprNow'>now</a> expression.") | ||
@Since("INSERT VERSION") | ||
@Example("set {_timezones::*} to all timezones") | ||
public class ExprAllTimezones extends SimpleExpression<String> { | ||
|
||
static { | ||
Skript.registerExpression(ExprAllTimezones.class, String.class, ExpressionType.SIMPLE, "all time[ ]zones"); | ||
erenkarakal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
return true; | ||
} | ||
|
||
@Override | ||
protected String @Nullable [] 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"; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,78 @@ | ||
package ch.njol.skript.expressions; | ||
|
||
import ch.njol.skript.doc.*; | ||
import org.bukkit.event.Event; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.doc.Description; | ||
import ch.njol.skript.doc.Examples; | ||
import ch.njol.skript.doc.Name; | ||
import ch.njol.skript.doc.Since; | ||
import ch.njol.skript.lang.Expression; | ||
import ch.njol.skript.lang.ExpressionType; | ||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
import ch.njol.skript.lang.util.SimpleExpression; | ||
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") | ||
@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.", | ||
"Optionally specify a timezone to get the current date at a timezone. The returned value might not be equal to" | ||
+ "'now' without timezones. If a timezone is invalid no value will be returned.", | ||
"Use <a href='#ExprAllTimezones'>all timezones</a> to get a list of valid timezones." | ||
}) | ||
@Example("broadcast \"Current server time: %now%\"") | ||
@Example(""" | ||
set {_date} to now in timezone "Europe/Istanbul" | ||
set {_clock} to {_date} formatted as "kk:mm" | ||
send "It is currently %{_clock}% in Istanbul!" to player | ||
""") | ||
@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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably throw a runtime error here |
||
} | ||
|
||
ZoneId targetZoneId; | ||
try { | ||
targetZoneId = ZoneId.of(timezone); | ||
} catch (DateTimeException e) { // invalid zone format | ||
return new Date[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. runtime error There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know about throwing an error here, there is no way to check if the timezone is valid There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could add a condition to check if the timezone is a valid one There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is a lookup on 603 time zones worth it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the user wants to yes |
||
} | ||
|
||
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 | ||
|
@@ -46,8 +86,11 @@ public Class<? extends Date> getReturnType() { | |
} | ||
|
||
@Override | ||
public String toString(final @Nullable Event e, final boolean debug) { | ||
public String toString(@Nullable Event event, boolean debug) { | ||
if (usingTimezone) { | ||
return "now in timezone " + timezone.toString(event, debug); | ||
} | ||
return "now"; | ||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.