JMeter中将当前系统时间转换为GMT及EPOCH时间的实现方案咨询
JMeter IST时间转GMT、EPOCH时间实现方案
首先明确IST为印度标准时间,对应时区ID为Asia/Kolkata,偏移为UTC+5:30,以下是两种可行实现方法:
方法1:内置函数实现(无需编码,适合简单场景)
- IST转GMT时间(格式
dd/MM/yyyy HH:mm:ss)
直接调用JMeter自带的__timeShift函数,指定输出时区为GMT即可:
执行后生成的GMT时间会自动存入${__timeShift(dd/MM/yyyy HH:mm:ss,,P0D,GMT,gmt_time)}gmt_time变量,后续可直接引用。 - IST转EPOCH时间
EPOCH时间本身以UTC为基准,无需额外做时区转换,直接调用__time函数即可:- 13位毫秒级EPOCH:
${__time(,epoch_milli)} - 10位秒级EPOCH:
${__jexl3(${__time(,)} / 1000,epoch_second)}
- 13位毫秒级EPOCH:
方法2:JSR223 Groovy脚本实现(灵活度高,适合复杂逻辑)
推荐使用Groovy语言,性能优于BeanShell,操作步骤如下:
- 按需添加JSR223预处理/后处理器到对应请求下
- 填入以下脚本:
// 指定IST时区 def istZone = TimeZone.getTimeZone("Asia/Kolkata") def currentIstTime = Calendar.getInstance(istZone).getTime() // 转换为指定格式GMT时间 def gmtSdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss") gmtSdf.setTimeZone(TimeZone.getTimeZone("GMT")) vars.put("gmt_time", gmtSdf.format(currentIstTime)) // 转换为EPOCH时间 def epochMilli = currentIstTime.getTime() def epochSecond = (epochMilli / 1000) as long vars.put("epoch_milli", epochMilli.toString()) vars.put("epoch_second", epochSecond.toString())
注意:格式符中大写MM代表月份,小写mm代表分钟,切勿混淆避免格式错误
内容的提问来源于stack exchange,提问作者user16999478
相关产品推荐
相关产品推荐

