Author Topic: Problem with JS function at Composer  (Read 5757 times)

Offline cavagnaro

  • Administrator
  • Hero Member
  • *****
  • Posts: 7639
  • Karma: 56330
Problem with JS function at Composer
« on: May 30, 2016, 02:55:08 PM »
Hi guys,
Having a headache here...hope someone already come with similar stuff.

Lets see. I have a WS which returns a date in format

AppState.dataWS_Detalhe.dataEntrega = "2016-05-30T18:00:00.000-0300"

Now I call a JS function I declared

[code]
PassouDate(AppState.dataWS_Detalhe.dataEntrega.toString())
[/code]



The JS function is:

[code]
function PassouDate(date1)
{
var dataAtual = new Date();
var dataWS = new Date(date1);

if(dataWS > dataAtual)
return 1; //Ainda nao passou
else
return 0 + ' ' + dataAtual + ' vs ' + dataWS + ' from ' + date1; //Passou

}
[/code]

Nothing from the other world...

Function always return 0 because the output is:

[quote]
0 Mon May 30 2016 11:38:23 GMT-0300 vs Invalid Date from 2016-05-30T18:00:00.000-0300
[/quote]

However if I execute the same exact logic and function from a browser it works as expected...returns 1 or 0 as date is treated as valid...
I already tried calling the function with

[code]
PassouDate(AppState.dataWS_Detalhe.dataEntrega)
[/code]

And have same exact problem...any clue why on earth??? Or a workaround?

Offline hsujdik

  • Hero Member
  • *****
  • Posts: 541
  • Karma: 30
Re: Problem with JS function at Composer
« Reply #1 on: May 30, 2016, 03:35:46 PM »
It seems that the AppState.dataWS_Detalhe.dataEntrega.toString() method has the following format:

YYYY-MM-DDTHH:mm:ss.sss-hhmm

Try to change to

YYYY-MM-DDTHH:mm:ss.sss-hh:mm (adding colon between hh and mm).

Seems the proper string notation for date as specified by ECMAScript Language (referenced on http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15 - section 15.9.1.15 [i]"Z is the time zone offset specified as “Z” (for UTC) or either “+” or “-” followed by a time expression [b]HH:mm[/b]"[/i])

Well, it is just a guess but I think that there's no harm to try it...

Offline cavagnaro

  • Administrator
  • Hero Member
  • *****
  • Posts: 7639
  • Karma: 56330
Re: Problem with JS function at Composer
« Reply #2 on: May 30, 2016, 04:24:50 PM »
Tried:

2016-05-30T11:10:00.000
2016-05-30T11:10:00.000-03:00

Same problem...thanks for the idea

Offline cavagnaro

  • Administrator
  • Hero Member
  • *****
  • Posts: 7639
  • Karma: 56330
Re: Problem with JS function at Composer
« Reply #3 on: May 30, 2016, 06:57:33 PM »
Have tried even simpler date formats and nothing, always get an Invalid Date message.

Funnier
Did this:
x = new Date().toISOString()

x = '2016-05-30T18:31:19.000Z'

Yay! yeah? I found the valid format...a Z you would say. So lets test!
y = new Date(x);
y = Invalid date

XD
This really sucks


again, testing all this on browser works without problems.

Offline cavagnaro

  • Administrator
  • Hero Member
  • *****
  • Posts: 7639
  • Karma: 56330
Re: Problem with JS function at Composer
« Reply #4 on: May 30, 2016, 07:27:21 PM »
More news:

Found 3 valid formats so far:
2009,11,11
Fri Mar 25 2015 09:56:24 GMT+0100 (Tokyo Time)
Wed Mar 25 2015 09:56:24 GMT+0100

Can't find how to do the 2009,11,11 to include hh and mm

Offline cavagnaro

  • Administrator
  • Hero Member
  • *****
  • Posts: 7639
  • Karma: 56330
Re: Problem with JS function at Composer
« Reply #5 on: May 30, 2016, 07:58:55 PM »
SO...
Valid formats so far:

2009,11,11 OK
Fri Mar 25 2015 09:56:24 GMT+0100 (Tokyo Time) OK
Wed Mar 25 2015 09:56:24 GMT+0100 OK
Sat Jun 09 2007 17:46:21 OK
Jun 09 2007 17:46:21 OK
06 09 2007 17:46:21 OK

Will use the last one.


Offline hsujdik

  • Hero Member
  • *****
  • Posts: 541
  • Karma: 30
Re: Problem with JS function at Composer
« Reply #6 on: May 30, 2016, 08:05:06 PM »
What about use a different constructor instead of Date(String)? Even the Spidermonkey Javascript Engine (which MCP uses) does not recommend using this constructor:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Offline cavagnaro

  • Administrator
  • Hero Member
  • *****
  • Posts: 7639
  • Karma: 56330
