Skip to content

Instantly share code, notes, and snippets.

@sharkpp
Last active May 28, 2016 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharkpp/2bdf0b6a70fc08044b01b7089e6ef023 to your computer and use it in GitHub Desktop.
Save sharkpp/2bdf0b6a70fc08044b01b7089e6ef023 to your computer and use it in GitHub Desktop.
<?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