×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Incorrect result when using EXSLT date:seconds() function in xslt code

Incorrect result when using EXSLT date:seconds() function in xslt code

Incorrect result when using EXSLT date:seconds() function in xslt code

(OP)

I'm using the date:seconds function (from the EXSLT date and time functions) to obtain the number of seconds in a duration string, but I'm getting incorrect results.

For example, for the following xslt code, I would expect the result to be 85904. Instead, I'm getting a result 1000 times greater: 85904000.
<xsl:value-of select="date:seconds('PT23H51M44S')"/>

I'm using Xslt version 1.0. Here is what is at the top of my file --
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:date="http://exslt.org/dates-and-times"

Can anyone help?

RE: Incorrect result when using EXSLT date:seconds() function in xslt code

EGuthrie,

Can you show us a self-contained XML + XSLT combination that reproduces your problem? And name the XSLT processor you're using?

Just tried with Saxon and .NET processors and everything is working as it should.

RE: Incorrect result when using EXSLT date:seconds() function in xslt code

(OP)
Hi atlopes,
I looked further and found that we are using the Javascript implementation of these functions provided by Chris Bays 2001-06-12 --
/**
<doc:module date="2001-06-12">
<doc:module>exslt:date-time</doc:module>
<doc:name>dates</doc:name>
<doc:version>2.0</doc:version>
<doc:language>exslt:javascript</doc:language>
<doc:meta>
<doc:author email="chris@bayes.co.uk" url="http://www.bayes.co.uk">Chris Bayes</doc:author>
<doc:summary>Implementation of EXSLT - Dates (http://www.exslt.org/date)</doc:summary>;
<doc:todo>_validDuration is not 100%</doc:todo>
<doc:todo>All timezone code.</doc:todo>
</doc:meta>
</doc:module>
**/

Here is the function. The part in red below is returning the incorrect value. If I change it to "return oDate.Seconds()/1000", I obtain the desired result--
function seconds(){//
if (arguments.length > 0){
var oDate;
if ((oDate = _validDate(arguments[0], "xs:dateTime")) ||
(oDate = _validDate(arguments[0], "xs:date")) ||
(oDate = _validDate(arguments[0], "xs:gYearMonth")) ||
(oDate = _validDate(arguments[0], "xs:gYear"))
)
return oDate.valueOf()/1000;
else
if (oDate = _validDuration(arguments[0]))
return oDate.Seconds();
else
return Number.NaN;
}else
return Math.floor(new Date().valueOf()/1000);//current local date/time
}

I don't understand enough about his code to determine why it isn't returning the value from the first "if" section, rather than from the "else if" section.
But in any case, shouldn't the result in the "else if" section be divided by 1000?

RE: Incorrect result when using EXSLT date:seconds() function in xslt code

Quote (eguthrie)


I don't understand enough about his code to determine why it isn't returning the value from the first "if" section, rather than from the "else if" section.
But in any case, shouldn't the result in the "else if" section be divided by 1000?
Hi eguthrie,
first of all the next time when you post your code then place it please between the
[code] 
and
[/code] 
tags.
Then it will be better readable for us.
If you don't understand why your code isn't returning A but B then use for example console.log() to print out the computed values for debugging purposes.
By the way, you use in your if-conditions the assignment operator = instead of the comparition operator ==

RE: Incorrect result when using EXSLT date:seconds() function in xslt code

(OP)
mikrom,
That is not my code. It's a snippet from code provided in the implementation of these functions provided June 2001 by Chris Bays in his file date.msxsl.xsl. (You can see a reference to it at http://exslt.org/date/functions/seconds/date.secon....)

RE: Incorrect result when using EXSLT date:seconds() function in xslt code

Hi eguthrie,
OK, but when the JavaScript module is from 2001, then nowaday it's pretty old.
Probably something essential could have changed since there: an another module which is called or a library, which in the year 2001 was originally returning time seconds, but now it returns rather time in milliseconds.
I would not be annoyed with that and just simply divide the result by 1000 and let it go.

RE: Incorrect result when using EXSLT date:seconds() function in xslt code

EGuthrie,

It's an implementation problem. The function Seconds() of the Duration object is returning milliseconds.

CODE --> Javascript

this.Seconds = function(){if (this.years || this.months)return Number.NaN;return this.days*giDayMill + this.hours*giHourMill + this.minutes*giMinMill + this.seconds*giSecMill;} 

where each of gi*Mill variables holds the number in milliseconds for each point in the time scale (days, hours, minutes, and seconds). There is nothing you can do unless change the behavior.

As to the other two questions: the function is returning the value from the "else if" because the first condition returns false for invalid dates (as is the case of duration strings); and @mikrom, the condition is being tested on the result of the assignment, that is, on what oDate holds after the assignment, so it's ok to use = instead of == in this case (it reads as if it were written as (oDate = _validDate(arguments[0], "xs:dateTime")) == true).

RE: Incorrect result when using EXSLT date:seconds() function in xslt code

Hi atlopes,
ok, but according to your investigations the conclusion is to divide the result by 1000.

RE: Incorrect result when using EXSLT date:seconds() function in xslt code

(OP)
I think the function in the file date.msxsl.xsl would need to be adjusted, because it isn't consistent in what it returns. It can return either seconds or milliseconds. So I can't simply divide the function result by 1000.

function seconds(){//
if (arguments.length > 0){
var oDate;
if ((oDate = _validDate(arguments[0], "xs:dateTime")) ||
(oDate = _validDate(arguments[0], "xs:date")) ||
(oDate = _validDate(arguments[0], "xs:gYearMonth")) ||
(oDate = _validDate(arguments[0], "xs:gYear"))
)
return oDate.valueOf()/1000; -->Correct (seconds)
else
if (oDate = _validDuration(arguments[0]))
return oDate.Seconds(); -->**Wrong** (milliseconds)
else
return Number.NaN;
}else
return Math.floor(new Date().valueOf()/1000); -->Correct (seconds) //current local date/time
}

RE: Incorrect result when using EXSLT date:seconds() function in xslt code

Yes, you have to change the behavior of the library at the point I mentioned, the function Seconds of the Duration object. Basically, change it to

CODE --> Javascript

this.Seconds = function(){
  if (this.years || this.months)
    return Number.NaN;
  return (this.days*giDayMill + this.hours*giHourMill + this.minutes*giMinMill + this.seconds*giSecMill) / 1000;
} 

RE: Incorrect result when using EXSLT date:seconds() function in xslt code

(OP)
atlopes,
If I simply modify date.msxsl.xsl to change the faulty statement within function seconds() from
return oDate.Seconds() to return oDate.Seconds()/1000 , that corrects the result.

Is that not the adjustment I should make?




RE: Incorrect result when using EXSLT date:seconds() function in xslt code

EGuthrie,

Your choice, really - forget about my "you have to", it should have been "you may".

I think that the effect in the library will be the same since Duration.Seconds() is called only once, precisely at the point you intend to correct from milliseconds to seconds. It's your choice to correct it at the origin (a function named Seconds() shouldn't return seconds, instead of milliseconds?), or at where the function is called.

RE: Incorrect result when using EXSLT date:seconds() function in xslt code

(OP)
Thanks to both of you for your helpful inputs!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close