muledataweavemule4

Convert a string date in UTC to AEST


I am trying to convert a date in string format in UTC timezone into Australia/Sydney timezone. It seems to work for one value, but not the other, so I am a little confused

%dw 2.0
import * from dw::core::Strings
output application/json

var inputUtc1 = "2024-09-08T08:23:00Z"
var inputUtc2 = "2024-10-15T00:00:00Z"
fun format(d: DateTime) = d as String {format: "yyyy-MM-dd'T'HH:mm:ss"}
---
{
    "a1" : (format(inputUtc1) >> "Australia/Sydney") as String {format: 'yyyy-MM-dd\'T\'HH:mm:ss'},
    "a2" : (format(inputUtc2) >> "Australia/Sydney") as String {format: 'yyyy-MM-dd\'T\'HH:mm:ss'}
}

The output generated is:

{
  "a1": "2024-09-08T18:23:00",
  "a2": "2024-10-15T11:00:00"
}

The value against a1 seems to be correct, but it seems incorrect for a2 2024-10-15T00:00:00Z in UTC should convert into 2024-10-15T10:00:00

However it is converting it into 2024-10-15T11:00:00Z

I'm not sure why it works for one date and not the other? (I am trying this in playground.)


Solution

  • The difference between the conversions is because the daylight savings change in Australia changes in October 6, 2024. Because the first date is before the change and the second date is after the change there is a one hour difference to be expected as normal behavior.

    If you want to have a constant conversion you could use a fixed numeric timezone difference instead of the timezone of a location.

    %dw 2.0
    output application/json
    ---
    {
        l1: |2024-09-08T08:23:00Z| >> "Australia/Sydney",
        l2: |2024-10-15T00:00:00Z| >> "Australia/Sydney",
        d1: |2024-09-08T08:23:00Z| >> "UTC+10",
        d2: |2024-10-15T00:00:00Z| >> "UTC+10"
    }
    

    Output

    {
      "l1": "2024-09-08T18:23:00+10:00",
      "l2": "2024-10-15T11:00:00+11:00",
      "d1": "2024-09-08T18:23:00+10:00",
      "d2": "2024-10-15T10:00:00+10:00"
    }
    

    Unrelated to the issue, if you are going to do date time transformations is better to keep the type as date time instead of converting to String, to avoid doing extra implicit transformations. Example: "2024-09-08T08:23:00Z" as DateTime >> "Australia/Sydney" as String {format:...}