I am trying to use JS policy for events logging in apigeeX that will automatically adjust to "Daylight saving time". I want to log the EST . I added the "moment-timezone-with-data-10-year-range.js" & "moment.min.js" libraries to my resources & I configured Like below.
I am also using the following JS code to get the EST time
var offset;
var isDST = moment().tz('America/New_York').isDST();
if(isDST=== true)
{
offset = '-04:00';
}
else
{
offset = '-05:00';
}
var est_time = moment().utcOffset(offset).format('YYYY-MM-DD-HH:mm:ss a');
context.setVariable('est_time', est_time); ---> this variable will be called from another policy.
But I am not able to see the "est_time" value printed in the calling policy.
Can you guys help me with what I miss here?
Thank you in advance.
Solved! Go to Solution.
Yes, I get what you're saying. And my suggestion is, if you use something like momentjs or dayjs, the library should handle the shifting offset for you. You shouldn't need the check in your own code.
OR, just use UTC and there's no need for any off that.
Just looking at the name of your JS step - it's "calculateLatency". Are you really just trying to calc latency ? If so you don't need to know what time it is in New York. You just need to subtract system.timestamp from point 1 in the execution (start), to point 2 (end). You don't need to format the start and end as strings. Just subtract them and you get a timespan, a duration.
and also , if you are measuring a particular policy step, you can just read the context variable
apigee.metrics.policy.POLICY-NAME.timeTaken