Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SCORM 2004 Time Format - Regular Expression?

Tags:

regex

time

scorm

I am building a SCORM 2004 javascript API for an LMS, and one of the SCORM 2004 requirements is that timeintervals passed into it must follow the following format. Does anyone know what the regular expression of this would be? I am trying to wrap my mind around it, but to no avail. Note: P must always be the first character.

P[yY][mM][dD][T[hH][nM][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 and S shall appear if the corresponding non-zero value is present.
  • Zero-padding of the values shall be supported. Zero-padding does not change the integer value of the number being represented by a set of characters. For example, PT05H is equivalent to PT5H and PT000005H.

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

Any help would be greatly appreciated.

Thanks!

UPDATE:

I have added some additional standards that must be kept -

  • The designator P shall be present
  • If the value of years, months, days, hours, minutes or seconds is zero, the value and corresponding character literal designation may be omitted, but at least one character literal designator and value shall be present in addition to the designator P
  • The designator T shall be omitted if all of the time components (hours, minutes and seconds) are not used. A zero value may be used with any of the time components (e.g., PT0S)
like image 670
Dan Appleyard Avatar asked Aug 20 '09 16:08

Dan Appleyard


2 Answers

Here is the regex i use;

^P(?=\w*\d)(?:\d+Y|Y)?(?:\d+M|M)?(?:\d+D|D)?(?:T(?:\d+H|H)?(?:\d+M|M)?(?:\d+(?:\­.\d{1,2})?S|S)?)?$ 
like image 157
Shannow Avatar answered Dec 11 '22 18:12

Shannow


Use [0-9] to match any numeral. + to match 1 or more repetitions. ? to match 0 or 1 repetitions. () to group and extract the output.

P(([0-9]+Y)?([0-9]+M)?([0-9]+D)?)(T([0-9]+H)?([0-9]+M)?([0-9.]+S)?)?

import re

>>> p = re.compile('P(([0-9]+Y)?([0-9]+M)?([0-9]+D)?)(T([0-9]+H)?([0-9]+M)?([0-9.]+S)?)?')

>>> p.match('P1Y3M2DT3H').groups()
('1Y3M2D', '1Y', '3M', '2D', 'T3H', '3H', None, None)

>>> p.match('P3M2DT3H').groups()
('3M2D', None, '3M', '2D', 'T3H', '3H', None, None)

>>> p.match('PT3H5M').groups()
('', None, None, None, 'T3H5M', '3H', '5M', None)

>>> p.match('P1Y3M4D').groups()
('1Y3M4D', '1Y', '3M', '4D', None, None, None, None)
like image 31
Joe Koberg Avatar answered Dec 11 '22 16:12

Joe Koberg