469,315 Members | 1,538 Online

# 'point in time' time interval

How do I convert a length of time, measured in seconds, into a "point in
time" type time interval or what's represented as: time (second,10,2)

The format is:

P[yY][mM][dD][T[hH][mM][s[.s]S]] where:

y: The number of years (integer, >= 0, not restricted)
m: The number of months (integer, >=0, not restricted)
d: The number of days (integer, >=0, not restricted)
h: The number of hours (integer, >=0, not restricted)
n: The number of minutes (integer, >=0, not restricted)
s: The number of seconds or fraction of seconds (real or integer, >=0,
not restricted). If fractions of a second are used, SCORM further
restricts the string to a maximum of 2 digits (e.g., 34.45  valid,
34.45454545  not valid).
The character literals designators P,Y,M,D,T,H,M,S shall
appear if the corresponding non-zero value is present.

Example:
P1Y3M2DT3H indicates a period of time of 1 year, 3 months, 2 days and 3
hours
PT3H5M indicates a period of time of 3 hours and 5 minutes

It's used by the SCORM RTE to measure the length of session times.

I've had a few goes but the code starts to look like spaghetti then I
have to give up.
Andrew Poulos
Jul 23 '05 #1
4 2683 Andrew Poulos wrote:
How do I convert a length of time, measured in seconds, into a "point in
time" type time interval or what's represented as: time (second,10,2)

You may use the Date object from JavaScript. Create a new Date with your
given seconds (multiply with 1000 to get milliseconds):

var seconds = 200;
var date = new Date(seconds*1000);
date.setFullYear(0);
date.setMinutes(date.getMinutes()+date.getTimezone Offset());

Now you may use all the date methods to get the wanted values

getFullYear();
getMinutes();
getSeconds();

....

Daniel
Jul 23 '05 #2
Andrew Poulos <ap*****@hotmail.com> writes:
How do I convert a length of time, measured in seconds, into a "point in
time" type time interval or what's represented as: time (second,10,2)
Representation not understood.
The format is:

P[yY][mM][dD][T[hH][mM][s[.s]S]] where: y: The number of years (integer, >= 0, not restricted)
m: The number of months (integer, >=0, not restricted)
d: The number of days (integer, >=0, not restricted)
Here you have to make some definitions. Months doesn't all have the
same length, so how many seconds are a month? (there are six different
answers if you include daylight saving time changes).
Likewise, how long is a year? 365 or 366 days?
And depending on what it's used for, the varying length of a day might
also matter, when daylight saving changes.
It's used by the SCORM RTE to measure the length of session times.
No idea what SCORM RTE is either (ok, Google told me), but why not
just count the seconds. What else is needed?
I've had a few goes but the code starts to look like spaghetti then I
have to give up.

Define what you need, then it's faily simple.
If you fix on 24 hours per day, 30 days per month and 12 months per year,
then:
---
function represent(time) {
var res = [];
var secs = time % 60;
if (secs) {
if (secs == Math.floor(secs)) {
res.push(secs + "S");
} else {
res.push(secs.toFixed(2)+"M");
}
}
time = Math.floor(time / 60);

var mins = time % 60;
if (mins) {
res.push(mins + "M");
}
time = Math.floor(time / 60);

var hours = time % 24;
if (hours) {
res.push(hours + "H");
}
time = Math.floor(time / 24);

if (res.length) {
res.push("T");
}

var date = time % 30;
if (date) {
res.push(date + "D");
}
time = Math.floor(time / 30);

var months = time % 12;
if (months) {
res.push(months + "M");
}
time = Math.floor(time / 60);

if (time) {
res.push(time + "Y");
}

res.push("P");

return res.reverse().join("");
}
---

Good luck
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #3
01.iinet.net.au>, dated Tue, 14 Jun 2005 23:15:21, seen in
news:comp.lang.javascript, Andrew Poulos <ap*****@hotmail.com> posted :
How do I convert a length of time, measured in seconds, into a "point in
time" type time interval or what's represented as: time (second,10,2)
What the latter might mean is unclear to me.

The format is:

P[yY][mM][dD][T[hH][mM][s[.s]S]] where:

y: The number of years (integer, >= 0, not restricted)
m: The number of months (integer, >=0, not restricted)
d: The number of days (integer, >=0, not restricted)
h: The number of hours (integer, >=0, not restricted)
n: The number of minutes (integer, >=0, not restricted)
s: The number of seconds or fraction of seconds (real or integer, >=0,
not restricted). If fractions of a second are used, SCORM further
restricts the string to a maximum of 2 digits (e.g., 34.45  valid,
34.45454545  not valid).
The character literals designators P,Y,M,D,T,H,M,S shall
appear if the corresponding non-zero value is present.

Example:
P1Y3M2DT3H indicates a period of time of 1 year, 3 months, 2 days and 3
hours
PT3H5M indicates a period of time of 3 hours and 5 minutes

It's used by the SCORM RTE to measure the length of session times.

I've had a few goes but the code starts to look like spaghetti then I
have to give up.

Since months have varying numbers of days and days can have varying
numbers of hours, there can be no proper answer.

