-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetHist.php
74 lines (68 loc) · 1.67 KB
/
getHist.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
<?php
// Read any stored historical data into correct format
$fName = getenv("DATA_LOCATION") . "/$zip.txt";
if (!file_exists($fName)) {
$zipReg = fopen(getenv("DATA_LOCATION") . "/zips.txt", "a+");
fwrite($zipReg, "$zip\n");
fclose($zipReg);
touch($fName);
}
$f = fopen($fName, "r");
rewind($f);
$raw = fread($f, filesize($fName));
fclose($f);
$raw = explode("\n", $raw);
$pData = Array();
foreach ($raw as $line) { // Formating
if ($line) {
$line = explode(",", $line);
$line[0] = new DateTime($line[0]);
$line[1] = (float) $line[1];
$line[2] = (float) $line[2];
$line[3] = (float) $line[3];
$line[4] = (float) $line[4];
$pData[] = $line;
}
}
// Check for today's historical data
$tDataGot = False;
$today = new DateTime(date('Y-m-d'));
if ($pData[count($pData) - 1][0] == $today) { // Only the latest data
$pData = array_slice($pData, -5, 4);
$tDataGot = True;
} else {
$pData = array_slice($pData, -4);
}
// var_dump($tDataGot);
// var_dump($pData);
// Make sure the data isn't missing anything
$missingData = False;
for ($i = 3; $i >= 0; $i--) {
if (! isset($pData[$i][0])) {
$missingData = True;
break;
}
$add = "+" . (string) (4 - $i) . " day";
$dD = $pData[$i][0];
$dD->modify($add);
if ($dD->format("Y-m-d") !== $today->format("Y-m-d")) {
$missingData = True;
break;
}
}
// Write new historical data to file
if (!$tDataGot) {
$f = fopen($fName, "a+");
$tData = Array();
$tData[] = (string) date('Y-m-d');
$tData[] = (string) $fData[0][0];
$tData[] = (string) $fData[1][0];
$tData[] = (string) $fData[2][0];
$tData[] = (string) $fData[3][0];
$tData = (string) implode(",", $tData);
fwrite($f, $tData);
fwrite($f, "\n");
fclose($f);
}
return $pData;
?>