You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@jmeter.apache.org by bu...@apache.org on 2012/06/05 12:10:45 UTC

[Bug 53362] New: Add 'days to add' to __time function

https://issues.apache.org/bugzilla/show_bug.cgi?id=53362

          Priority: P2
            Bug ID: 53362
          Assignee: issues@jmeter.apache.org
           Summary: Add 'days to add' to __time function
          Severity: enhancement
    Classification: Unclassified
                OS: All
          Reporter: spam.moreplz@gmail.com
          Hardware: All
            Status: NEW
           Version: 2.7
         Component: Main
           Product: JMeter

Created attachment 28888
  --> https://issues.apache.org/bugzilla/attachment.cgi?id=28888&action=edit
src file, not including single change to message.properties

Needed a __time function with the ability to add n days before returning a date
string so I created one.

Patch form:

message.properties
+ time_add=Number of days to add (optional)

--- <unnamed>
+++ <unnamed>
@@ -19,6 +19,7 @@
 package org.apache.jmeter.functions;

 import java.text.SimpleDateFormat;
+import java.util.Calendar;
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashMap;
@@ -36,7 +37,6 @@

 /**
  * __time() function - returns the current time in milliseconds
- * @since 2.2
  */
 public class TimeFunction extends AbstractFunction {

@@ -49,6 +49,7 @@

     static {
         desc.add(JMeterUtils.getResString("time_format")); //$NON-NLS-1$
+        desc.add(JMeterUtils.getResString("time_add")); //$NON-NLS-1$
         desc.add(JMeterUtils.getResString("function_name_paropt"));
//$NON-NLS-1$
         aliases.put("YMD", //$NON-NLS-1$
                 JMeterUtils.getPropDefault("time.YMD", //$NON-NLS-1$
@@ -67,6 +68,7 @@

     // Ensure that these are set, even if no paramters are provided
     private String format   = ""; //$NON-NLS-1$
+    private String addDays = ""; //$NON-NLS-1$
     private String variable = ""; //$NON-NLS-1$

     public TimeFunction(){
@@ -86,7 +88,16 @@
                 fmt = format;// Not found
             }
             SimpleDateFormat df = new SimpleDateFormat(fmt);// Not
synchronised, so can't be shared
-            datetime = df.format(new Date());
+            
+            Date date = new Date();
+
+            if(addDays.length() > 0) {
+                Calendar c = Calendar.getInstance();
+                c.setTime(date);
+                c.add(Calendar.DATE, Integer.parseInt(addDays)); //Add or
subtract n days to the date object. //TODO BUT no support for the milliseconds
bit above. Add?
+                date = c.getTime();
+            }
+            datetime = df.format(date);
         }

         if (variable.length() > 0) {
@@ -102,7 +113,7 @@
     @Override
     public synchronized void setParameters(Collection<CompoundVariable>
parameters) throws InvalidVariableException {

-        checkParameterCount(parameters, 0, 2);
+        checkParameterCount(parameters, 0, 3);

         Object []values = parameters.toArray();
         int count = values.length;
@@ -112,7 +123,11 @@
         }

         if (count > 1) {
-            variable = ((CompoundVariable)values[1]).execute().trim();
+            addDays = ((CompoundVariable)values[1]).execute().trim();
+        }
+        
+        if (count > 2) {
+            variable = ((CompoundVariable)values[2]).execute().trim();
         }

     }
@@ -127,4 +142,4 @@
     public List<String> getArgumentDesc() {
         return desc;
     }
-}+}

-- 
You are receiving this mail because:
You are the assignee for the bug.

[Bug 53362] Add 'days to add' to __time function

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=53362

--- Comment #4 from spam.moreplz@gmail.com ---
KISS, a month is 30 days, a year 365 days :)
I'm aware of the oversimplification, but I'd estimate this works for 99% of use
cases

I mostly use it for 'tomorrow' (+1) or for example a range: 'this month' (0,
+30)

Aware of the workarounds, but I can't be the only person who'd like to be able
to use a simple 'tomorrow' function without having to write code.

I'd happily create a new function or append the current one. Would you prefer a 

__time(format, variable, +/-Days) (reordered for compatebility)

or a new function?

proposal:

__timeMath(format, number, m|h|d|w|M|y**, variable)

** applied to number, ie +/- 1 minute, hour, day, week, month or year. Only one
option is possible.

-- 
You are receiving this mail because:
You are the assignee for the bug.

[Bug 53362] Add 'days to add' to __time function

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=53362

--- Comment #2 from spam.moreplz@gmail.com ---
Seemed like an overkill change for such limited functionality but I don't
disagree with you either. It does change the contract and breaks (backwards?)
compatability. I'll see if I can refactor it nicely and minimize duplicity. 

Actually,  as it's an optional parameter, how about adding it to the end
instead?

-- 
You are receiving this mail because:
You are the assignee for the bug.

[Bug 53362] Add 'days to add' to __time function

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=53362

Philippe Mouawad <p....@ubik-ingenierie.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |p.mouawad@ubik-ingenierie.c
                   |                            |om
         Resolution|---                         |WONTFIX

--- Comment #1 from Philippe Mouawad <p....@ubik-ingenierie.com> ---
This would change the contrat of the function.
I suggest you maybe create a new function.

-- 
You are receiving this mail because:
You are the assignee for the bug.

[Bug 53362] Add 'days to add' to __time function

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=53362

--- Comment #3 from Sebb <se...@apache.org> ---
Not sure why days are special; surely there must also be use cases for other
date calculations?

Note that there is a work-round using the scripting functions - it's easy
enough to create functions to do this using Beanshell, Jexl, or Javascript.

-- 
You are receiving this mail because:
You are the assignee for the bug.

[Bug 53362] Add 'days to add' to __time function

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=53362

--- Comment #5 from Sebb <se...@apache.org> ---
(In reply to comment #4)
> KISS, a month is 30 days, a year 365 days :)
> I'm aware of the oversimplification, but I'd estimate this works for 99% of
> use cases
> 
> I mostly use it for 'tomorrow' (+1) or for example a range: 'this month' (0,
> +30)
> 
> Aware of the workarounds, but I can't be the only person who'd like to be
> able to use a simple 'tomorrow' function without having to write code.
> 
> I'd happily create a new function or append the current one. Would you
> prefer a 
> 
> __time(format, variable, +/-Days) (reordered for compatebility)
> 
> or a new function?
> 
> proposal:
> 
> __timeMath(format, number, m|h|d|w|M|y**, variable)
> 
> ** applied to number, ie +/- 1 minute, hour, day, week, month or year. Only
> one option is possible.

Bugzilla is not ideal for discussions; best to raise this on the user list to
see what (if anything) other users would be interested in.

The dev list would be the place to discuss implementation issues.

-- 
You are receiving this mail because:
You are the assignee for the bug.