http://www.sharkpp.net/blog/2016/05/28/compare-format-for-date-to-string-php-and-ruby.html 用のスクリプト(メイン)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// The MIT License (MIT) | |
// Copyright (c) 2016 sharkpp. | |
// See: https://opensource.org/licenses/mit-license.php | |
// usage: _format-diff.php | |
// | |
// 実行するスクリプトの仕様 | |
// 1. 1番目の引数に UNIX 時刻を指定された場合にその値を利用し書式化すること | |
// 2. 出力フォーマットは "フォーマット@@値@~" を1単位とする | |
// 3. それぞのたんの出力ごとに改行文字は付与しないこと | |
function array_search_callback($cb, array $haystack) | |
{ | |
foreach ($haystack as $key => $value) | |
{ | |
if ($cb($value)) { | |
return $key; | |
} | |
} | |
return FALSE; | |
} | |
ini_set('date.timezone', 'Asia/Tokyo'); | |
$tbls = []; | |
// テストするコマンドの一覧 | |
$cmds = [ | |
'php php-strftime.php', | |
'php php-date.php', | |
'ruby ruby-Time#strftime.rb', | |
]; | |
for ($timestamp = mktime(23, 45, 57,12,30, 2012), | |
$timestamp_last = mktime(23, 45, 57, 2, 8, 2013); | |
//$timestamp_last = mktime(23, 45, 57, 1, 2, 2013); | |
$timestamp <= $timestamp_last; $timestamp += 1234) | |
{ | |
foreach ($cmds as $cmd) { | |
$tblkey = array_search_callback(function ($v) use ($cmd) { | |
return $v['cmd'] === $cmd; | |
}, $tbls); | |
$fmts = false === $tblkey ? [] : $tbls[$tblkey]['fmts']; | |
// コマンドの結果を取得 | |
ob_start(); | |
passthru($cmd . ' ' . $timestamp); | |
$tmp = ob_get_clean(); | |
ob_end_clean(); | |
// 行を分解 ※改行文字はない | |
// | : | |
// |フォーマット@@値@~ | |
// | : | |
foreach (explode("@~", $tmp) as $line) { | |
if (empty($line)) | |
continue; | |
$tmp2 = [ 'fmt' => '', 'value' => '', 'used' => 0 ]; | |
list($tmp2['fmt'], $tmp2['value']) = explode("@@", $line); | |
$key = array_search_callback(function ($v) use ($tmp2) { | |
return $v['fmt'] === $tmp2['fmt']; | |
}, $fmts); | |
if (false !== $key) { | |
$fmts[$key]['value'] .= "\x7F" . $tmp2['value']; | |
} | |
else { | |
$fmts[] = $tmp2; | |
} | |
} | |
if (false !== $tblkey) { | |
$tbls[$tblkey]['fmts'] = $fmts; | |
} | |
else { | |
$tbls[] = [ 'cmd' => $cmd, 'fmts' => $fmts ]; | |
} | |
} | |
} | |
//print_r($tbls); | |
$results = []; | |
$diff_init = array_combine($cmds, array_fill(0, count($cmds), [])); | |
$diff = []; | |
foreach ($tbls as $i => $tbl) | |
{ | |
foreach ($tbl['fmts'] as $j => $fmt) | |
{ | |
if (!isset($diff[ $fmt['value'] ])) { | |
$diff[ $fmt['value'] ] = $diff_init; | |
} | |
$diff[ $fmt['value'] ][ $tbl['cmd'] ][] = $fmt['fmt']; | |
} | |
} | |
foreach ($diff as $diff_key => $diff_value) | |
{ | |
$tmp = []; | |
foreach ($diff_value as $cmd => $value) { | |
$tmp[$cmd] = implode(', ', $value); | |
} | |
$results[] = $tmp; | |
} | |
// カラムの幅を計算 | |
$width = array_combine($cmds, array_fill(0, count($cmds), 5)); | |
for ($i = 0; $i < count($results); $i++) { | |
foreach ($cmds as $cmd) { | |
$width[$cmd] = max($width[$cmd], strlen($results[$i][$cmd])); | |
} | |
} | |
//print_r($results); | |
// 結果を Markdown の表形式で出力 | |
// ヘッダを出力 | |
echo '|'; | |
foreach ($cmds as $cmd) { | |
echo preg_replace('/.+? ([^-]+)-([^.]+)\..+/', '$1:$2', $cmd) . '|'; | |
} | |
echo '説明|'.PHP_EOL; | |
echo '|'.implode('|', array_fill(0, count($cmds), ':-:')).'|-|'.PHP_EOL; | |
// 結果を出力 | |
for ($i = 0; $i < count($results); $i++) { | |
echo '|'; | |
foreach ($cmds as $cmd) | |
{ | |
echo str_pad($results[$i][$cmd], $width[$cmd], ' ', STR_PAD_BOTH) . '|'; | |
} | |
echo '|'; | |
echo PHP_EOL; | |
} | |
echo PHP_EOL; | |
//echo $timestamp. PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment