-
-
Notifications
You must be signed in to change notification settings - Fork 399
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
erenkarakal
wants to merge
25
commits into
SkriptLang:dev/feature
Choose a base branch
from
erenkarakal:feature/timezone-syntaxes
base: dev/feature
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Timezone Support #7887
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
bcd9fdc
timezone support
erenkarakal f027b75
fix shifting
erenkarakal 6d0a0df
requested changes
erenkarakal ce306e7
revert ExprNow
erenkarakal 2b911a8
add ExprDateInTimezone
erenkarakal d62d417
add ExprDateInTimezone
erenkarakal af2dd89
tests
erenkarakal 3724465
docs
erenkarakal 695252e
update test
erenkarakal 708fa49
force environment timezone
erenkarakal ce4a3cf
update test
erenkarakal 5a59fc7
Merge branch 'dev/feature' into feature/timezone-syntaxes
erenkarakal d822c91
requested changes
erenkarakal 05e9edf
update syntax
erenkarakal 1a423a1
getarray()
erenkarakal de725b9
update syntaxes
erenkarakal ab77896
update tests
erenkarakal ae175db
grammar
erenkarakal 4f848e1
update test
erenkarakal e3eee43
update description
erenkarakal 393906b
Merge branch 'dev/feature' into feature/timezone-syntaxes
erenkarakal 0145212
Merge branch 'dev/feature' into feature/timezone-syntaxes
erenkarakal a4f2868
simpleliteral
erenkarakal 06e9e0c
remove isSingle
erenkarakal 63ae504
Merge branch 'dev/feature' into feature/timezone-syntaxes
erenkarakal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/main/java/ch/njol/skript/conditions/CondIsTimezoneValid.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package ch.njol.skript.conditions; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.doc.*; | ||
import ch.njol.skript.lang.Condition; | ||
import ch.njol.skript.lang.Expression; | ||
import ch.njol.skript.lang.SkriptParser; | ||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
import ch.njol.util.Kleenean; | ||
import org.bukkit.event.Event; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.time.DateTimeException; | ||
import java.time.ZoneId; | ||
|
||
@Name("Is Timezone Valid") | ||
@Description("Checks if a timezone is valid.") | ||
@Example(""" | ||
set {_timezone} to "America/New_York" | ||
if timezone {_timezone} is valid: | ||
set {_date} to now in timezone {_timezone} | ||
""") | ||
@Since("INSERT VERSION") | ||
public class CondIsTimezoneValid extends Condition { | ||
|
||
static { | ||
Skript.registerCondition(CondIsTimezoneValid.class, "time[ ]zone[s] %strings% (is|are) [negate:in]valid"); | ||
} | ||
|
||
private Expression<String> timezones; | ||
private boolean isNegated; | ||
|
||
@Override | ||
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
timezones = (Expression<String>) expressions[0]; | ||
isNegated = parseResult.hasTag("negate"); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean check(Event event) { | ||
for (String timezone : timezones.getAll(event)) { | ||
erenkarakal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (timezone == null) { | ||
return isNegated; | ||
} | ||
erenkarakal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
try { | ||
ZoneId.of(timezone); | ||
} catch (DateTimeException e) { | ||
return isNegated; | ||
} | ||
} | ||
|
||
return !isNegated; | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { | ||
return "timezone " + timezones.toString(event, debug) + " is " + (isNegated ? "in" : "") + "valid"; | ||
} | ||
|
||
} |
22 changes: 16 additions & 6 deletions
22
src/main/java/ch/njol/skript/expressions/ExprAllTimezones.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,37 +1,47 @@ | ||||||
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; | ||||||
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.") | ||||||
@Example("set {_timezones::*} to all timezones") | ||||||
@Since("INSERT VERSION") | ||||||
public class ExprAllTimezones extends SimpleExpression<String> { | ||||||
erenkarakal marked this conversation as resolved.
Show resolved
Hide resolved
erenkarakal marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
static { | ||||||
Skript.registerExpression(ExprAllTimezones.class, String.class, ExpressionType.SIMPLE, "all time[ ]zones"); | ||||||
Skript.registerExpression(ExprAllTimezones.class, String.class, ExpressionType.SIMPLE, "all [of [the]] time[ ]zones"); | ||||||
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.
Suggested change
|
||||||
} | ||||||
|
||||||
private static String[] timezones = ZoneId.getAvailableZoneIds().toArray(new String[0]); | ||||||
|
||||||
@Override | ||||||
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { | ||||||
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||||||
return true; | ||||||
} | ||||||
|
||||||
@Override | ||||||
@Nullable | ||||||
protected String[] get(Event event) { | ||||||
return ZoneId.getAvailableZoneIds().toArray(new String[0]); | ||||||
protected String @Nullable [] get(Event event) { | ||||||
return timezones; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public boolean isSingle() { | ||||||
return false; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public Class<? extends String> getReturnType() { | ||||||
return String.class; | ||||||
|
100 changes: 100 additions & 0 deletions
100
src/main/java/ch/njol/skript/expressions/ExprDateInTimezone.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package ch.njol.skript.expressions; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.config.Node; | ||
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; | ||
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 org.bukkit.event.Event; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer; | ||
|
||
import java.time.DateTimeException; | ||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
|
||
@Name("Date in Timezone") | ||
@Description({ | ||
"Returns a date in the specified timezone. Note that the result date might not be equal to the input date.", | ||
"Use <a href='#ExprAllTimezones'>all timezones</a> to get a list of valid timezones." | ||
}) | ||
@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("INSERT VERSION") | ||
public class ExprDateInTimezone extends SimpleExpression<Date> { | ||
|
||
static { | ||
Skript.registerExpression(ExprDateInTimezone.class, Date.class, ExpressionType.SIMPLE, "[the] [date] %date% in time[ ]zone %string%"); | ||
erenkarakal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private Expression<Date> date; | ||
private Expression<String> timezone; | ||
|
||
@Override | ||
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
date = (Expression<Date>) expressions[0]; | ||
timezone = (Expression<String>) expressions[1]; | ||
return true; | ||
} | ||
|
||
@Override | ||
protected Date @Nullable [] get(Event event) { | ||
String timezone = this.timezone.getSingle(event); | ||
Date date = this.date.getSingle(event); | ||
|
||
if (timezone == null) { | ||
error("Timezone is not set."); | ||
return new Date[0]; | ||
} | ||
|
||
if (date == null) { | ||
return new Date[0]; | ||
} | ||
|
||
ZoneId targetZoneId; | ||
try { | ||
targetZoneId = ZoneId.of(timezone); | ||
} catch (DateTimeException e) { // invalid zone format | ||
error("Invalid timezone."); | ||
return new Date[0]; | ||
} | ||
|
||
Instant instantDate = date.toInstant(); | ||
ZoneId localZoneId = ZoneId.systemDefault(); | ||
Instant shiftedNow = ZonedDateTime.ofInstant(instantDate, targetZoneId) | ||
.toLocalDateTime() | ||
.atZone(localZoneId) | ||
.toInstant(); | ||
java.util.Date javaDate = java.util.Date.from(shiftedNow); | ||
Date shiftedDate = Date.fromJavaDate(javaDate); | ||
return new Date[]{ shiftedDate }; | ||
} | ||
|
||
@Override | ||
public boolean isSingle() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Class<? extends Date> getReturnType() { | ||
return Date.class; | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { | ||
return "date " + date.toString(event, debug) + " in timezone " + timezone.toString(event, debug); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
test "timezone syntaxes": | ||
assert size of all timezones > 0 with "timezones aren't set" | ||
|
||
assert timezone "Europe/Istanbul" is valid with "single timezone should've been valid" | ||
assert timezones ("Europe/Istanbul", "Asia/Tokyo") are valid with "multiple timezones should've been valid" | ||
|
||
assert timezone "hello!" is invalid with "single timezone should've been invalid" | ||
assert timezones ("hello!", "Asia/Tokyo") are invalid with "multiple timezones should've been invalid" | ||
erenkarakal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
set {_d} to date(2030, 6, 4, 7, 23) | ||
set {_d.in.nyc} to date(2030, 6, 4, 3, 23) | ||
set {_d.in.istanbul} to date(2030, 6, 4, 10, 23) | ||
|
||
assert difference between ({_d} in timezone "America/New_York") and {_d.in.nyc} < 1 second with "returned incorrect date for New York" | ||
assert difference between ({_d} in timezone "Europe/Istanbul") and {_d.in.istanbul} < 1 second with "returned incorrect date for Istanbul" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.