Tester
Tester

Reputation: 11

Setting time in UTC in the HTTP request

I want to pass current date and time in yyyy-mm-ddThh:mm:ss format in HTTP request. I want my time to in 24 hrs format and timezone should be UTC. I'm currently using the function ${__time(yyyy-MM-dd'T'hh:mm:ss)} which returns the time in 12-hour format and timezone is being taken as local.

Can someone please help me, how can I get the time in 24-hour format and set timezone as UTC in Jmeter.

Upvotes: 1

Views: 2239

Answers (2)

Ori Marko
Ori Marko

Reputation: 58862

You can use JSR223 component to calculate time using timezone

import java.time.*
import java.time.format.DateTimeFormatter;
LocalDateTime  today = LocalDateTime.now(ZoneId.of( "UTC"));
log.info(today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm:ss")));

Or one (long) liner

${__groovy(import java.time.*;import java.time.format.DateTimeFormatter;LocalDateTime.now(ZoneId.of( "UTC")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm:ss")))}

Upvotes: 0

Dmitri T
Dmitri T

Reputation: 168147

As of JMeter 5.2.1 you can get time only in default timezone using __time() function, if you need to get the time in different timezone you can set user.timezone Java System Property under system.properties file (lives in "bin" folder of your JMeter installation.

If your test assumes generating timestamps in different time zones, to wit the above approach is not an option you can consider switching to the __groovy() function which allows executing arbitrary Groovy code

  1. Current timestamp in the default time zone:

    ${__groovy(new Date().format("yyyy-MM-dd'T'hh:mm:ss"),)}
    
  2. Current timestamp in UTC time zone:

    ${__groovy(new Date().format("yyyy-MM-dd'T'hh:mm:ss"\,TimeZone.getTimeZone("UTC")),)}
    

Demo:

enter image description here

More information: Apache Groovy - Why and How You Should Use It

Upvotes: 1

Related Questions