Completed
Push — master ( d35909...f740b5 )
by Yannick
06:46
created

Predict_Time::unix2daynum()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Functions from sgp_time.c and time-tools.c (except where noted)
5
 * ported to PHP by Bill Shupp
6
 */
7
8
//require_once 'Predict.php';
9
require_once 'Math.php';
10
11
/*
12
 * Unit SGP_Time
13
 *       Author:  Dr TS Kelso
14
 * Original Version:  1992 Jun 02
15
 * Current Revision:  2000 Jan 22
16
 * Modified for Y2K:  1999 Mar 07
17
 *          Version:  2.05
18
 *        Copyright:  1992-1999, All Rights Reserved
19
 * Version 1.50 added Y2K support. Due to limitations in the current
20
 * format of the NORAD two-line element sets, however, only dates
21
 * through 2056 December 31/2359 UTC are valid.
22
 * Version 1.60 modifies Calendar_Date to ensure date matches time
23
 * resolution and modifies Time_of_Day to make it more robust.
24
 * Version 2.00 adds Julian_Date, Date_Time, and Check_Date to support
25
 * checking for valid date/times, permitting the use of Time_to_UTC and
26
 * Time_from_UTC for UTC/local time conversions.
27
 * Version 2.05 modifies UTC_offset to allow non-integer offsets.
28
 *
29
 *   Ported to C by: Neoklis Kyriazis  April 9  2001
30
 */
