You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by fs...@apache.org on 2020/01/10 17:16:26 UTC

[jmeter] branch master updated: _timeshift function does not work with offset formatters

This is an automated email from the ASF dual-hosted git repository.

fschumacher pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git


The following commit(s) were added to refs/heads/master by this push:
     new 40db97b  _timeshift function does not work with offset formatters
40db97b is described below

commit 40db97b6dc1ca2460bf78d28d5368c83a4345869
Author: Felix Schumacher <fe...@internetallee.de>
AuthorDate: Fri Jan 10 18:12:47 2020 +0100

    _timeshift function does not work with offset formatters
    
    Bugzilla Id: 64070
---
 .../org/apache/jmeter/functions/TimeShift.java     | 40 ++++++++++++----------
 .../jmeter/functions/TestTimeShiftFunction.java    | 17 +++++++++
 xdocs/changes.xml                                  |  2 ++
 3 files changed, 41 insertions(+), 18 deletions(-)

diff --git a/src/functions/src/main/java/org/apache/jmeter/functions/TimeShift.java b/src/functions/src/main/java/org/apache/jmeter/functions/TimeShift.java
index df07c6f..2a321b0 100644
--- a/src/functions/src/main/java/org/apache/jmeter/functions/TimeShift.java
+++ b/src/functions/src/main/java/org/apache/jmeter/functions/TimeShift.java
@@ -19,10 +19,9 @@ package org.apache.jmeter.functions;
 
 import java.time.Duration;
 import java.time.Instant;
-import java.time.LocalDateTime;
 import java.time.Year;
 import java.time.ZoneId;
-import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
 import java.time.format.DateTimeFormatter;
 import java.time.format.DateTimeFormatterBuilder;
 import java.time.format.DateTimeParseException;
