<?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 scToNum($scStr)
{
    $check_str = '';
    if (strpos($scStr, 'e') !== false) {
        $check_str = 'e';
    }
    if (strpos($scStr, 'E') !== false) {
        $check_str = 'E';
    }
    if (empty($check_str)) {
        return $scStr; //非科学计数直接返回。
    }
    $num_length = 0;
    $split_array = explode($check_str, $scStr);
    $num_length += strlen($split_array[0]);
    $num_length += (int)str_replace(['-', '+'], '', $split_array[1]);
    $float_number = (float)$scStr;
    $decimal_string = number_format($float_number, $num_length, '.', '');
    $num =  rtrim(rtrim($decimal_string, '0'), '.');   //去除小数后多余的0
    return $num;
}
//时间戳转ISO8601
function tsToISO($timestamp)
{
    $datetime = new \DateTime();
    $datetime->setTimestamp(floor($timestamp / 1000));
    $datetime->setTimezone(new \DateTimeZone('UTC'));
    $milliseconds = sprintf('.%03d', $timestamp % 1000);
    return $datetime->format('Y-m-d\TH:i:s') . $milliseconds . 'Z';
}


//以守护进程运行
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();
    }
}