4

I want to generate date in the format 2019-03-28T15:30:59+12:00 using wiremock.

I tried:

"currentDateTime": "{{now timezone='Australia/Sydney' format='yyyy-MM-dd'T'HH:mm:ssZ'}}"

but, I get exception:

wiremock.com.github.jknack.handlebars.HandlebarsException: inline: found: ''yyyy-MM-dd'T'HH:mm:ssZ'', expected: 'no viable alternative at input ''yyyy-MM-dd'T'HH:mm:ssZ'''

I have also tried escaping both the quotes around T, but it does not work.

What I am doing wrong?

Rajkishan Swami
  • 3,569
  • 10
  • 48
  • 68
  • The quotes at your format string are unbalanced due to the quoted T in the middle: `format='yyyy-MM-dd'T'HH:mm:ssZ`'. I guess you have to escape this somehow. – TobiSH Aug 22 '19 at 05:45
  • @RajKishan How did you solve this problem? – Shahbaz Ahmad Sep 04 '19 at 12:58
  • @ShahbazAhmad I did not find a direct solution. So I used a hack instead. What I did is, put a placeholder in place of the date like `"currentDateTime":"currentTimePlaceholder"`. Then in a custom transformer (extend `ResponseTransformer`), replace that string with the desired value which is easy to do. – Rajkishan Swami Sep 05 '19 at 04:33

6 Answers6

4

In case anyone else comes across this post in the future, the simplest (but hacky) solution that I used is to format the parts before and after the 'T' separately like so:

"currentDateTime": "{{now timezone='Australia/Sydney' format='yyyy-MM-dd'}}T{{now timezone='Australia/Sydney' format='HH:mm:ssZ'}}"
3

A simple workaround:

  1. Declare a variable (myformat in the example)

    {{#assign 'myformat'}}yyyy-MM-dd'T'HH:mm:ssZ{{/assign}}

  2. Use it in the stub/mock

    {{now format=myformat}}

A sample templated response is -

{
    "time": "{{#assign 'myformat'}}yyyy-MM-dd'T'HH:mm:ssZ{{/assign}}{{now format=myformat}}"
}

References

arif
  • 524
  • 8
  • 18
Marek
  • 31
  • 2
3

Try using following block of code. Working fine for me and probably the best way. format='yyyy-MM-dd\'T\'HH:mm:ss.SSSXXX'

3

If you need to include the date in the ISO 8601 format, you can omit the format option:

{{now timezone='Australia/Sydney'}}

It will produce the following result: 2021-06-09T05:45:53+10:00. If you omit the timezone and just use {{now}} - it will produce a date in UTC: 2021-06-08T19:48:27Z.

For reference, you can check the RenderableDate class in wiremock

Arthur Kalimullin
  • 462
  • 1
  • 5
  • 14
0
"body": "{\"datetime\": \"{{now format='yyyy-MM-dd\\'T\\'HH:mm:ss'}}\"}"
G1yyK
  • 216
  • 2
  • 7
  • Please add a short description and explanation to your code instead of just posting the code snippet. Thanks! – dns_nx Sep 24 '20 at 08:04
0

Simpler version that works for formatting in Wiremock

"{{now format='yyyy-MM-dd'}}T{{now format='HH:mm:ss.SSS'}}Z"
Roi
  • 1,434
  • 2
  • 11
  • 13