By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 429,261 Members | 2,662 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,261 IT Pros & Developers. It's quick & easy.

# 'point in time' time interval

 P: n/a 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
Share this Question
4 Replies

 P: n/a 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

 P: n/a Andrew Poulos 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: 'Faith without judgement merely degrades the spirit divine.' Jul 23 '05 #3

 P: n/a JRS: In article <42aed83a\$0\$22927\$5a62ac22@per-qv1-newsreader- 01.iinet.net.au>, dated Tue, 14 Jun 2005 23:15:21, seen in news:comp.lang.javascript, Andrew Poulos posted :How do I convert a length of time, measured in seconds, into a "point intime" 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 furtherrestricts 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 shallappear if the corresponding non-zero value is present.Example:P1Y3M2DT3H indicates a period of time of 1 year, 3 months, 2 days and 3hoursPT3H5M indicates a period of time of 3 hours and 5 minutesIt'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 Ihave 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. -- © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 © JL/RC: FAQ of news:comp.lang.javascript jscr maths, dates, sources. TP/BP/Delphi/jscr/&c, FAQ items, links. Jul 23 '05 #4

 P: n/a Dr John Stockton wrote: JRS: In article <42aed83a\$0\$22927\$5a62ac22@per-qv1-newsreader- 01.iinet.net.au>, dated Tue, 14 Jun 2005 23:15:21, seen in news:comp.lang.javascript, Andrew Poulos posted :How do I convert a length of time, measured in seconds, into a "point intime" 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 furtherrestricts 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 shallappear if the corresponding non-zero value is present.Example:P1Y3M2DT3H indicates a period of time of 1 year, 3 months, 2 days and 3hoursPT3H5M indicates a period of time of 3 hours and 5 minutesIt'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 Ihave 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. 