Re: Problem with JS function at Composer
« Reply #7 on: May 30, 2016, 08:45:43 PM »
The issue is that the WS JS date format is valid...I can't ask them to modify it as is a universal WS and works on other components.
I can't do nothing with date functions with that Date returned...so can do only parsing. finally went with 06 09 2007 17:46:21
Nice info about the JS engine used :D I was searching that
« Last Edit: May 30, 2016, 08:48:13 PM by cavagnaro »

Offline hsujdik

  • Hero Member
  • *****
  • Posts: 541
  • Karma: 30
Re: Problem with JS function at Composer
« Reply #8 on: May 30, 2016, 10:55:34 PM »
I don't remember where I found it, but last time I was looking for it, I saw that MCP uses SpiderMonkey 1.7 (maybe it has been updated already)

Offline hsujdik

  • Hero Member
  • *****
  • Posts: 541
  • Karma: 30
Re: Problem with JS function at Composer
« Reply #9 on: May 30, 2016, 11:21:44 PM »
Found it on VP MCP 8.1.x release notes (https://docs.genesys.com/Special:Repository/gvp_mcp81rn.html?id=da7f5c20-055a-4dd5-bc20-cadd6f900d2e)

[i]Mozilla SpiderMonkey JavaScript engine in the Next Generation VoiceXML Interpreter has been upgraded to version 1.7.[/i]

Offline cavagnaro

  • Administrator
  • Hero Member
  • *****
  • Posts: 7639
  • Karma: 56330
Re: Problem with JS function at Composer
« Reply #10 on: May 30, 2016, 11:23:42 PM »
Using MCP 8.5
Had the hope that an upgrade would help too but no luck


Enviado de meu E6633 usando Tapatalk


Offline hsujdik

  • Hero Member
  • *****
  • Posts: 541
  • Karma: 30
Re: Problem with JS function at Composer
« Reply #11 on: May 31, 2016, 12:14:15 AM »
So, I'm far from being a good developer, but find below an attempt for a function that parses the date in ISO8601 (such as your WS return example).

[code]
function parseISO8601(strISO8601Date) {
var dateRegExp = /^([0-9]{4})-([0-9]{2})-([0-9]{2})(T([0-9]{2}):([0-9]{2}):([0-9]{2})\.([0-9]{3})(Z|[-+][0-9]{2}:?[0-9]{2}))?$/i;
var match = dateRegExp.exec(strISO8601Date);
var year = parseInt(match[1]);
var month = parseInt(match[2]);
var day = parseInt(match[3]);
var hasTime = match[4];
var hour;
var minute;
var second;
var mils;
var tz;
var mydate;
if (hasTime && (typeof hasTime != 'undefined')) {
  hour = parseInt(match[5]);
  minute = parseInt(match[6]);
  second = parseInt(match[7]);
  mils = parseInt(match[8]);
  tz = match[9];
  mydate = new Date(year, month-1, day, hour, minute, second, mils);
  if ("Z" === tz.toUpperCase()) {
  mydate.setTime(mydate.getTime() - mydate.getTimezoneOffset() * 60000);
  } else {
  tz = tz.replace(":","");
  var tzhour = parseInt(tz.substr(1,2));
  var tzmin = parseInt(tz.substr(3,2));
  var tzOffset = (tz.substr(0,1) === "+") ? ((tzhour * 3600000) + (tzmin * 60000)) : -((tzhour * 3600000) + (tzmin * 60000));
  var diff =  -tzOffset - (mydate.getTimezoneOffset() * 60000);
  mydate.setTime(mydate.getTime() + diff);
  }
} else {
  mydate = new Date(year, month-1, day);
}
return mydate;
}
[/code]

If you can try it out on MCP and post results that would be great as I don't have a lab ready for this kind of testing (although I have tested against FireFox without problems).
« Last Edit: May 31, 2016, 12:47:26 AM by hsujdik »

Offline cavagnaro

  • Administrator
  • Hero Member
  • *****
  • Posts: 7639
  • Karma: 56330
Re: Problem with JS function at Composer
« Reply #12 on: May 31, 2016, 02:32:08 AM »
Nice ;) but I ended up doing something simpler, need to think on guys who will support this

[code]
function PassouDate(date1)
{
//06 09 2007 17:46:21 OK
//date1 = 2016-04-18T03:00:00.000+0000

var YYYY = date1.substr(0,4);
var MM = date1.substr(5,2);
var DD = date1.substr(8,2);
var HHMMSS = date1.substr(11,8);

date1 = YYYY + ' ' + MM + ' ' + DD + ' ' + HHMMSS;

var dataAtual = new Date();
var dataWS = new Date(date1);

if(dataWS > dataAtual)
return 1; //Ainda nao passou
else
return 0; //Passou

}
[/code]