OhG*_*Why 5 mysql sql datetime iso8601
我有一个SQL列的值像
PT2M52.37S
PT21.79S
PT53M29.68S
P
PT9S
Run Code Online (Sandbox Code Playgroud)
某些MySQL函数可以将上述格式转换为秒吗?
我已经搜索了所有内容,但没有找到与上述格式完全相同的格式。我尝试的任何日期mysql函数均无法正常工作。
有关此格式的更多信息:http : //www.ostyn.com/standards/scorm/samples/ISOTimeForSCORM.htm
PHP函数(始终为P,第一个字符)
function scorm_format_duration($duration) {
// Fetch date/time strings.
$stryears = get_string('years');
$strmonths = get_string('nummonths');
$strdays = get_string('days');
$strhours = get_string('hours');
$strminutes = get_string('minutes');
$strseconds = get_string('seconds');
if ($duration[0] == 'P') {
// If timestamp starts with 'P' - it's a SCORM 2004 format
// this regexp discards empty sections, takes Month/Minute ambiguity into consideration,
// and outputs filled sections, discarding leading zeroes and any format literals
// also saves the only zero before seconds decimals (if there are any) and discards decimals if they are zero.
$pattern = array( '#([A-Z])0+Y#', '#([A-Z])0+M#', '#([A-Z])0+D#', '#P(|\d+Y)0*(\d+)M#',
'#0*(\d+)Y#', '#0*(\d+)D#', '#P#', '#([A-Z])0+H#', '#([A-Z])[0.]+S#',
'#\.0+S#', '#T(|\d+H)0*(\d+)M#', '#0*(\d+)H#', '#0+\.(\d+)S#',
'#0*([\d.]+)S#', '#T#' );
$replace = array( '$1', '$1', '$1', '$1$2 '.$strmonths.' ', '$1 '.$stryears.' ', '$1 '.$strdays.' ',
'', '$1', '$1', 'S', '$1$2 '.$strminutes.' ', '$1 '.$strhours.' ',
'0.$1 '.$strseconds, '$1 '.$strseconds, '');
} else {
// Else we have SCORM 1.2 format there
// first convert the timestamp to some SCORM 2004-like format for conveniency.
$duration = preg_replace('#^(\d+):(\d+):([\d.]+)$#', 'T$1H$2M$3S', $duration);
// Then convert in the same way as SCORM 2004.
$pattern = array( '#T0+H#', '#([A-Z])0+M#', '#([A-Z])[0.]+S#', '#\.0+S#', '#0*(\d+)H#',
'#0*(\d+)M#', '#0+\.(\d+)S#', '#0*([\d.]+)S#', '#T#' );
$replace = array( 'T', '$1', '$1', 'S', '$1 '.$strhours.' ', '$1 '.$strminutes.' ',
'0.$1 '.$strseconds, '$1 '.$strseconds, '' );
}
$result = preg_replace($pattern, $replace, $duration);
return $result;
}
Run Code Online (Sandbox Code Playgroud)
我认为没有一个标准函数可以做到这一点。使用str_to_date()是一种可能性,但它会因缺少时间组件而失败。如果您只有分和秒组件,则使用字符串操作可能会很痛苦。
select ((case when p like '%M%S'
then substring_index(substring_index(p, 'M', 2), 'M', -1)
when p like '%T%S'
then substring_index(substring_index(p, 'T', 2), 'T', -1)
else ''
end) +
(case when p like 'T%M'
then substring_index(substring_index(p, 'T', 2), 'T', -1) + 0
else 0
end) * 60
) as seconds
Run Code Online (Sandbox Code Playgroud)
随着更多时间组件的添加,这并不容易推广。例如,当分隔符为“M”或“T”时,秒组件是第二个元素。有时,您只需想知道 ISO 委员会在制定这些标准时的想法。
请注意,MySQL 使用前导数字(如果有)将字符串转换为数字。上面就用到了这个特性。
如果您支持更复杂的周期格式,那么我建议编写一个用户定义的函数并将其发布在这里作为答案。
我还要指出 SAS 支持这种格式。更好的是,他们有非常好的文档来解释它。
编辑:
通过这个组合,您可以更轻松地处理所有格式。它在每个组件后面插入一个空格,然后使用substring_index():
select (case when newp like '%S'
then substring_index(substring_index(newp, 'S', 1), ' ', -1) + 1
else 0
end) as seconds,
(case when newp like '%M%'
then substring_index(substring_index(newp, 'M', 1), ' ', -1) + 1
else 0
end) as minutes,
. . .
from (select replace(replace(replace(replace(replace(p, 'T', 'T '
), 'Y', 'Y '
), 'D', 'D '
), 'H', 'H '
), 'S', 'S '
) as newp
from t
) t
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
827 次 |
| 最近记录: |