31
class Predict_Time
32
{
33
    /* The function Julian_Date_of_Epoch returns the Julian Date of     */
34
    /* an epoch specified in the format used in the NORAD two-line      */
35
    /* element sets. It has been modified to support dates beyond       */
36
    /* the year 1999 assuming that two-digit years in the range 00-56   */
37
    /* correspond to 2000-2056. Until the two-line element set format   */
38
    /* is changed, it is only valid for dates through 2056 December 31. */
39
    public static function Julian_Date_of_Epoch($epoch)
40
    {
41
        $year = 0;
42
43
        /* Modification to support Y2K */
44
        /* Valid 1957 through 2056     */
45
        $day = self::modf($epoch * 1E-3, $year) * 1E3;
46
        if ($year < 57) {
47
            $year = $year + 2000;
48
        } else {
49
            $year = $year + 1900;
50
        }
51
        /* End modification */
52
53
        return self::Julian_Date_of_Year($year) + $day;
54
    }
55
56
    /* Equivalent to the C modf function */
57
    public static function modf($x, &$ipart) {
58
        $ipart = (int)$x;
59
        return $x - $ipart;
60
    }
61
62
    /* The function Julian_Date_of_Year calculates the Julian Date  */
63
    /* of Day 0.0 of {year}. This function is used to calculate the */
64
    /* Julian Date of any date by using Julian_Date_of_Year, DOY,   */
65
    /* and Fraction_of_Day. */
66
    public static function Julian_Date_of_Year($year)
67
    {
68
        /* Astronomical Formulae for Calculators, Jean Meeus, */
69
        /* pages 23-25. Calculate Julian Date of 0.0 Jan year */
70
        $year = $year - 1;
71
        $i = (int) ($year / 100);
72
        $A = $i;
73
        $i = (int) ($A / 4);
74
        $B = (int) (2 - $A + $i);
75
        $i = (int) (365.25 * $year);
76
        $i += (int) (30.6001 * 14);
77
        $jdoy = $i + 1720994.5 + $B;
78
79
        return $jdoy;
80
    }
81
82
    /* The function ThetaG calculates the Greenwich Mean Sidereal Time */
83
    /* for an epoch specified in the format used in the NORAD two-line */
84
    /* element sets. It has now been adapted for dates beyond the year */
85
    /* 1999, as described above. The function ThetaG_JD provides the   */
86
    /* same calculation except that it is based on an input in the     */
87
    /* form of a Julian Date. */
88
    public static function ThetaG($epoch, Predict_DeepArg $deep_arg)
89
    {
90
        /* Reference:  The 1992 Astronomical Almanac, page B6. */
91
        // double year,day,UT,jd,TU,GMST,_ThetaG;
92
93
        /* Modification to support Y2K */
94
        /* Valid 1957 through 2056     */
95
        $year = 0;
96
        $day = self::modf($epoch * 1E-3, $year) * 1E3;
97
98
        if ($year < 57) {
99
            $year += 2000;
100
        } else {
101
            $year += 1900;
102
        }
103
        /* End modification */
104
105
        $UT = fmod($day, $day);
106
        $jd = self::Julian_Date_of_Year($year) + $day;
107
        $TU = ($jd - 2451545.0) / 36525;
108
        $GMST = 24110.54841 + $TU * (8640184.812866 + $TU * (0.093104 - $TU * 6.2E-6));
109
        $GMST = Predict_Math::Modulus($GMST + Predict::secday * Predict::omega_E * $UT, Predict::secday);
0 ignored issues
show
Unused Code introduced by
$GMST is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
110
        $deep_arg->ds50 = $jd - 2433281.5 + $UT;
111
112
        return Predict_Math::FMod2p(6.3003880987 * $deep_arg->ds50 + 1.72944494);
113
    }
114
115
    /* See the ThetaG doc block above */
116
    public static function ThetaG_JD($jd)
117
    {
118
        /* Reference:  The 1992 Astronomical Almanac, page B6. */
119
        $UT   = Predict_Math::Frac($jd + 0.5);
120
        $jd   = $jd - $UT;
121
        $TU   = ($jd - 2451545.0) / 36525;
122
        $GMST = 24110.54841 + $TU * (8640184.812866 + $TU * (0.093104 - $TU * 6.2E-6));
123
        $GMST = Predict_Math::Modulus($GMST + Predict::secday * Predict::omega_E * $UT, Predict::secday);
124
125
        return Predict::twopi * $GMST / Predict::secday;
126
    }
127
128
    /**
129
     * Read the system clock and return the current Julian day.  From phpPredict
130
     *
131
     * @return float
132
     */
133
    public static function get_current_daynum() {
134
        // Gets the current decimal day number from microtime
135
136
        list($usec, $sec) = explode(' ', microtime());
137
        return self::unix2daynum($sec, $usec);
138
    }
139
140
    /**
141
     * Converts a standard unix timestamp and optional
142
     * milliseconds to a daynum
143
     *
144
     * @param int $sec  Seconds from the unix epoch
145
     * @param int $usec Optional milliseconds
146
     *
147
     * @return float
148
     */
149
    public static function unix2daynum($sec, $usec = 0)
150
    {
151
        $time = ((($sec + $usec) / 86400.0) - 3651.0);
152
        return $time + 2444238.5;
153
    }
154
155
    /* The function Delta_ET has been added to allow calculations on   */
156
    /* the position of the sun.  It provides the difference between UT */
157
    /* (approximately the same as UTC) and ET (now referred to as TDT).*/
158
    /* This function is based on a least squares fit of data from 1950 */
159
    /* to 1991 and will need to be updated periodically. */
160
    public static function Delta_ET($year)
161
    {
162
      /* Values determined using data from 1950-1991 in the 1990
163
         Astronomical Almanac.  See DELTA_ET.WQ1 for details. */
164
165
      $delta_et = 26.465 + 0.747622 * ($year - 1950) +
166
                 1.886913 * sin(Predict::twopi * ($year - 1975) / 33);
167
168
      return $delta_et;
169
    }
170
171
    /**
172
     * Converts a daynum to a unix timestamp.  From phpPredict.
173
     *
174
     * @param float $dn Julian Daynum
175
     *
176
     * @return float
177
     */
178
    public static function daynum2unix($dn) {
179
        // Converts a daynum to a UNIX timestamp
180
181
        return (86400.0 * ($dn - 2444238.5 + 3651.0));
182
    }
183
184
    /**
185
     * Converts a daynum to a readable time format.
186
     *
187
     * @param float $dn The julian date
188
     * @param string $zone The zone string, defaults to America/Los_Angeles
189
     * @param string $format The date() function's format string.  Defaults to m-d-Y H:i:s
190
     *
191
     * @return string
192
     */
193
    public static function daynum2readable($dn, $zone = 'America/Los_Angeles', $format = 'm-d-Y H:i:s')
194
    {
195
        $unix = self::daynum2unix($dn);
196
        $date = new DateTime("@" . round($unix));
197
        $dateTimezone = new DateTimezone($zone);
198
        $date->setTimezone($dateTimezone);
199
        return $date->format($format);
200
    }
201
202
    /**
203
     * Returns the unix timestamp of a TLE's epoch
204
     *
205
     * @param Predict_TLE $tle The TLE object
206
     *
207
     * @return int
208
     */
209
    public static function getEpochTimeStamp(Predict_TLE $tle)
210
    {
211
        $year = $tle->epoch_year;
212
        $day  = $tle->epoch_day;
213
        $sec  = round(86400 * $tle->epoch_fod);
214
215
        $zone = new DateTimeZone('GMT');
216
        $date = new DateTime();
217
        $date->setTimezone($zone);
218
        $date->setDate($year, 1, 1);
219
        $date->setTime(0, 0, 0);
220
221
        return $date->format('U') + (86400 * $day) + $sec - 86400;
222
    }
223
}
224