Time   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 123
rs 10
c 0
b 0
f 0
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A countleaps() 0 20 6
A getleaps() 0 22 1
A gps2unix() 0 11 2
A isleap() 0 12 3
A unix2gps() 0 14 2
1
<?php
2
3
/**
4
 * Time.php
5
 * DQSEGDB - Convert segdb-format data to DQSEGDB.
6
 *
7
 * @link https://github.com/ligovirgo/dqsegdb/blob/master/server/db/db_utils/segdb_to_dqsegdb_auto_converter/src/classes/TimeConversion.php Source
8
 * @link https://www.andrews.edu/~tzs/timeconv/timealgorithm.html Algorithm
9
 */
10
11
namespace Bmatovu\Conversion;
12
13
/**
14
 * Class Time.
15
 */
16
class Time
17
{
18
    /**
19
     * Define GPS leap seconds.
20
     *
21
     * @return array
22
     */
23
    private static function getleaps()
24
    {
25
        $leaps = [
26
            46828800,
27
            78364801,
28
            109900802,
29
            173059203,
30
            252028804,
31
            315187205,
32
            346723206,
33
            393984007,
34
            425520008,
35
            457056009,
36
            504489610,
37
            551750411,
38
            599184012,
39
            820108813,
40
            914803214,
41
            1025136015,
42
        ];
43
44
        return $leaps;
45
    }
46
47
    /**
48
     * If a GPS second is a leap second.
49
     *
50
     * @param int $gpsTime GPS Time
51
     *
52
     * @return bool
53
     */
54
    private static function isleap($gpsTime)
55
    {
56
        $isLeap = false;
57
        $leaps = self::getleaps();
58
        $lenLeaps = count($leaps);
59
        for ($i = 0; $i < $lenLeaps; $i++) {
60
            if ($gpsTime == $leaps[$i]) {
61
                $isLeap = true;
62
            }
63
        }
64
65
        return $isLeap;
66
    }
67
68
    /**
69
     * Count number of leap seconds that have passed.
70
     *
71
     * @param int    $gpsTime GPS Time
72
     * @param string $dirFlag ??
73
     *
74
     * @return int
75
     */
76
    private static function countleaps($gpsTime, $dirFlag)
77
    {
78
        $leaps = self::getleaps();
79
        $lenLeaps = count($leaps);
80
        $nleaps = 0; // number of leap seconds prior to gpsTime
81
        for ($i = 0; $i < $lenLeaps; $i++) {
82
            if (!strcmp('unix2gps', $dirFlag)) {
83
                if ($gpsTime >= $leaps[$i] - $i) {
84
                    $nleaps++;
85
                }
86
            } elseif (!strcmp('gps2unix', $dirFlag)) {
87
                if ($gpsTime >= $leaps[$i]) {
88
                    $nleaps++;
89
                }
90
            } else {
91
                echo 'ERROR Invalid Flag!';
92
            }
93
        }
94
95
        return $nleaps;
96
    }
97
98
    /**
99
     * Convert GPS Time to Unix Time.
100
     *
101
     * @param int $gpsTime GPS Time
102
     *
103
     * @return mixed
104
     */
105
    public static function gps2unix($gpsTime)
106
    {
107
        // Add offset in seconds
108
        $unixTime = $gpsTime + 315964800;
109
        $nleaps = self::countleaps($gpsTime, 'gps2unix');
110
        $unixTime = $unixTime - $nleaps;
111
        if (self::isleap($gpsTime)) {
112
            $unixTime = $unixTime + 0.5;
113
        }
114
115
        return $unixTime;
116
    }
117
118
    /**
119
     * Convert Unix Time to GPS Time.
120
     *
121
     * @param int $unixTime Unix Time
122
     *
123
     * @return mixed
124
     */
125
    public static function unix2gps($unixTime)
126
    {
127
        // Add offset in seconds
128
        if (fmod($unixTime, 1) != 0) {
129
            $unixTime = $unixTime - 0.5;
130
            $isLeap = 1;
131
        } else {
132
            $isLeap = 0;
133
        }
134
        $gpsTime = $unixTime - 315964800;
135
        $nleaps = self::countleaps($gpsTime, 'unix2gps');
0 ignored issues
show
Bug introduced by
$gpsTime of type double is incompatible with the type integer expected by parameter $gpsTime of Bmatovu\Conversion\Time::countleaps(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
        $nleaps = self::countleaps(/** @scrutinizer ignore-type */ $gpsTime, 'unix2gps');
Loading history...
136
        $gpsTime = $gpsTime + $nleaps + $isLeap;
137
138
        return $gpsTime;
139
    }
140
}
141