File: Sunrise.php

package info (click to toggle)
php-horde 5.2.13%2Bdebian0-1%2Bdeb9u2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 12,652 kB
  • sloc: php: 11,153; xml: 6,751; javascript: 5,560; sh: 92; makefile: 33; sql: 1
file content (178 lines) | stat: -rw-r--r-- 5,136 bytes parent folder | download | duplicates (6)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
 * @package Horde
 */
class Horde_Block_Sunrise extends Horde_Core_Block
{
    /**
     */
    public function __construct($app, $params = array())
    {
        parent::__construct($app, $params);

        $this->_name = _("Sunrise/Sunset");
    }

    /**
     */
    protected function _params()
    {
        $params = array(
            'location' => array(
                'type' => 'mlenum',
                'name' => _("Location"),
                'default' => '51.517:-0.117'
            )
        );

        global $coordinates;
        if (!is_array($coordinates)) {
            include 'Horde/Nls/Coordinates.php';
            if (!is_array($coordinates)) {
                $coordinates = array();
            }
        }
        $params['location']['values'] = $coordinates;

        return $params;
    }

    /**
     */
    protected function _content()
    {
        if (empty($this->_params['location'])) {
            throw new Horde_Exception(_("No location is set."));
        }

        // Set the timezone variable, if available.
        $GLOBALS['registry']->setTimeZone();

        list($lat, $long) = explode(':', $this->_params['location']);
        $rise = $this->_calculateSunset(time(), $lat, $long, false, floor(date('Z') / 3600));
        $set = $this->_calculateSunset(time(), $lat, $long, true, floor(date('Z') / 3600));

        $location = '';
        global $coordinates;
        if (!is_array($coordinates)) {
            require 'Horde/Nls/Coordinates.php';
        }
        foreach ($coordinates as $country) {
            if (array_key_exists($this->_params['location'], $country)) {
                $location = $country[$this->_params['location']];
                break;
            }
        }

        return '<table width="100%" height="100%" cellspacing="0"><tr>' .
            '<td colspan="2" class="control"><strong>' . $location . '</strong></td></tr><tr height="100%">' .
            '<td width="50%" align="center">' .
            Horde_Themes_Image::tag('block/sunrise/sunrise.png', array('alt' => _("Sun Rise"))) .
            '<br />' . $rise . '</td>' .
            '<td width="50%" align="center">' .
            Horde_Themes_Image::tag('block/sunrise/sunset.png', array('alt' => _("Sun Set"))) .
            '<br />' . $set . '</td>' . '</tr></table>';
    }

    /**
     * http://www.zend.com/codex.php?id=135&single=1
     */
    private function _calculateSunset($date, $latitude, $longitude, $sunset = true, $timezone)
    {
        $yday = date('z', $date);
        $mon = date('n', $date);
        $mday = date('j', $date);
        $year = date('Y', $date);

        if ($timezone == '13') {
            $timezone = '-11';
            $mday++;
            $yday++;
        }

        $A = 1.5708;
        $B = 3.14159;
        $C = 4.71239;
        $D = 6.28319;
        $E = 0.0174533 * $latitude;
        $F = 0.0174533 * $longitude;
        $G = 0.261799  * $timezone;

        // For astronomical twilight, use R = -.309017
        // For nautical twilight, use R = -.207912
        // For civil twilight, use R = -.104528
        // For sunrise or sunset, use R = -.0145439
        $R = -.0145439;

        if ($sunset) {
            $J = $C;
        } else {
            $J = $A;
        }

        $K = $yday + (($J - $F) / $D);
        $L = ($K * .017202) - .0574039;              // Solar Mean Anomoly
        $M = $L + .0334405 * sin($L);                // Solar True Longitude
        $M += 4.93289 + (3.49066E-04) * sin(2 * $L); // Quadrant Determination
        while ($M < 0) {
            $M = ($M + $D);
        }
        while ($M >= $D) {
            $M = ($M - $D);
        }

        if (($M / $A) - intval($M / $A) == 0) {
            $M += 4.84814E-06;
        }

        $P = sin($M) / cos($M);                   // Solar Right Ascension
        $P = atan2(.91746 * $P, 1);

        // Quadrant Adjustment
        if ($M > $C) {
            $P += $D;
        } elseif ($M > $A) {
            $P += $B;
        }

        $Q = .39782 * sin($M);            // Solar Declination
        $Q = $Q / sqrt(-$Q * $Q + 1);     // This is how the original author wrote it!
        $Q = atan2($Q, 1);

        $S = $R - (sin($Q) * sin($E));
        $S = $S / (cos($Q) * cos($E));

        if (abs($S) > 1) {
            return 'none';                // Null phenomenon
        }

        $S = $S / sqrt(-$S * $S + 1);
        $S = $A - atan2($S, 1);

        if (!$sunset) {
            $S = $D - $S ;
        }

        $T = $S + $P - 0.0172028 * $K - 1.73364; // Local apparent time
        $U = $T - $F;                            // Universal timer
        $V = $U + $G;                            // Wall clock time

        // Quadrant Determination
        while ($V < 0) {
            $V = ($V + $D);
        }
        while ($V >= $D) {
            $V = ($V - $D);
        }
        $V = $V * 3.81972;

        $hour = intval($V);
        $V   -= $hour;
        $min  = intval($V * 60);
        $V   -= $min / 60;
        $sec  = intval($V * 3600);

        return strftime('%X', mktime($hour, $min, $sec, $mon, $mday, $year));
    }

}