-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.util.php
115 lines (83 loc) · 2.19 KB
/
functions.util.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/* REST API class */
class APIREST {
private $url;
public function __construct($url) {
$this->url = $url;
}
/**
* @param $httpheader array of headers
* @return response
*/
public function call($httpheader, $method, $query = NULL) {
try {
$curl = curl_init();
if (FALSE === $curl)
throw new Exception('Failed to initialize');
$curl_opt = array(
CURLOPT_URL => $this->url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 60, /* number of seconds to wait for response */
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => $query,
CURLOPT_HTTPHEADER => $httpheader
);
curl_setopt_array($curl, $curl_opt);
$response = curl_exec($curl);
if (FALSE === $response)
throw new Exception(curl_error($curl), curl_errno($curl));
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if (200 != $http_status)
throw new Exception($response, $http_status);
curl_close($curl);
} catch(Exception $e) {
$response= $e->getCode() . $e->getMessage();
echo $response;
}
return $response;
}
}
/* get timestamp */
function milliseconds() {
list($msec, $sec) = explode(' ', microtime());
return (int) ($sec . substr($msec, 2, 3));
}
/* encrypt key */
function hmac($msg, $secret) {
$hmac = hash_hmac('sha256', $msg, $secret, true);
$hmac = base64_encode($hmac);
return $hmac;
}
/* array depth */
function countdim($array) {
if (is_array(reset($array))) {
$return = countdim(reset($array)) + 1;
} else {
$return = 1;
}
return $return;
}
/*
asset pair only reported on weekdays
expect some asset pairs are not recorded on weekends, for instance fiat currencies like $AUD to $USD
*/
function weekends($class) {
if ($class == 'fiat' || $class == 'stock') {
$weekend = FALSE;
} else {
$weekend = TRUE;
}
return $weekend;
}
/* count number of observations falling on weekends */
function count_on_weekends($from, $to, $period_ms) {
$wmiss = 0;
for ($j = $from; $j <= $to; $j += $period_ms) {
$day = date('l', $j / 1000);
if (!($day == 'Sunday' || $day == 'Monday')) $wmiss++;
}
return $wmiss;
}