|
|
|
<?php
|
|
|
|
namespace Jiaoyin;
|
|
|
|
//格式化输出
|
|
|
|
use Swoole\Process;
|
|
|
|
use Swoole\Event;
|
|
|
|
|
|
|
|
function output(): void
|
|
|
|
{
|
|
|
|
$args = func_get_args();
|
|
|
|
$outStr = '['.timeFormat('ms').']:';
|
|
|
|
foreach($args as $key => $value){
|
|
|
|
$value = is_array($value) ? json_encode($value) : $value;
|
|
|
|
if(is_bool($value)){
|
|
|
|
$value = $value ? 'bool:true':'bool:false';
|
|
|
|
}
|
|
|
|
$outStr .= count($args) - $key > 1 ? $value.' ' : $value;
|
|
|
|
}
|
|
|
|
echo $outStr.PHP_EOL;
|
|
|
|
}
|
|
|
|
//格式化时间
|
|
|
|
function timeFormat($type='s',$format='Y-m-d H:i:s'): string
|
|
|
|
{
|
|
|
|
date_default_timezone_set('Asia/Shanghai');
|
|
|
|
$microTime = microtime();
|
|
|
|
list($msTime,$sTime) = explode(' ',$microTime);
|
|
|
|
$timeStr = date($format,$sTime);
|
|
|
|
if($type == 'ms'){
|
|
|
|
$timeStr .= '.'.sprintf("%03d",floor($msTime*1000));
|
|
|
|
}
|
|
|
|
return $timeStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取当前时间的微秒数
|
|
|
|
function getMicrotime(): float|int
|
|
|
|
{
|
|
|
|
list($uSec, $sec) = explode(' ', microtime());
|
|
|
|
return $sec*1000+round($uSec*1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
//获取精度
|
|
|
|
function getPrecision($number): int
|
|
|
|
{
|
|
|
|
$count = 0;
|
|
|
|
while($number < 1){
|
|
|
|
$number *= 10;
|
|
|
|
$count += 1;
|
|
|
|
}
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getIntervalUnit($interval,$type='s'): float|int
|
|
|
|
{
|
|
|
|
$unitTime = 0;
|
|
|
|
if($interval == '1m'){
|
|
|
|
$unitTime = 60;
|
|
|
|
}
|
|
|
|
if($interval == '5m'){
|
|
|
|
$unitTime = 60*5;
|
|
|
|
}
|
|
|
|
if($interval == '15m'){
|
|
|
|
$unitTime = 60*15;
|
|
|
|
}
|
|
|
|
if($interval == '1h'){
|
|
|
|
$unitTime = 60*60;
|
|
|
|
}
|
|
|
|
if($interval == '4h'){
|
|
|
|
$unitTime = 60*60*4;
|
|
|
|
}
|
|
|
|
if($interval == '1d'){
|
|
|
|
$unitTime = 60*60*24;
|
|
|
|
}
|
|
|
|
return $type == 's' ? $unitTime : $unitTime*1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
//以守护进程运行
|
|
|
|
function runAsDaemon($callback,$isDaemon=true): void
|
|
|
|
{
|
|
|
|
if($isDaemon){
|
|
|
|
Process::daemon();
|
|
|
|
$process = new Process(function ()use ($callback){
|
|
|
|
call_user_func($callback);
|
|
|
|
Event::wait();
|
|
|
|
});
|
|
|
|
$process->start();
|
|
|
|
}else{
|
|
|
|
call_user_func($callback);
|
|
|
|
Event::wait();
|
|
|
|
}
|
|
|
|
} |