'point in time' time interval

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Poulos

    '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
  • Daniel Kirsch

    #2
    Re: 'point in time' time interval

    Andrew Poulos wrote:[color=blue]
    > 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)[/color]

    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*10 00);
    date.setFullYea r(0);
    date.setMinutes (date.getMinute s()+date.getTim ezoneOffset());

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

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

    ....

    Daniel

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: 'point in time' time interval

      Andrew Poulos <ap_prog@hotmai l.com> writes:
      [color=blue]
      > 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)[/color]

      Representation not understood.
      [color=blue]
      > The format is:
      >
      > P[yY][mM][dD][T[hH][mM][s[.s]S]] where:[/color]
      [color=blue]
      > 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)[/color]

      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.
      [color=blue]
      > It's used by the SCORM RTE to measure the length of session times.[/color]

      No idea what SCORM RTE is either (ok, Google told me), but why not
      just count the seconds. What else is needed?
      [color=blue]
      > I've had a few goes but the code starts to look like spaghetti then I
      > have to give up.[/color]

      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.t oFixed(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().j oin("");
      }
      ---

      Good luck
      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Dr John Stockton

        #4
        Re: 'point in time' time interval

        JRS: In article <42aed83a$0$229 27$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_prog@hotmai l.com> posted :[color=blue]
        >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)[/color]

        What the latter might mean is unclear to me.

        [color=blue]
        >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.[/color]


        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.leng th-1)] = parseFloat(X[J]) }
        with (Obj) ISO = ((((Y*100+M)*10 0+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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        • Andrew Poulos

          #5
          Re: 'point in time' time interval

          Dr John Stockton wrote:
          [color=blue]
          > JRS: In article <42aed83a$0$229 27$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_prog@hotmai l.com> posted :
          >[color=green]
          >>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)[/color]
          >
          >
          > What the latter might mean is unclear to me.
          >
          >
          >[color=green]
          >>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.[/color]
          >
          >
          >
          > 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.leng th-1)] = parseFloat(X[J]) }
          > with (Obj) ISO = ((((Y*100+M)*10 0+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.
          >[/color]
          The representation I first mentioned should've been "timeinterv al
          (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

          Comment

          Working...