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
Maybe the reason your code is not working is that momentjs is not ES5. In case you were not aware: The JS callout in Apigee does not run in a current ES6-and-later compatible JavaScript engine. It uses ES5+, in the Rhino engine. You cannot use require, const, spread syntax, arrow functions, Promises, lots of other stuff. To use a JS module , you may need to convert it (transpile it) to ES5. There are tools like browserify and rollup that do this.
Also, You may wish to reconsider your use of Moment. Have you seen this: "You don't need Moment"? Dayjs is a nice, small alternative. You will need a "plugin" to get timezone specific formatting. I think ideally there should not be "isDST" tests in your own code - the library ought to handle that. Dayjs does. Here is an example.
Disclaimer: I've never used dayjs in a JS callout within Apigee. Here again, you will need to browserify it, or use some other transpiler thing. You need to generate an ES5-compatible source module to include as a module.
Related to your request, the internet consensus seems to be that you should log times in UTC to prevent confusion. If you did that you would avoid the "how can I get a time formatted in EST" issue entirely.
Good luck!
Thank you @dchiesa1 . The thing is, if I hard coded the offset it is working. But I should have to update the code twice a year.
var est_time = moment().utcOffset('-05:00').format('YYYY-MM-DD-HH:mm:ss a');
this still works.
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
As per your suggestion I removed the if statement & my code looks like
var est_time = moment().tz('America/New_York').format('YYYY-MM-DD-HH:mm:ss a');
I am trying to pring "est_time" calling from another policy But I couldn't.
ok. Did you verify that your the moment library you are including is ES5-friendly?
Did you check the execution with a debugsession (trace)? it's possible that JS step is not running at all. If the Moment module is ES6, it may fail to compile inside Apigee, which means it won't execute and you won't see your string.
I have never tried to include momemtjs into an Apigee callout, so I don't know if it will work.
I don't know about the ES5, but I am using "/moment-timezone-with-data-10-year-range.js"(https://momentjs.com/timezone/).
OK. You need to verify that the module is ES5 friendly and will work in Rhino.
I don't know about the ES5,
I suggest you look into that. Also check the trace - it may give you a hint.