Azure API Management DotLiquid Template TimeZone conversion

123 views Asked by At

I am writing a transformation policy for an Azure APIM API. I am having trouble transforming an inbound Date Time field in the JSON formatted Request Body to an output format of the same TimeZone.

The input is formatted: "transactionDateTime": "2023-11-08T18:28:12.123+10:00"

The output needs to be formatted (and in AEST which is +10:00) - this is a hard requirement, so please don't bother to suggest changing it: "2023-11-08T18:28"

Test Harness Response

Test Harness Code 1

Test Harness Code 2

DateTimeStyles not available

I would just treat it as a string (and truncate) if I could, but DotLiquid has determine it is a DateTime and stored it as UTC.

I've tried using DotLiquid (with .Net Date format notation - not the Ruby one):

    {{header.transactionDateTime | Date: "yyyy-MM-ddTH:mm" }}

which outputs in UTC (shown in testing a 7am time changes to the day before and the hour to 21): "2023-11-08T8:28"

I've been trying to get access to the Params culture and dateFormat visible in documentation and in the DotLiquid code base. Culture is used in the date filter formatting function:

https://github.com/dotliquid/dotliquid/blob/229ddbd6ee96dc8fb6d32df6bf1ea5b927d1597a/src/DotLiquid/StandardFilters.cs#L621

The documentation shows an example: {% param culture='fr-FR' %} I've tried various combinations (eg. Uppercasing Param) but APIM doesn't want to accept it.

I've also tried using a set-variable policy before the set-body to try setting it, but without guidance I was just stabbing in the dark.

Liquid has a filter in_time_zone but it doesn't exist in the DotLiquid implementation.

From this question I'd ideally like a Azure APIM DotLiquid compatible Date filter format string to enable me to transform the original DateTime to the required output format, failing that, retrieving and/or setting the DotLiquid Template Parameters in an Azure APIM Policy to set the Culture/dateformat, failing that, bonus points for a set-variable c# expression to get the value from the Request Body JSON (path: header.transactionDateTime) and formatting the date to the required output format.

For reference my updated workaround code is:

<set-variable name="obligationDateTimeAEST" value="@{
    // Required output needs to be AEST but with no TZ in the output string ie. yyyy-MM-ddTHH:mm
    // Workaround for DotLiquid and C# expressions both automatically convert all dates to UTC
    // DotLiquid doesn't have any options for Timezone output or Date arithmetic
    
    JObject inBody = context.Request.Body.As<JObject>(preserveContent: true);
    DateTime dt = (DateTime) inBody.SelectToken("header.transactionDateTime");
    if (dt != null && dt is DateTime) {
      try {
        // Assumption that C# is automatically converting all dates to UTC and so adding 10 will output AEST
        dt = dt.AddHours(10);
        return dt.ToString("yyyy-MM-ddTHH:mm");
      } catch (ArgumentOutOfRangeException aoore) {
        return "Failed AEST Timezone convert";
      }
    } else {
      return "header.transactionDateTime not available";
    }
}" />
0

There are 0 answers