Reputation: 133
Hello — I'm using FreeMarker in a ClickDimensions template and I'm having some issues forcing datetimes to use the correct timezone. I have this at the top of my template:
<#setting time_zone="Canada/Pacific">
I've played around with that a little, setting it to different regions (e.g. "America/New_York") and it correctly adjusts the time when I call this:
${.now}
So that's great. However... I have a variable ('startdate') which holds a datetime value pulled from an instance of Microsoft Dynamics. For some reason it believes it's a Pacific datetime (the offset is -07:00) but it's not, it's UTC.
Here are some examples of what I've written:
${startdate}
${startdate?datetime("M/d/yyyy h:mm a")}
${startdate?datetime("M/d/yyyy h:mm a")?string.iso_m}
${startdate?datetime("M/d/yyyy h:mm a")?string.iso_m_nz}
${startdate?datetime("M/d/yyyy h:mm a")?string.iso_m_nz_u}
And this is what I receive:
The third line reveals the problem — the datetime offset is incorrect. It should read +00:00 rather than -07:00.
How do I change the offset associated with a datetime...?
Upvotes: 0
Views: 685
Reputation: 31152
The problem is that the string that you parse to be a datetime doesn't contain the time zone or offset. As datetime
is technically java.util.Date
(not java.time.LocalDateTime
, which can happily remain ignorant of time zone), FreeMarker has to blindly assume some time zone, and that will be the current time zone.
At least as of 2.3.30, there's no "please assume this time zone if the parsed string doesn't contain that information" option for ?datetime
.
The really correct solution is putting those dates into the data model as java.util.Date
-s, not as strings. Ideally, parsing strings to whatever objects (to java.util.Date
in this case) is not the duty of the template.
If you must solve this inside the template, since the format of that string is hard wired into the template anyway, you could do this:
<#function parseMsDynDateTime(s)>
<#return (startdate + " +0000")?datetime("M/d/yyyy h:mm a Z")>
</#function>
and then
${parseMsDynDateTime(startdate)?string.iso_m}
Upvotes: 1