Created
August 1, 2016 21:31
-
-
Save frsyuki/618c4e6c1f5f876e4ee74b9da2fd37c0 to your computer and use it in GitHub Desktop.
This file contains 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
# compile | |
$ javac -cp cron4j-2.2.5.jar Cron4jTzTest.java | |
# expected result if default time zone is Europe/Rome | |
$ java -cp cron4j-2.2.5.jar:. -Duser.timezone=Europe/Rome Cron4jTzTest | |
next time = 2016-08-02T00:00:00Z | |
# unexpected behavior | |
$ java -cp cron4j-2.2.5.jar:. -Duser.timezone=America/Los_Angeles Cron4jTzTest | |
next time = 2016-08-01T07:00:00Z |
This file contains 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
diff -Nurp cron4j-2.2.5/src/it/sauronsoftware/cron4j/Predictor.java cron4j-2.2.5-tz-fix/src/it/sauronsoftware/cron4j/Predictor.java | |
--- cron4j-2.2.5/src/it/sauronsoftware/cron4j/Predictor.java 2011-12-28 19:03:04.000000000 -0800 | |
+++ cron4j-2.2.5-tz-fix/src/it/sauronsoftware/cron4j/Predictor.java 2016-08-01 14:31:06.000000000 -0700 | |
@@ -164,7 +164,7 @@ public class Predictor { | |
// Go a minute ahead. | |
time += 60000; | |
// Is it matching? | |
- if (schedulingPattern.match(time)) { | |
+ if (schedulingPattern.match(timeZone, time)) { | |
return time; | |
} | |
// Go through the matcher groups. |
This file contains 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
import it.sauronsoftware.cron4j.Predictor; | |
import it.sauronsoftware.cron4j.SchedulingPattern; | |
import java.util.Date; | |
import java.util.TimeZone; | |
import java.time.Instant; | |
public class Cron4jTzTest { | |
public static void main(String[] args) { | |
final TimeZone scheduleTimeZone = TimeZone.getTimeZone("UTC"); | |
// daily pattern | |
SchedulingPattern pattern = new SchedulingPattern("0 0 * * *") { | |
// work-around: | |
//@Override | |
//public boolean match(long millis) { | |
// return match(scheduleTimeZone, millis); | |
//} | |
}; | |
// 2016-08-01 00:00:00 -0700 == 2016-08-01 07:00:00 UTC | |
Instant instant = Instant.parse("2016-08-01T07:00:00Z").minusSeconds(1); | |
Predictor utcPredicator = new Predictor(pattern, Date.from(instant)); | |
utcPredicator.setTimeZone(scheduleTimeZone); | |
long nextTime = utcPredicator.nextMatchingTime(); | |
System.out.println("next time = " + Instant.ofEpochMilli(nextTime)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment