99_SUNRISE.pm 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ##############################################
  2. # - Use 99_SUNRISE_EL.pm instead of this module
  3. # - Be aware: Installing the DateTime modules might be tedious, one way is:
  4. # perl -MCPAN -e shell
  5. # cpan> install DateTime::Event::Sunrise
  6. # - Please call sunrise_coord before using this module, else you'll get times
  7. # for frankfurt am main (germany). See the "at" entry in commandref.html
  8. package main;
  9. use strict;
  10. use warnings;
  11. use DateTime;
  12. use DateTime::Event::Sunrise;
  13. sub sr($$$$);
  14. sub sunrise_rel(@);
  15. sub sunset_rel(@);
  16. sub sunrise_abs(@);
  17. sub sunset_abs(@);
  18. sub isday();
  19. sub sunrise_coord($$$);
  20. sub SUNRISE_Initialize($);
  21. # See perldoc DateTime::Event::Sunrise for details
  22. my $long = "8.686";
  23. my $lat = "50.112";
  24. my $tz = "Europe/Berlin";
  25. sub
  26. SUNRISE_Initialize($)
  27. {
  28. my ($hash) = @_;
  29. }
  30. ##########################
  31. # Compute:
  32. # rise: 1: event is sunrise (else sunset)
  33. # isrel: 1: _relative_ times until the next event (else absolute for today)
  34. # seconds: second offset to event
  35. # daycheck: if set, then return 1 if the sun is visible, 0 else
  36. sub
  37. sr($$$$)
  38. {
  39. my ($rise, $seconds, $isrel, $daycheck) = @_;
  40. my $sunrise = DateTime::Event::Sunrise ->new(
  41. longitude => $long,
  42. latitude => $lat,
  43. altitude => '-6', # Civil twilight
  44. iteration => '3');
  45. my $now = DateTime->now(time_zone => $tz);
  46. my $stm = ($rise ? $sunrise->sunrise_datetime( $now ) :
  47. $sunrise->sunset_datetime( $now ));
  48. if($daycheck) {
  49. return 0 if(DateTime->compare($now, $stm) < 0);
  50. $stm = $sunrise->sunset_datetime( $now );
  51. return 0 if(DateTime->compare($now, $stm) > 0);
  52. return 1;
  53. }
  54. if(!$isrel) {
  55. $stm = $stm->add(seconds => $seconds) if($seconds);
  56. return $stm->hms();
  57. }
  58. $stm = $stm->add(seconds => $seconds) if($seconds);
  59. if(DateTime->compare($now, $stm) >= 0) {
  60. my $tom = DateTime->now(time_zone => $tz)->add(days => 1);
  61. $stm = ($rise ? $sunrise->sunrise_datetime( $tom ) :
  62. $sunrise->sunset_datetime( $tom ));
  63. $stm = $stm->add(seconds => $seconds) if($seconds);
  64. }
  65. my $diff = $stm->epoch - $now->epoch;
  66. return sprintf("%02d:%02d:%02d", $diff/3600, ($diff/60)%60, $diff%60);
  67. }
  68. sub sunrise_rel(@) { return sr(1, shift, 1, 0) }
  69. sub sunset_rel(@) { return sr(0, shift, 1, 0) }
  70. sub sunrise_abs(@) { return sr(1, shift, 0, 0) }
  71. sub sunset_abs(@) { return sr(0, shift, 0, 0) }
  72. sub isday() { return sr(1, 0, 0, 1) }
  73. sub sunrise_coord($$$) { ($long, $lat, $tz) = @_; return undef; }
  74. 1;