[Freeswitch-users] Help writing a simple JS script

Chad Engler Chad.Engler at patlive.com
Thu Oct 25 17:27:08 MSD 2012


JavaScript timestamps are expressed in milliseconds, and the timestamps from freeswitch are in nanoseconds, so you need to convert the timestamp to ms before passing it to Date:

 

var date = new Date(argv / 1000);

 

-Chad

 

From: freeswitch-users-bounces at lists.freeswitch.org [mailto:freeswitch-users-bounces at lists.freeswitch.org] On Behalf Of George Diamantopoulos
Sent: Wednesday, October 24, 2012 11:49 PM
To: freeswitch-users at lists.freeswitch.org
Subject: [Freeswitch-users] Help writing a simple JS script

 

Hello,

I've been trying to write a simple JS script, which is called from within the XML dialplan, in order to convert the value of ${created_time} to something more meaningful than UNIX time in ms. The problem is when I try to print something to the console (see log application below), I get NaN (not a number) for variable ${created_date}, even though I don't want a number (a string is fine, I tried enforcing this with toString()). Moreover, you'll notice I've left out var month = date.getMonth();, because if I don't I get the following error: TypeError: date.getMonth is not a function. I can't figure out why, it's documented in JS in the date object, and the syntax is identical to other functions I use to acquire object values. Any hints would be greatly appreciated, I've been trying different code variations for over two hours now and I'm stuck. Thanks.

Here's the call in the dialplan:

<action application="javascript" data="unixtocal.js $created_time"/>

And here's the JS script itself:

var date = new Date(argv);
var hours = date.getHours();
hours = hours.toString();
var minutes = date.getMinutes();
minutes = minutes.toString();
var seconds = date.getSeconds();
seconds = seconds.toString();
var ms = date.getMilliseconds();
ms = ms.toString();
var year = date.getFullYear();
year = year.toString();
var day = date.getDate();
day = day.toString();

session.execute("set", "created_year="+year);
session.execute("set", "created_date="+day);
session.execute("set", "created_hours="+hours);
session.execute("set", "created_minutes="+minutes);
session.execute("set", "created_seconds="+seconds);
session.execute("set", "created_ms="+ms);

Then the dialplan takes over again, for example:

<action application="log" data="INFO Date is ${created_date}"/>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freeswitch.org/pipermail/freeswitch-users/attachments/20121025/39d83db6/attachment.html 


Join us at ClueCon 2011 Aug 9-11, 2011
More information about the FreeSWITCH-users mailing list