473,394 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

'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 2940
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
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 <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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<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.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #4
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 <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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marcus | last post by:
I am having some problems with trying to perform calculations on time fields. Say I have a start time and an end time, 1:00:00 and 2:30:00 (on a 24 hour scale, not 12). I want to find the...
10
by: Andreas | last post by:
Hi! Is it possible to get a time event at a specific time, for instance eight a'clock? My program is running in the background and is minimized to the tray bar. If not, is there a smooth way...
4
by: Holger Marzen | last post by:
Say, we have uptimes from several servers: Server up_from up_to ------ ------- ------- s1 0:00 8:00 s1 10:00 20:00 s1 22:00 24:00 (would better be...
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
9
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null;...
3
by: arief# | last post by:
Dear all, I'm sorry if this sounds stupid or have been talked about before. Suppose I have a field in my table that's called duration with type 'time without timezone'. How do I do sum on...
4
by: archana | last post by:
Hi all, I want to develop one windows service in which i want to provide some scheduling facility. What i want is to take start time frm one xml file and then at that specified start time. ...
5
by: MathNewbie | last post by:
Hi, I'm trying to do get my head around some, probably basic, math calculations. I checked some .net math libs, but the only thing I learn from studying those is humility ;-). Anyway, i have...
15
by: student4lifer | last post by:
Hello, I have 2 time fields dynamically generated in format "m/d/y H:m". Could someone show me a good function to calculate the time interval difference in minutes? I played with strtotime() but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.