For another function, please refer Functions and Variables post


The time function returns the current time in various formats.

Parameters

Attribute
Description
Required
Format
The format to be passed to SimpleDateFormat. The function supports various shorthand aliases. If omitted, the function returns the current time in milliseconds since the epoch.
No
Name of variable
The name of the variable to set.
No

1. Context

Sometimes in your test plan, a test request need a time parameter at the time executed that request, and it looks like 1493216744317 (milliseconds), or 1471425829 (seconds). It can be a SimpleDateFormat time:

  • 22:16:46
  • 04/26/17 8:14 PM
  • Wed Apr 26 22:16:46 ICT 2017
  • April 26, 2017 10:16:46 PM
  • 26-04-2017 22:16:46
  • Wed, Apr 26, 2017

Or any parameter related to the time. If you’re looking for something to help to do this, __time is the best choice in that case.

2. Using

2.1 Default

The time function uses the format to parsed the current time to SimpleDateFormat. But it’s just an optional so we can omit it. And it will return the current time in milliseconds. i.e. 1493218009407

Use: ${__time} or ${__time()} or ${__time(,)}

We can use one of three ways above, but I prefer to use ${__time} because it’s short and easy to remember.

Example 1: (Download here) I use Dummy Sampler to show the result of three functions above. I inputted these lines into Response Data field of Dummy Sampler.

Way 1: ${__time}
Way 2: ${__time()}
Way 3: ${__time(,)}

__time-function-example-1-1

Run and see the result:

__time-function-example-1-2.png

Way 1: 1493271514296
Way 2: 1493271514296
Way 3: 1493271514296

You can see, all of them has the same result, returns the current time in milliseconds.

2.2 Format

We can use the format as a parameter in the time function, to parse the current time to SimpleDateFormat we want. I provide some sample above: 04/26/17 8:14 PM or Wed, Apr 26, 2017. These formats are represented by some patterns.

i.e. mm/dd/yyyy hh:mm a represent for 04/26/17 8:14 PM and E, MMM dd, yyyy represent for Wed, Apr 26, 2017

So how can we know all patterns and what do they represent? Don’t worry, just refer the table below.

Letters Description Example Output
y The full year 2017
yyy
yyyy
yy Two-digits year 17
M One-digit month 4, 11
MM Two-digit month 04, 11
MMM Three-letter abbreviation for month Apr, Nov
MMMM Month spelled out in full April, November
d One-digit day in month 5, 15, 25
dd Two-digit day in month 05, 15, 25
E Three-letter abbreviation for day name in week Mon, Sun
EE
EEE
EEEE Weekday name spelled out in full Monday, Sunday
h One-digit hour in AM/PM time 7, 10
hh Two-digit hour in AP/PM time 07, 10
H One-digit hour in 24 hr time 2, 13, 21
HH Two-digit hour in 24 hr time 02, 13, 21
m One-digit minute in time 7, 18, 58
mm Two-digit minute in time 07, 18, 58
s One-digit second in time 6, 26, 46
ss Two-digit second in time 06, 26, 46
S One-digit millisecond in time 9, 37, 989
SS Two-digit millisecond in time 09, 37, 989
SSS Three-digit millisecond in time 009, 037, 989
a AM/PM AM, PM
z General Time zone ICT
zzzz General Time zone Indochina Time
X ISO 8601 time zone +07
XX ISO 8601 time zone +0700
XXX ISO 8601 time zone +07:00
w One-digit week in the year 4, 47
ww Two-digit week in the year 04, 47
W One-digit week in the month 1, 5
D Day in the year 117
F Week number in the month 1, 2, 3, 4, 5
u Day number in the week 1 (Monday), 2, 3, 4, 5, 6, 7
k Hour in the day (1-24) 1, 12, 21
kk Hour in the day (1-24) 01, 12, 21

Now, it’s time to go back some example SimpleDateFormat in the Context section:

2.2.1

22:16:46 –> It’s equal to the format pattern HH:mm:ss

And input this format as the parameter of the __time function, we will have:

${__time(HH:mm:ss)}

2.2.2

04/26/17 8:14 PM –> It’s equal to the format pattern MM/dd/yy h:mm a

And input this format as the parameter of the __time function, we will have:

${__time(MM/dd/yy h:mm a)}

2.2.3

Wed Apr 26 22:16:46 ICT 2017 –> E MMM dd HH:mm:ss z yyyy

${__time(E MMM dd HH:mm:ss z yyyy)}

2.2.4

April 26, 2017 10:16:46 PM –> MMMM dd, yyyy hh:mm:ss a

