Thursday, 8 August 2013

How to convert SCORM 2004 session_time format to a usable datetime format in SQL

How to convert SCORM 2004 session_time format to a usable datetime format
in SQL

So SCORM 2004's session_time format looks like the following
P[yY][mM][dD][T[hH][mM][s[.s]S]]
Where the options in the square brackets are optional, however it must
include at least 1 of the options.
An example of the format is PT0H0M47S
Which means 0 hours, 0 minutes and 47 seconds.
The value is stored as varchar in a MSSQL database.
I need to be able to convert this string format into a usable format so
that I can add this time onto a Start Date Time to work out the End Date
Time.
I am currently moving the SCORM data from an old database to a new 1. I
could do this in PHP and format the time easily but I would prefer to
write all of the import scripts in SQL.
Any help on this would be greatly appreciated.
EDIT:
If it helps, this is a PHP function I wrote to handle the format for
another purpose
private function formatDuration($duration)
{
$count =
preg_match('/P(([0-9]+)Y)?(([0-9]+)M)?(([0-9]+)D)?T?(([0-9]+)H)?(([0-9]+)M)?(([0-9]+)(\.[0-9]+)?S)?/',
$duration, $matches);
if ($count)
{
$_years = (int) $matches[2];
$_months = (int) $matches[4];
$_days = (int) $matches[6];
$_hours = (int) $matches[8];
$_minutes = (int) $matches[10];
$_seconds = (int) $matches[12];
}
else
{
if (strstr($duration, ':'))
{
list($_hours, $_minutes, $_seconds) = explode(':', $duration);
}
else
{
$_hours = 0;
$_minutes = 0;
$_seconds = 0;
}
}
// I just ignore years, months and days as it is unlikely that a
// course would take any longer than 1 hour
return $_seconds + (($_minutes + ($_hours * 60)) * 60);
}

No comments:

Post a Comment