@@ -47,16 +46,21 @@ import com.github.benmanes.caffeine.cache.Caffeine;
 
 /**
  * timeShifting Function permit to shift a date
- *
- * Parameters: - format date @see
+ * <p>
+ * Parameters:
+ * <ul>
+ * <li>format date @see
  * https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
- * (optional - defaults to epoch time in millisecond) - date to shift formatted
- * as first param (optional - defaults now) - amount of (seconds, minutes,
- * hours, days ) to add (optional - default nothing is add ) - a string of the locale for the format
- * ( optional ) - variable name ( optional )
- *
- * Returns: a formatted date with the specified number of (seconds, minutes,
- * hours, days or months ) added. - value is also saved in the variable for
+ * (optional - defaults to epoch time in millisecond)</li>
+ * <li>date to shift formatted
+ * as first param (optional - defaults now)</li>
+ * <li>amount of (seconds, minutes, hours, days ) to add (optional - default nothing is add)</li>
+ * <li>a string of the locale for the format ( optional )</li>
+ * <li>variable name ( optional )</li>
+ * </ul>
+ * Returns:
+ * <p>a formatted date with the specified number of (seconds, minutes,
+ * hours, days or months ) added. Value is also saved in the variable for
  * later re-use.
  *
  * @since 3.3
@@ -136,7 +140,7 @@ public class TimeShift extends AbstractFunction {
     public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
         String amountToShift = amountToShiftCompound.execute().trim();
         String dateToShift = dateToShiftCompound.execute().trim();
-        LocalDateTime localDateTimeToShift = LocalDateTime.now(systemDefaultZoneID);
+        ZonedDateTime zonedDateTimeToShift = ZonedDateTime.now(systemDefaultZoneID);
 
         DateTimeFormatter formatter = null;
         if (!StringUtils.isEmpty(format)) {
@@ -154,9 +158,9 @@ public class TimeShift extends AbstractFunction {
         if (!dateToShift.isEmpty()) {
             try {
                 if (formatter != null) {
-                    localDateTimeToShift = LocalDateTime.parse(dateToShift, formatter);
+                    zonedDateTimeToShift = ZonedDateTime.parse(dateToShift, formatter);
                 } else {
-                    localDateTimeToShift = LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(dateToShift)),
+                    zonedDateTimeToShift = ZonedDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(dateToShift)),
                             systemDefaultZoneID);
                 }
             } catch (DateTimeParseException | NumberFormatException ex) {
@@ -169,7 +173,7 @@ public class TimeShift extends AbstractFunction {
         if (!StringUtils.isEmpty(amountToShift)) {
             try {
                 Duration duration = Duration.parse(amountToShift);
-                localDateTimeToShift = localDateTimeToShift.plus(duration);
+                zonedDateTimeToShift = zonedDateTimeToShift.plus(duration);
             } catch (DateTimeParseException ex) {
                 log.error(
                         "Failed to parse the amount duration '{}' to shift "
@@ -179,10 +183,9 @@ public class TimeShift extends AbstractFunction {
         }
         String dateString;
         if (formatter != null) {
-            dateString = localDateTimeToShift.format(formatter);
+            dateString = zonedDateTimeToShift.format(formatter);
         } else {
-            ZoneOffset offset = systemDefaultZoneID.getRules().getOffset(localDateTimeToShift);
-            dateString = String.valueOf(localDateTimeToShift.toInstant(offset).toEpochMilli());
+            dateString = String.valueOf(zonedDateTimeToShift.toInstant().toEpochMilli());
         }
 
         if (!StringUtils.isEmpty(variableName)) {
@@ -205,6 +208,7 @@ public class TimeShift extends AbstractFunction {
                 .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
                 .parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
                 .parseDefaulting(ChronoField.YEAR_OF_ERA, Year.now().getValue())
+                .parseDefaulting(ChronoField.OFFSET_SECONDS, ZonedDateTime.now().getOffset().getTotalSeconds())
                 .toFormatter(format.getLocale());
 
     }
diff --git a/src/functions/src/test/java/org/apache/jmeter/functions/TestTimeShiftFunction.java b/src/functions/src/test/java/org/apache/jmeter/functions/TestTimeShiftFunction.java
index 5e6fb72..6b3bdc9 100644
--- a/src/functions/src/test/java/org/apache/jmeter/functions/TestTimeShiftFunction.java
+++ b/src/functions/src/test/java/org/apache/jmeter/functions/TestTimeShiftFunction.java
@@ -27,6 +27,7 @@ import static org.junit.Assert.assertThat;
 import java.time.Instant;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
+import java.time.ZonedDateTime;
 import java.time.format.DateTimeFormatter;
 import java.time.temporal.ChronoUnit;
 import java.util.Collection;
@@ -128,6 +129,22 @@ public class TestTimeShiftFunction extends JMeterTestCase {
         assertThat(futureDateFromFunction, within(1, ChronoUnit.SECONDS, futureDate));
     }
 
+    @Test
+    void testShiftWithTimeZone() throws Exception {
+        String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
+        String timeString = "2017-12-21T12:00:00.000+0100";
+        Collection<CompoundVariable> params = makeParams(pattern, timeString, "P10DT-1H-5M5S", "");
+        function.setParameters(params);
+        value = function.execute(result, null);
+        ZonedDateTime futureDateFromFunction = ZonedDateTime.parse(value, DateTimeFormatter.ofPattern(pattern));
+
+        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(pattern);
+        LocalDateTime baseDate = ZonedDateTime.parse(timeString, dateFormat).toLocalDateTime();
+        LocalDateTime futureDate = baseDate.plusDays(10).plusHours(-1).plusMinutes(-5).plusSeconds(5);
+        assertThat(futureDateFromFunction.toLocalDateTime(), within(1, ChronoUnit.SECONDS, futureDate));
+
+    }
+
     public static void main(String[] args) {
         System.out.println(java.time.Duration.parse("P10DT-1H-5M5S").toMillis());
     }
diff --git a/xdocs/changes.xml b/xdocs/changes.xml
index ab7bc6d..59c2d7d 100644
--- a/xdocs/changes.xml
+++ b/xdocs/changes.xml
@@ -103,6 +103,7 @@ to view the last release notes of version 5.2.1.
 
 <h3>Functions</h3>
 <ul>
+  <li><bug>64070</bug><code>_timeshift</code> function does not work with offset formatters</li>
 </ul>
 
 <h3>I18N</h3>
@@ -187,6 +188,7 @@ to view the last release notes of version 5.2.1.
 <ul>
   <li>Michael McDermott (mcdermott.michaelj at gmail.com)</li>
   <li>yangxiaofei77 (yangxiaofei77 at gmail.com)</li>
+  <li>Markus Wolf (wolfm at t-systems.com)</li>
 </ul>
 <p>
 Apologies if we have omitted anyone else.