HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux bsx-1-dev 6.8.0-101-generic #101-Ubuntu SMP PREEMPT_DYNAMIC Mon Feb 9 10:15:05 UTC 2026 x86_64
User: www-data (33)
PHP: 8.3.6
Disabled: NONE
Upload Files
File: /var/www/html/wp-content/plugins/bsx/ass/helper.php
<?php


function array_transpose($array, $selectKey = false) {
    if (!is_array($array)) return false;
    $return = array();
    foreach($array as $key => $value) {
        if (!is_array($value)) return $array;
        if ($selectKey) {
            if (isset($value[$selectKey])) $return[] = $value[$selectKey];
        } else {
            foreach ($value as $key2 => $value2) {
                $return[$key2][$key] = $value2;
            }
        }
    }
    return $return;
} 



// ==========================================================================================
function gmt2local($x){
    $dts = ["pdt","edt","cdt","xdt","dt"];
    foreach($dts as $t) {
        if(isset($x->$t)) $x->$t = get_date_from_gmt($x->$t);
    }
    return $x;
}

function local2gmt($x){
    $dts = ["pdt","edt","cdt","xdt","dt"];
    foreach($dts as $t) {
        if(isset($x->$t)) $x->$t = get_gmt_from_date($x->$t);
    }
    return $x;
}


// ==========================================================================================
function wdays($start, $end) {
    $period = new DatePeriod(
        new DateTime($start),
        new DateInterval('P1D'),
        (new DateTime($end))->modify('+1 day')
    );

    $days = [];

    foreach ($period as $date) {
        $weekday = $date->format('N');   // 1 = Monday, 7 = Sunday
        if ($weekday < 6) {              // Monday–Friday
            $days[] = $date->format('Y-m-d');
        }
    }
    return $days;
}


// ==========================================================================================
function wday_prev($tstamp) {
    while(date('N',$tstamp)>5) $tstamp =wday_prev($tstamp-24*3600);
    return time2day($tstamp);
}


// ==========================================================================================
function time2day($t) {
    return floor($t / 3600 / 24) * 24 * 3600;
}


// ==========================================================================================
function time2sec($s) {
    $hms = explode(':',$s);
    $sec = 0;
    $m   = [3600,60,1];
    for($i=0;$i<count($hms);$i++) $sec = $sec + floatval($hms[$i]) * $m[$i];
    return $sec;
}


// ==========================================================================================

    
function tdelta($th,$cdt) {
    $th0    = $th[0]; $th1 = $th[1];
    $cdt    = strtotime($cdt);
    $tday   = wday_prev(time());
    $yday   = wday_prev($tday-3600*24);

    $tclose = $tday + time2sec($th1);
    $topen  = $tday + time2sec($th0);
    $yclose = $yday + time2sec($th1);
             
    if(time() > $tclose) {
        return $tclose - $cdt;
    } else if(time() > $topen) {
        return time() - $cdt;
    } else {
        return $yclose - $cdt;
    }
    
}





// --------------------------------------------------------------------------------
// financial



// --- LOCF mapping function ---
function map_to_master_locf($master_dates, $series) {
    $series_assoc = [];
    foreach ($series as $s) $series_assoc[$s['x']] = $s['y'];
    $md = [];
    foreach($master_dates as $d) $md[] = strtotime($d);
    $mapped = [];
    $last_val = null;
    foreach ($md as $d) {
        if (isset($series_assoc[$d])) {
            $last_val = $series_assoc[$d];
            $mapped[] = ['x'=>$d, 'y'=>$series_assoc[$d]];
        } else {
            $mapped[] = ['x'=>$d, 'y'=>$last_val];
        }
    }
    return $mapped;
}





// ==========================================================================================
function bsx_return_series(array $prices): array {
    $returns = [0];
    $n = count($prices);
    for ($i = 1; $i < $n; $i++) $returns[] = ($prices[$i]-$prices[$i-1])/$prices[$i-1];
    return $returns;
}




// --------------------------------------------------------------------------------
//minor data formatters or accessors


function bsx_trade_period($id) {
    $start = get_field('entry_date',$id);
    $end   = get_field('close_date',$id);
    $end   = $end ? strtotime($end) : time();
    $start = floor(strtotime($start)/24/3600)*3600*24;
    return [(int)$start,$end];
}




// ==========================================================================================
function bsx_isdt($dt = null) {
    return !empty($dt) && $dt != '0000-00-00 00:00:00';
}


        
// ==========================================================================================
function bsx_to_price($p,$c="USD") {
    $r = sprintf("%8.3f %s",floatval($p),$c);
    return $r;
}



// ==========================================================================================
function bsx_format_mcap($mcap) {
    $mcap = floatval($mcap);
    switch(true) {
    case $mcap / 1e6 > 1:
        $mc = sprintf("% 7.3ftr", $mcap / 1e6);
        break;
    case $mcap / 1e3 > 1:
        $mc = sprintf("% 7.3fbn", $mcap / 1e3);
        break;
    default:
        $mc = sprintf("% 7.3fm", $mcap );
        break;
    }
    return $mc;
}