NOTE: In other cases above, if the format needs some separators of the components (/ – slash, . – dots or full stops, - – hyphens or dashes,   – spaces), we just put it in the format pattern, very easy. But in this case, we have a little bit different, If a function parameter contains a comma, then be sure to escape this with \ , otherwise, JMeter will treat it as a parameter delimiter.

${__time(MMMM dd\, yyyy hh:mm:ss a)}

2.2.5

26-04-2017 22:16:46 –> dd-MM-yyyy HH:mm:ss

${__time(dd-MM-yyyy HH:mm:ss)}

2.2.6

Wed, Apr 26, 2017 –> EEE, MMM dd, yyyy

The same as above, we need to escape the comma with \, and the function looks like

${__time(EEE\, MMM dd\, yyyy)}

Example 2: (Download here) Now I will config all the example above in JMeter and try to run. I use Debug Sampler to show the result. Input the text above into Response Data of Dummy Sampler:

Format Time
HH:mm:ss --> ${__time(HH:mm:ss)}
MM/dd/yy h:mm a --> ${__time(MM/dd/yy h:mm a)}
E MMM dd HH:mm:ss z yyyy --> ${__time(E MMM dd HH:mm:ss z yyyy)}
MMMM dd\, yyyy hh:mm:ss a --> ${__time(MMMM dd\, yyyy hh:mm:ss a)}
dd-MM-yyyy HH:mm:ss --> ${__time(dd-MM-yyyy HH:mm:ss)}
EEE, MMM dd, yyyy --> ${__time(EEE\, MMM dd\, yyyy)}

__time-function-example-2-1.png

Run and observe the result

__time-function-example-2-2.png

Hope these examples will help you all can understand and use the format parameter efficiently to generate the time format as you need

2.3 Shorthand Aliases

With the time function, we have some shorthand aliases are predefined in jmeter.properties file

#---------------------------------------------------------------------------
# __time() function configuration
#
# The properties below can be used to redefine the default formats
#---------------------------------------------------------------------------
#time.YMD=yyyyMMdd
#time.HMS=HHmmss
#time.YMDHMS=yyyyMMdd-HHmmss
#time.USER1=
#time.USER2= 

So when we call the function with shorthand aliases, it will return the value base on this

${__time(YMD)} will return the value with format yyyyMMdd i.e.  20170427

${__time(HMS)} will return the value with format HHmmss i.e. 201236

${__time(YMDHMS)} will return the value with format yyyyMMdd-HHmmss i.e. 20170427-214635

For the aliases USER1 and USER2, it’s for you to define your own format. If you don’t do anything and use these aliases, it will return nothing

Example 3: (Download here) I continue to use Dummy Sampler fo this example. Please remember this example should work before you change the default value of alias as below

__time-function-example-3-1.png

Run and observe the result

__time-function-example-3-2.png

If you want to change the format of these aliases, let do the following steps:

  • Copy these alias
  • Open the user.properties file (under ​​​/${JMETER}/bin/ folder
  • Paste those alias into the end of user.properties file, uncomment them (remove # character at beginning of each line)
  • And input your new format:
    # (you're seeing these lines in the user.properties file, remember)
    #---------------------------------------------------------------------------
    # __time() function configuration
    #
    # The properties below can be used to redefine the default formats
    #---------------------------------------------------------------------------
    time.YMD=yy-MM-dd
    time.HMS=HH:mm:ss
    time.YMDHMS=yyyy/MM/dd HH:mm:ss
    time.USER1=dd-MM-yyyy HH:mm:ss
    time.USER2=M/dd/yy h:mm a

After saving the user.properties file, you should close all JMeter UI and re-open it. Then you can run the Test Plan the same as Example 3. And now let see what we have

__time-function-example-3-3.png

It applied the new format which we defined in user.properties file. And also aliases USER1 and USER2 work now.

2.4 Store to variable

One more thing, after generating the properly time format, you might want to store that value somewhere for later use. Now, it’s time to use the parameter name of the variable. It separates with time format by a comma.

Example 4: (Download here) I will use the Dummy Sampler, input this one into Response Data area: ${__time(hh:mm:ss a,CURRENT_TIME)} . This means I will get the current time with the format “hh:mm:ss a“, and store that value into variable CURRENT_TIME. After that, I will prove that variable hold the value by calling it in another place, using syntax ${CURRENT_TIME}

__time-function-example-4-1.png

The time function and the variable should return the same value, let’s see

__time-function-example-4-2.png

So, after storing with a variable, you can use the generated time value in anywhere you want by calling the variable like ${VARIABLE_NAME}.

NOTE: To open my example files, you need to install Dummy Sampler plugin and use jmeter version 3.2


For another function, please refer Functions and Variables post.

Advertisements