[Сценарий] Восход и заход солнца

Не требует установки программ или изменения файлов

Модераторы: immortal, newz20

Ответить
Аватара пользователя
Amarok
Сообщения: 1425
Зарегистрирован: Пт дек 14, 2012 12:24 pm
Откуда: Россия, Нижняя Тура
Благодарил (а): 460 раз
Поблагодарили: 126 раз
Контактная информация:

[Сценарий] Восход и заход солнца

Сообщение Amarok » Пн дек 16, 2013 7:13 am

На одном из форумов я нашел скрипт вычисление восхода и захода солнца. После очень небольшой переделки прикрутил его к Алисе.

Код: Выделить всё

$lat=gg('ThisComputer.latitude');   // широта
$long=gg('ThisComputer.longitude'); // долгота
$utc=gg('ThisComputer.UTC');        // часовой пояс

class sun
{
    var $latitude;     #szerokosc geograficzna
    var $longitude;    #dlugosc geograficzna
    var $timezone;     #strefa czasowa
        function sun ($latitude, $longitude, $timezone)
            {
                $this->latitude = $latitude;
                $this->longitude = $longitude;
                $this->timezone = $timezone;
                $this->yday = date("z");
                $this->mon = date("n");
                $this->mday = date("j");
                $this->year = date("Y");
                #---------------------
                $this->DST=$this->is_daylight_time(date("U"));
                    if ($this->DST)
                       {
                          $this->timezone = ($this->timezone + 1);
                       }
                    if ($this->timezone == "13")
                       {
                          $this->timezone = "-11";
                       }
                #---------------------
                $this->A = 1.5708;
                $this->B = 3.14159;
                $this->C = 4.71239;
                $this->D = 6.28319;
                $this->E = 0.0174533 * $this->latitude;
                $this->F = 0.0174533 * $this->longitude;
                $this->G = 0.261799  * $this->timezone;
                #---------------------
                  # For astronomical twilight, use
                  #$this->R = -.309017;
                  # For     nautical twilight, use
                  #$this->R = -.207912;
                  # For        civil twilight, use
                  #$this->R = -.104528;
                  # For     sunrise or sunset, use
                  $this->R = -.0145439;
                #---------------------
                
            }
        function is_daylight_time($time)
            {
               list($dom, $dow, $month, $hour, $min) = explode(":", date("d:w:m:H:i", $time));
               if   ($month > 4 && $month < 10)
                  {
                         $this->retval = 1;        # May thru September
                  }
               elseif ($month == 4 && $dom > 7)
                  {
                         $this->retval = 1;        # After first week in April
                  }
               elseif ($month == 4 && $dom <= 7 && $dow == 0 && $hour >= 2)
                  {
                         $this->retval = 1;        # After 2am on first Sunday ($dow=0) in April
                  }
               elseif ($month == 4 && $dom <= 7 && $dow != 0 && ($dom-$dow > 0))
                  {
                         $this->retval = 1;        # After Sunday of first week in April
                  }
               elseif ($month == 10 && $dom < 25)
                  {
                         $this->retval = 1;        # Before last week of October
                  }
               elseif ($month == 10 && $dom >= 25 && $dow == 0 && $hour < 2)
                  {
                         $this->retval = 1;        # Before 2am on last Sunday in October
                  }
               elseif ($month == 10 && $dom >= 25 && $dow != 0 && ($dom-24-$dow < 1) )
                  {
                         $this->retval = 1;        # Before Sunday of last week in October
                  }
               else
                  {
                         $this->retval = 0;
                  }

                  

                return $this->retval;
        }
    function sunrise()
        {
            $J =  $this->A;
            $K = $this->yday + (($J - $this->F) / $this->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);
                if ($this->D == 0)
                  {
                     echo "Trying to normalize with zero offset..."; exit;
                  }
                while ($M < 0)
                  {
                     $M = ($M + $this->D);
                  }
                while ($M >= $this->D)
                  {
                     $M = ($M - $this->D);
                  }
                if (($M / $this->A) - intval($M / $this->A) == 0)
                  {
                     $M += 4.84814E-06;
                  }
            $P = sin($M) / cos($M);                   # Solar Right Ascension
            $P = atan2(.91746 * $P, 1);
            # Quadrant Adjustment
                if ($M > $this->C)
                  {
                     $P += $this->D;
                  }
                else
                  {
                     if ($M > $this->A)
                        {
                           $P += $this->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 = $this->R - (sin($Q) * sin($this->E));
            $S = $S / (cos($Q) * cos($this->E));
                if (abs($S) > 1)
                    {
                        echo 'none';
                    }     # Null phenomenon
            $S = $S / sqrt(-$S * $S + 1);
            $S = $this->A - atan2($S, 1);
            $S = $this->D - $S ;
            $T = $S + $P - 0.0172028 * $K - 1.73364; # Local apparent time
            $U = $T - $this->F;                            # Universal timer
            $V = $U + $this->G;                            # Wall clock time
            # Quadrant Determination
                if ($this->D == 0)
                    {
                        echo "Trying to normalize with zero offset..."; exit;
                    }
                while ($V < 0)
                    {
                        $V = ($V + $this->D);
                    }
                while ($V >= $this->D)
                    {
                        $V = ($V - $this->D);
                    }
            $V = $V * 3.81972;
            $hour = intval($V);
            $min  = intval((($V - $hour) * 60) + 0.5);
            return date( "G:i ", mktime($hour,$min,0,$this->mon,$this->mday,$this->year) );
            //return mktime($hour,$min,0,$this->mon,$this->mday,$this->year) - 1800;
        }
    function sunset()
        {
            $J =  $this->C;
            $K = $this->yday + (($J - $this->F) / $this->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);
                if ($this->D == 0)
                  {
                     echo "Trying to normalize with zero offset..."; exit;
                  }
                while ($M < 0)
                  {
                     $M = ($M + $this->D);
                  }
                while ($M >= $this->D)
                  {
                     $M = ($M - $this->D);
                  }
                if (($M / $this->A) - intval($M / $this->A) == 0)
                  {
                     $M += 4.84814E-06;
                  }
            $P = sin($M) / cos($M);                   # Solar Right Ascension
            $P = atan2(.91746 * $P, 1);
            # Quadrant Adjustment
                if ($M > $this->C)
                  {
                     $P += $this->D;
                  }
                else
                  {
                     if ($M > $this->A)
                        {
                           $P += $this->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 = $this->R - (sin($Q) * sin($this->E));
            $S = $S / (cos($Q) * cos($this->E));
                if (abs($S) > 1)
                    {
                        echo 'none';
                    }     # Null phenomenon
            $S = $S / sqrt(-$S * $S + 1);
            $S = $this->A - atan2($S, 1);
            #$S = $this->D - $S ;
            $T = $S + $P - 0.0172028 * $K - 1.73364; # Local apparent time
            $U = $T - $this->F;                            # Universal timer
            $V = $U + $this->G;                            # Wall clock time
            # Quadrant Determination
                if ($this->D == 0)
                    {
                        echo "Trying to normalize with zero offset..."; exit;
                    }
                while ($V < 0)
                    {
                        $V = ($V + $this->D);
                    }
                while ($V >= $this->D)
                    {
                        $V = ($V - $this->D);
                    }
            $V = $V * 3.81972;
            $hour = intval($V);
            $min  = intval((($V - $hour) * 60) + 0.5);
            return date( "G:i ", mktime($hour,$min,0,$this->mon,$this->mday,$this->year) );
            //return mktime($hour,$min,0,$this->mon,$this->mday,$this->year) + 1800;
        }

}

//$ext_light = show_list($keys_id, "#key_pio#", "", 1, "key_label='ext_light'", 1);

$sun = new sun($lat, $long, $utc);
$my_sunrise = $sun->sunrise();
$my_sunset = $sun->sunset();

sg('ThisComputer.SunSet',$my_sunset);
sg('ThisComputer.SunRise',$my_sunrise );
Алиска живёт на Ubuntu Server 14.04.3 LTS
connect, группа в Telegram, Яндекс.Деньги для благодарностей за помощь: 41001355945165
Urbas81
Сообщения: 289
Зарегистрирован: Вс сен 16, 2012 9:39 am
Благодарил (а): 0
Поблагодарили: 1 раз

Re: Восход и заход солнца

Сообщение Urbas81 » Пн дек 16, 2013 10:03 am

Ого, так в php есть встроенная ф-я, код занимает три строчки.
dmw
Сообщения: 469
Зарегистрирован: Вт мар 12, 2013 1:22 am
Благодарил (а): 41 раз
Поблагодарили: 81 раз

Re: Восход и заход солнца

Сообщение dmw » Пн дек 16, 2013 10:36 am

Аватара пользователя
Amarok
Сообщения: 1425
Зарегистрирован: Пт дек 14, 2012 12:24 pm
Откуда: Россия, Нижняя Тура
Благодарил (а): 460 раз
Поблагодарили: 126 раз
Контактная информация:

Re: Восход и заход солнца

Сообщение Amarok » Пн дек 16, 2013 12:38 pm

Urbas81 писал(а):Ого, так в php есть встроенная ф-я, код занимает три строчки.
Прошу пример?
dmw писал(а):А если так?: http://smartliving.ru/Main/ScOtherSunrise
От инета зависимо сие.
Алиска живёт на Ubuntu Server 14.04.3 LTS
connect, группа в Telegram, Яндекс.Деньги для благодарностей за помощь: 41001355945165
Urbas81
Сообщения: 289
Зарегистрирован: Вс сен 16, 2012 9:39 am
Благодарил (а): 0
Поблагодарили: 1 раз

Re: Восход и заход солнца

Сообщение Urbas81 » Пн дек 16, 2013 2:02 pm

BlackWarrior писал(а): Прошу пример?

Код: Выделить всё

$lat = 43.2;    // North   
$long = 39.5;    // East
$offset = 4;    // difference between GMT and local time in hours
$zenith=90+50/60;


echo 'Восход солнца: ' .date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
echo '<br>';
echo 'Заход солнца: ' .date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
echo '<br>';
Ответить