You can get an approximate answer by assuming 30 or 30.6 days per month
and 24 hours per day.
Each paragraph below refers to one line of the following code paragraph.

Create an Object containing zero-duration components.

T, if present, serves only to discriminate months and minutes; so use it
to change the minute-identifier, if present, to Z.

P & T now have no use; remove them, if present.

Insert semicolon separators between the fields.

Split the fields at the semicolons.

For each field, use the final character to index in the Object the place
where the numeric part is then stored, as a Number.

Determine, to check, the interval in (approx) ISO 8601 form

Determine the Duration with assumed component lengths.
X = "P33Y14DT3H5M3.7S" // test value

Obj = {Y:0, M:0, D:0, H:0, Z:0, S:0}
X = X.replace(/T(.*)M/, "T\$1Z")
X = X.replace(/[PT]/g, "")
X = X.replace(/([A-Z])(\d)/g, "\$1;\$2")
X = X.split(';')
for (J in X) { T = X[J] ; Obj[T.charAt(T.length-1)] = parseFloat(X[J]) }
with (Obj) ISO = ((((Y*100+M)*100+D)*100+H)*100+Z)*100+S
with (Obj) Dur = ((((Y*12+M)*30+D)*24+H)*60+Z)*60+S
The four conversions on X could be done in one statement.

If SCORM had followed the ISO 8601 standard for the representation of
duration, the job would have been considerably easier.

If your perplexing remark means seconds with 2 decimal places, then see
in the newsgroup FAQ or via below to convert variable Dur.

--
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
Jul 23 '05 #4
Dr John Stockton wrote:
01.iinet.net.au>, dated Tue, 14 Jun 2005 23:15:21, seen in
news:comp.lang.javascript, Andrew Poulos <ap*****@hotmail.com> posted :
How do I convert a length of time, measured in seconds, into a "point in
time" type time interval or what's represented as: time (second,10,2)

What the latter might mean is unclear to me.
The format is:

P[yY][mM][dD][T[hH][mM][s[.s]S]] where:

y: The number of years (integer, >= 0, not restricted)
m: The number of months (integer, >=0, not restricted)
d: The number of days (integer, >=0, not restricted)
h: The number of hours (integer, >=0, not restricted)
n: The number of minutes (integer, >=0, not restricted)
s: The number of seconds or fraction of seconds (real or integer, >=0,
not restricted). If fractions of a second are used, SCORM further
restricts the string to a maximum of 2 digits (e.g., 34.45  valid,
34.45454545  not valid).
The character literals designators P,Y,M,D,T,H,M,S shall
appear if the corresponding non-zero value is present.

Example:
P1Y3M2DT3H indicates a period of time of 1 year, 3 months, 2 days and 3
hours
PT3H5M indicates a period of time of 3 hours and 5 minutes

It's used by the SCORM RTE to measure the length of session times.

I've had a few goes but the code starts to look like spaghetti then I
have to give up.

Since months have varying numbers of days and days can have varying
numbers of hours, there can be no proper answer.

You can get an approximate answer by assuming 30 or 30.6 days per month
and 24 hours per day.
Each paragraph below refers to one line of the following code paragraph.

Create an Object containing zero-duration components.

T, if present, serves only to discriminate months and minutes; so use it
to change the minute-identifier, if present, to Z.

P & T now have no use; remove them, if present.

Insert semicolon separators between the fields.

Split the fields at the semicolons.

For each field, use the final character to index in the Object the place
where the numeric part is then stored, as a Number.

Determine, to check, the interval in (approx) ISO 8601 form

Determine the Duration with assumed component lengths.
X = "P33Y14DT3H5M3.7S" // test value

Obj = {Y:0, M:0, D:0, H:0, Z:0, S:0}
X = X.replace(/T(.*)M/, "T\$1Z")
X = X.replace(/[PT]/g, "")
X = X.replace(/([A-Z])(\d)/g, "\$1;\$2")
X = X.split(';')
for (J in X) { T = X[J] ; Obj[T.charAt(T.length-1)] = parseFloat(X[J]) }
with (Obj) ISO = ((((Y*100+M)*100+D)*100+H)*100+Z)*100+S
with (Obj) Dur = ((((Y*12+M)*30+D)*24+H)*60+Z)*60+S
The four conversions on X could be done in one statement.

If SCORM had followed the ISO 8601 standard for the representation of
duration, the job would have been considerably easier.

If your perplexing remark means seconds with 2 decimal places, then see
in the newsgroup FAQ or via below to convert variable Dur.

The representation I first mentioned should've been "timeinterval
(second,10,2)" and it's supposedly an IEEE standard for the measurement
of time.

Sorry you the confusion. I'm was reading directly from the specification
which is, I guess by necessity, terse and cross-referenced to a variety
of tables, appendices etc. The standard also gives the option of precise
and less precise definitions of months and years.

As it is I've found a minimal solution: I'm just going to store the time
interval as a total number of seconds and not worry about breaking into
other time units. For example: PT123S is 123 seconds.

Andrew Poulos
Jul 23 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.