Script to run everyday at sunset Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Script to run everyday at sunset

Post by garry » 14 Jan 2022, 14:23

sunrise calculation formula ( exist in ahk written formula ? )
;- https://gml.noaa.gov/grad/solcalc/solareqns.PDF
;-
;- https://gml.noaa.gov/grad/solcalc/sunrise.html
;- https://www.timeanddate.com/sun/australia/brisbane

this was in GWBASIC ~1982

Code: Select all

12900 '----SUNRISE   / SUNSET  ---------------------------------------------------
12950 'Basel     47.33   -07.35,  south and east negative
13000 'Zurich    47.22   -08.32
13050 'Bern      46.56   -07.26
13060 'VALLETTA  35.7    -14.3
13090 CLS
13100 '
13150 DIM N(12)
13200 D1=46.56:D2=-7.26           'BERN
13210 '
13250 PL=3.141592654#/26:J=57.29577951#
13300 GOSUB 16750
13350 LA = D1
13400 IF LA < 0 THEN LA = LA + 180
13450 IF D2 < 0 THEN D2 = D2 + 360
13500 LO = FIX(D2/15)*15 :REM finds time zone beginning
13550 TD=(D2-LO)/15
13600 '
13650 M$=MID$(DATE$,1,2):M=VAL(M$)
13700 DA$=MID$(DATE$,4,2):DA=VAL(DA$)
13750 '
13800 FOR I=1 TO 12: READ N(I):NEXT I
13850 DATA 0,31,59,90,120,151
13900 DATA 181,212,243,273,304,334
13950 X=(N(M)+DA)/7
14000 '
14050 D=.4560001-22.195*COS(PL*X)-.43*COS(2*PL*X)-.156*COS(3*PL*X)+3.83*SIN(PL*X)+.06*SIN(2*PL*X)-.082*SIN(3*PL*X)
14100 '
14150 'LOCATE 3,2
14200 PRINT"DECLINATION OF SUN:";
14250 PRINT USING"###.#";D;
14300 PRINT" DEGREES"
14350 E=8.000001E-03+.51*COS(PL*X)-3.197*COS(2*PL*X)-.106*COS(3*PL*X)-.15*COS(4*PL*X)-7.317001*SIN(PL*X)-9.471001*SIN(2*PL*X)-.391*SIN(3*PL*X)-.242*SIN(4*PL*X)
14400 '
14450 PRINT"EQUATION OF TIME:";
14500 PRINT USING"###.#";E;
14550 PRINT" MINUTES"
14600 CL=COS(LA/J):SD=SIN(D/J):CD=COS(D/J):Y=SD/CL
14650 IF ABS(Y)=>1 THEN PRINT"NO SUNRISE OR SUNSET":GOTO 17150
14700 Z = 90 - J*ATN(Y/SQR(1-Y*Y))
14750 PRINT"AZIMUTH OF SUNRISE:";
14800 PRINT USING"####.#";ABS(Z);
14850 PRINT" DEGREES"
14900 PRINT"AZIMUTH OF SUNSET: ";
14950 PRINT USING"####.#";360-ABS(Z);
15000 PRINT" DEGREES"
15050 ST=SIN(Z/J)/CD
15100 IF ABS(ST)>=1 THEN T=6:TT=6:GOTO 15300
15150 CT=SQR(1-ST*ST)
15200 T=J/15*ATN(ST/CT)
15250 TT=T
15300 'x
15350 IF D<0 AND LA<90 THEN T=12-T:TT=T
15400 IF D>0 AND LA>90 THEN T=12-T:TT=T
15450 T=T+TD-E/60-.04
15500 GOSUB 16050
15550 'LOCATE 3,2
15600 'PRINT "TIME OF SUNRISE BERN:";T1$;":";T2$;" ";T$;"L.T. ";GM$;":";T2$;" GM";
15650 PRINT "SWITZERLAND BERN     SUNRISE= ";T1$;":";T2$;" ";
15700 T=12-TT:T=T+TD-E/60+.04
15750 CNT=1
15800 T12=12
15850 GOSUB 16050
15900 'PRINT "TIME OF SUNSET= ";T1$;":";T2$;" ";T$;"L.T. ";GM$;":";T2$;" GM";" ";SD$
15950 PRINT "  SUNSET= ";T1$;":";T2$;" ";SD$
16000 GOTO 17150
16050 'x
16100 T1=INT(T):T2=T-T1
16150 IF SD$="ST" THEN LET T1=T1+1
16200 T1$=STR$(T1+T12):T2=INT((T2*600+5)/10):IF T2=60 THEN 16250 ELSE 16300
16250 T2=0:T1=T1+1:T1$=STR$(T1+T12)
16300 T2$=STR$(T2):T2$=RIGHT$(T2$,LEN(T2$)-1)
16350 IF INT(T2)<10 THEN T2$="0"+T2$
16400 GM = FIX(D2/15) :REM calculate difference between GM and local time
16450 IF CNT = 0 THEN GM = VAL(T1$)+GM :REM GMT for sunrise
16500 IF CNT > 0 THEN GM = VAL(T1$)+12+GM :REM GMT for sunset
16550 IF GM +(VAL(T2$)/60)> 24 THEN GM = GM - 24
16600 GM$ = STR$(GM) :GM$ = RIGHT$("0"+GM$,2)
16650 RETURN
16700 '
16750 'x This subroutine converts DD.MM input to DD.DD
16800 DEGTMP = (ABS(D1)-ABS(FIX(D1))) *100/60
16850 D1 = (FIX(ABS(D1))+DEGTMP)*SGN(D1)
16900 DEGTMP = (ABS(D2)-ABS(FIX(D2))) *100/60
16950 D2 = (FIX(ABS(D2))+DEGTMP)*SGN(D2)
17000 RETURN
17050 '
17100 '
17150 'END
30030 INPUT "PUSH ENTER";nothing
30040 A$=INKEY$
30050 IF LEN (A$)=0 THEN 30040
30060 IF ASC(A$)=13 THEN 31040
30080 'A$=INKEY$:IF A$="" THEN 30080
31030 'SHELL "CD\":SYSTEM
31040 PRINT "END"
Last edited by garry on 15 Jan 2022, 03:23, edited 1 time in total.

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Script to run everyday at sunset

Post by amateur+ » 14 Jan 2022, 18:31

RussF wrote:
14 Jan 2022, 10:01
Your example uses a negative timer value, which means the timer would fire only once until reset.
I thought that my explanation was so clear that even a child wouldn't have any problems in understanding, but nope...
And it is no way an offtopic! You and silentrings just don't read what is written.
Look, we talked about a one-time sleep (or settimer) adjustment of the clock. Of course one should use negative time parameter in Settimer if he wants to StartTheClock (that was the name of the label). How many times do you need to start the clock? How many times do you need to start a pendulum? To push On/Off button when you want to turn on your computer? Once, once and once. So you should use a negative number. It is so clear and obvious...

And there is absolutely no matter how often do you need to update other parameters for your furniture workers. You may update some information for them every 20 ms. But if you need to update the clock numbers just every minute, there is no any sense in checking the time every 20 ms, every second or every 10 seconds. Settimer function allows you to have timers for every 20 ms and every 10 hours simultaneously. You may have a hundred of timers if you need.

Here is an GUI-example below where the time is 1-5 seconds precisely checking and updating only once in a minute but another information for furniture workers is being updating once in every 2 seconds:

Code: Select all

gui, Add, Text, , Very large table for my workers with a huge font size
gui, Add, Text, Section, Workers group #1:
gui, Add, Text, , Workers group #2:
gui, Add, Text, , Time:
gui, Add, Text, ys w100 vG1,
gui, Add, Text, w100 vG2,
gui, Add, Text, vTime, % A_Hour ":" A_Min 
gui, Show,, Huge 100" display
gosub, AdjustTheClock
SetTimer, FurnitureWorkSimulate, 2000
return

AdjustTheClock:
Settimer, StartTheClock, % -Mod(61 - A_Sec, 60) *1000
return

StartTheClock:
SetTimer, Clock, % 60 *1000
gosub, Clock
return

Clock:
GuiControl, Text, Time,  % A_Hour ":" A_Min 
if (A_Sec >= 5)
	gosub, AdjustTheClock
return

FurnitureWorkSimulate:
loop, 2 {
	Random, G%A_Index%, 10000, 99999
	GuiControl, Text, G%A_Index%,  % "Article " . G%A_Index%
}
return

silentrings wrote:
14 Jan 2022, 10:51
Still open for your kind suggestions on specifics to how "Script to run everyday at sunset"
@silentrings, you really don't listen what people tell you. Your case is in fact closed as soon as you'd said that you hadn't any problems in calculation of a current sunset time with that precision that you need. Generally here is nothing to discuss anymore about your subject. You got all answers and recomendations.
@mikeyww offered you a way to check for the question "is it sunset already or not" just for every minute. But you denied it and start asking for more economic decision! Wow! To check a constant string (current sunset time) with % A_Hour A_Min once a minute is a way to crack your machine? I guess you have several GHz CPU and maybe it even has several cores. If you have 1-core 1 GHz CPU, you can afford >1000 000 000 elementary operations per second. Per minute it will be 60 000 000 000 operations. How many of them would you waste for a comparison of one short string with another one of two variables? Even if it would consume 100 000 operations you would have 600 000 * 100 000 every minute! If you have 8-core 3 GHz, you have 14 400 000 * 100 000 affordable operations every minute!

But ok, then was offered the solution to set a timer just once that will fire at the moment you need in the future. But not @RussF, not you don't hear. And you continue to ask for another way. If you know how to calculate today's sunset, just take A_Now, calculate how many miliseconds you should wait for the sunset and set a timer or sleep delay for that amount of miliseconds. And like Russ should, do it just once in the auto-execute section and you won't even need to check it every minute like Russ has to. Just once set the timer for hours ahead! No checks every second, no checks every minute, it is just one single time. It will consume like really nothing of your CPU limit and a couple of MB of your RAM if compiled (even if you have just 1 GB, it is still ~0.2% and 0.05% if you have 4 GB).
But nope, you will go to spend hours in learning how to dynamically set a task in a taskschd.
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

silentrings
Posts: 7
Joined: 13 Jan 2022, 06:03

Re: Script to run everyday at sunset

Post by silentrings » 15 Jan 2022, 12:52

Hi all,
final update: it works great!
I had to change the initial AHK script a bit, but the mentioned solution using sunwait works fine for our specific needs (not reliant on internet, reboot proof etc).
Many thanks to all, especially @ mikeyww (loved that "Time for bed") and many others for getting here.

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Script to run everyday at sunset

Post by garry » 15 Jan 2022, 13:38

how works a calculation in ahk-script depending latitude/longitude/date ?
@flyingDman has a good script for south california
a sunrise calculation formula
https://gml.noaa.gov/grad/solcalc/solareqns.PDF

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Script to run everyday at sunset

Post by amateur+ » 17 Jan 2022, 22:52

Here is the function sunset() with @garry's math.

Code: Select all

;-------------------------------------------------------------
;	Function 	sunset()
;		MIT license.
;		https://www.autohotkey.com/boards/viewtopic.php?p=439619#p439619
;		https://gml.noaa.gov/grad/solcalc/solareqns.pdf
; 	Parameters:
;		latitude		in degrees. 0 (equator) by default.
;		longitude		in degrees. 0-meridian (London, England) by default.
;		sunrise			boolean. If true, sunrise time is returned, otherwise - sunset. False (sunset) by default.
;		twilight		boolean. If true, an object {sunset: t, civil: t, nautical: t, astronomical: t} will be 
;					returned, where "t" is in "hh:mm" format of the end (for sunset and start for sunrise) of 
;					mentioned twilight stage. Also {sunrise: t,...} for sunrise. If twilight = false (by default) 
;					then not an object but a string in "hh:mm" format of sunset/sunrise time will be returned.
;		date			The date (in the YYYYMMDDHH24MISS format) of the day you need to know sunset/sunrise time. 
;					Today (A_NowUTC) by default
; 	Description:
;  		The function calculates offline and returns sunset or sunrise time of chosen geo location and date in "hh:mm" format. 
;		Returned time is always local for the user (not for chosen location or UTC). 
;		To get local time for chosen location you'll need to add the offset between the user's 
;		local time and local time of chosen location.
;		If twilight parameter is true, then an object of twilight stages is returned (read above about twilight parameter).
;	Examples:
;	1).
;		MsgBox, % sunset(40.689259, -74.044420)	
; 			; It is user's local time of today's sunset near the Statue of Liberty, New York.
;	2).
;		MsgBox, % sunset(51.507351, , "sunrise please", , A_YYYY)	
; 			; It is user's local time of January, 1st this year's sunrise in London, England.
;	3).
;		twilight_obj := sunset(47.37, 8.54, , "can I get twilight?")
;		MsgBox, % twilight_obj.sunset " - " twilight_obj.civil
;			; It will show today's civil twilight period after sunset in Zurich, Switzerland. https://www.timeanddate.com/sun/switzerland/zurich
;	Thanks to @garry, @mikeyww, @boiler, @flyingDman and @Drugwash at AutoHotkey.com
;-------------------------------------------------------------

sunset(latitude := 0, longitude := 0, sunrise := false, twilight := false, date := "") {
	if (date = "")
		date := A_NowUTC
	if date is not time
		return
	year := SubStr(date, 1, 4), yhours := date
	yhours -= year, Hours
	daysInYear := Mod(year, 4) || !Mod(year, 100) && Mod(year, 400) ? 365 : 366
	pi := 4*ATan(1)	; 3.1415926...
	gamma := 2*pi/daysInYear*(yhours - 12)/24 ; The fractional year in radians
	eqtime := 229.18*(0.000075+0.001868*Cos(gamma)-0.032077*Sin(gamma)-0.014615*Cos(2*gamma)-0.040849*Sin(2*gamma))	; In minutes
	decl := 0.006918-0.399912*Cos(gamma)+0.070257*Sin(gamma)-0.006758*Cos(2*gamma)+0.000907*Sin(2*gamma)-0.002697*Cos(3*gamma)+0.00148*Sin(3*gamma)	; Solar declination angle (in radians).
	degree_to_rad := 2*pi/360	; Coefficient of convertion from degrees to radians.
	lat := degree_to_rad*latitude	; The latitude in radians
	timezone := A_Now
	timezone -= A_NowUTC, Hours	; In hours
	time_offset := eqtime + 4*longitude - 60*timezone	; In minutes
	sunrise_coeff := sunrise ? 1 : -1
	a := Cos(lat)*Cos(decl), b := Tan(lat)*Tan(decl)
	angle_at_sunset := degree_to_rad*0.8525	; The angle under horizon of the center of the Sun at sunset converted to radians.
	; It is the approximate correction for atmospheric refraction at sunrise and sunset, and the size of the solar disk. 
	; 35.4' for refraction and 31.5'/2 is a visible angle of a half of the solar disk. 35.4' + 31.5'/2 = 0.8525 degrees.
	ha := sunrise_coeff/degree_to_rad*ACos(Sin(-angle_at_sunset)/a - b)	; The hour angle (in degrees) positive for sunrise and negative for sunset.
	sunset := Round(720 - 4*ha - time_offset)	; In minutes from midnight because 1 degree = 4 minutes of time. It can be negative. If ha is positive, it is sunrise time.
	sunset += 1440	; +24 hours to avoid negative values.
	sunset_hhmm := Format("{:02d}:{:02d}", Mod(sunset//60, 24), Mod(sunset,  60))	; In "hh:mm" format
	if !twilight
		return sunset_hhmm
	else {
		twilight_degrees := {civil: 6, nautical: 12, astronomical: 18}
		twilight_obj := {(sunrise ? "sunrise" : "sunset") : sunset_hhmm}
		for tw, deg in twilight_degrees {
			twilight_mins := Round(720 - time_offset - 4*sunrise_coeff/degree_to_rad*ACos(Sin(-degree_to_rad*deg)/a - b))	; The end (if sunset and start if sunrise) of current twilight stage in minutes from midnight. It can be negative.
			; We've used the formula: cos(ha) = sin(angle_to_horizon)/cos(Lat)/cos(decl) - tan(Lat)*tan(decl)
			twilight_mins += 1440	; +24 hours
			twilight_obj[tw] := Format("{:02d}:{:02d}", Mod(twilight_mins//60, 24), Mod(twilight_mins,  60))	; In "hh:mm" format
		}
		return twilight_obj
	}
}
EDIT2:
fixed daysInYear from Mod(year, 4) ? 365 : 366 to more precise Mod(year, 4) || !Mod(year, 100) && Mod(year, 400) ? 365 : 366.
Also got rid of degree() and rad() functions and of four times recalculation of pi.
EDIT3:
Big update. Now the function is also able to calculate civil, nautical and astronomical twilight returning an object if a new twilight parameter is true.
EDIT4:
Fixed a bug of negative time found by garry.

Thanks to @mikeyww, @boiler, @flyingDman, @garry and @Drugwash.
Last edited by amateur+ on 21 Jan 2022, 16:03, edited 16 times in total.
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Script to run everyday at sunset

Post by mikeyww » 18 Jan 2022, 05:56

This is very nice! The formula for leap year (daysInYear) is incorrect but close enough for anyone's uses, I think.

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Script to run everyday at sunset

Post by garry » 18 Jan 2022, 05:58

@amateur+ thank you very much for your script sunrise/sunset calculation
I just mentioned some links ..
;-
;- Example for Switzerland Zurich :
;- https://latitudelongitude.org/ch/zurich/
;- 47.36667 , 8.55 ;- decimal https://latitudelongitude.org/decimal-dms
;- 47°22'0.01"N , 8°33'0"E. ;- DMS https://latitudelongitude.org/dms-decimal
;- Latitude position : Equator ⇐ 5267km (3273mi) ⇐ Zürich ⇒ 4740km (2946mi) ⇒ North pole.
;- Longitude position : Prime meridian ⇒ 644km (400mi) ⇒ Zürich. GMT: +1h.
;- Local time in Zürich: Tuesday 11:27 am, January 18, 2022.

;- https://www.timeanddate.com/sun/switzerland/zurich
;- Current Time : 18. Jan 2022, 11:34:53
;- Sun Direction: 164,65° SSE
;- Sun Altitude : 20,78°
;- Sun Distance : 147,177 million km
;- Next Equinox : 20.03.2022 16:33 (Vernal)
;- Sunrise Today: 08:06 120° Southeast
;- Sunset Today: 17:06 240° Southwest
;=======================================================================================

EDIT1: new parameters twilight, changed , msgbox show result for test with the function sunset() script from user amateur+ above ...
EDIT2: example corrected by amateur+

Code: Select all

CH-Zurich            =       08:05---17:08
Twilight civil       = 07:31-08:05 / 17:08-17:42
Twilight nautical    = 06:53-07:31 / 17:42-18:19
Twilight astronomical= 06:16-06:53 / 18:19-18:56

Code: Select all

gosub,text
;- https://www.autohotkey.com/boards/viewtopic.php?p=439619#p439619  / user amateur+
;- https://gml.noaa.gov/grad/solcalc/solareqns.pdf
;- https://www.timeanddate.com/astronomy/different-types-twilight.html
;- https://www.timeanddate.com/sun/switzerland/zurich
;- https://latitudelongitude.org/ch/zurich/

;-
lati:=47.36667
long:=8.55
Sunrise_CH_zh:=sunset(lati,long,1,,a_now)
Sunset_CH_zh :=sunset(lati,long,0,,a_now)
;----
twilight1:=sunset(lati, long,1,1,a_now)           ;- twilight sunrise
twilight2:=sunset(lati, long,0,1,a_now)           ;- twilight sunset 
aa1:=twilight1.civil . "-" . twilight1.sunrise
aa2:=twilight2.sunset . "-" . twilight2.civil  
aa3:=twilight1.nautical . "-" . twilight1.civil
aa4:=twilight2.civil . "-" . twilight2.nautical  
aa5:=twilight1.astronomical . "-" . twilight1.nautical
aa6:=twilight2.nautical . "-" . twilight2.astronomical 
;----
msgbox, 262144,SUNSET/SUNRISE ,CH-Zurich= %Sunrise_CH_zh%---%Sunset_CH_zh%`nTwilight civil= %aa1% / %aa2%`nTwilight nautical=%aa3% / %aa4%`nTwilight astronomical=%aa5% / %aa6%`n`n%e4%
return

text:
e4=
(
-----------------
https://www.timeanddate.com/astronomy/different-types-twilight.html
-- The Different Types of Twilight, Dawn and Dusk
Civil twilight is the brightest form of twilight.
There is enough natural sunlight during this period that artificial light may not be required to carry out outdoor activities.
Only the brightest celestial objects can be observed by the naked eye during this time.
Several countries use this definition of civil twilight to make laws related to aviation, hunting, and the usage of headlights and street lamps.
-
-Nautical twilight occurs when the center of the Sun is between 6 degrees and 12 degrees below the horizon.
This twilight period is less bright than civil twilight and artificial light is generally required for outdoor activities.
-Nautical dawn occurs when the Sun is 12 degrees below the horizon during the morning.
-Nautical dusk occurs when the Sun is 12 degrees below the horizon in the evening.
-
-Civil twilight occurs when the Sun is less than 6 degrees below the horizon. In the morning, civil twilight begins when the Sun is 6 degrees below the horizon and ends at sunrise.
In the evening, it begins at sunset and ends when the Sun reaches 6 degrees below the horizon.
-Civil dawn is the moment when the center of the Sun is 6 degrees below the horizon in the morning.
-Civil dusk is the moment when the center of the Sun is 6 degrees below the horizon in the evening.
-
-Astronomical twilight occurs when the Sun is between 12 degrees and 18 degrees below the horizon.
-Astronomical dawn is the time when the center of the Sun is at 18 degrees below the horizon.
-Astronomical dusk is the instant when the center of the Sun is at 18 degrees below the horizon.
-
During astronomical twilight, most celestial objects can be observed in the sky. 
However, the atmosphere still scatters and refracts a small amount of sunlight, and that may make it difficult for astronomers to view the faintest objects.
Before astronomical dawn and after astronomical dusk, it is astronomical nighttime, when no indirect sunlight is visible and even faint celestial objects can be seen, weather permitting.
Shorter Twilight at the Equator
The length of twilight depends on the latitude. 
Equatorial and tropical regions tend to have shorter twilight than locations on higher latitudes.
During summer months at higher latitudes, there may be no distinction between astronomical twilight after sunset and astronomical twilight before sunrise. 
This happens when the Sun never goes more than 18 degrees below the horizon during the night.
Similarly, higher latitudes may experience an extended period of nautical or civil twilight.
---------------------
)
return

Last edited by garry on 20 Jan 2022, 15:38, edited 5 times in total.

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Script to run everyday at sunset

Post by garry » 18 Jan 2022, 06:09

@mikeyww
This is very nice! The formula for leap year (daysInYear) is incorrect but close enough for anyone's uses, I think.
... is it really incorrect (?)

Code: Select all

year:=2024
;year:=2022
daysInYear := Mod(year, 4) ? 365 : 366
msgbox,%daysinyear%

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Script to run everyday at sunset

Post by boiler » 18 Jan 2022, 06:23

Wikipedia wrote:In the Gregorian calendar, the standard calendar in most of the world…
The Gregorian reform modified the Julian calendar's scheme of leap years as follows:

Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400. For example, the years 1700, 1800, and 1900 are not leap years, but the years 1600 and 2000 are.

Over a period of four centuries, the accumulated error of adding a leap day every four years amounts to about three extra days.

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Script to run everyday at sunset

Post by flyingDman » 18 Jan 2022, 13:15

Borrowed from SKAN:

Code: Select all

y := 2024
msgbox % leap := !Mod(Y,4) && Mod(Y,100) || !Mod(Y,400) ? 1 : 0 
14.3 & 1.3.7

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Script to run everyday at sunset

Post by amateur+ » 18 Jan 2022, 14:47

I've fixed leap years calculation, thanks, guys!
garry wrote:
18 Jan 2022, 05:58

Code: Select all

Sunrise_CH_zh:=sunset(47.36667,8.55,1,a_now)
Sunset_CH_zh :=sunset(47.36667,8.55,0,a_now)
msgbox, Sunrise_CH-zh=%Sunrise_CH_zh%`nSunset_CH-zh =%Sunset_CH_zh%
return
Garry, it is better to ask for date = 2022011812 instead of a_now in your demonstration example because soon today's date will change.
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Script to run everyday at sunset

Post by garry » 18 Jan 2022, 15:51

thank you all for the precise calculation ( sunrise / sunset / leap year )

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Script to run everyday at sunset

Post by amateur+ » 19 Jan 2022, 04:27

I've changed leap years calculation close to SKAN's method daysInYear := Mod(year, 4) || !Mod(year, 100) && Mod(year, 400) ? 365 : 366.
Also I got rid of degree() and rad() functions and of four times recalculation of pi.
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Script to run everyday at sunset

Post by garry » 19 Jan 2022, 06:01

@amateur+ thank you for your script, works fine

User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: Script to run everyday at sunset

Post by Drugwash » 19 Jan 2022, 07:26

Feeling like a noob now. :D
@amateur+'s script results are very close to the times provided by an online service that I'm using in a Linux Mint Cinnamon applet. Deviation is only about 2-3 minutes, unimportant for the applet scope. Thing is, the applet also uses the nautical and civil twilight times for its calculations. I wonder if those times could be calculated as well by a/the script so that the online service could be avoided completely (except for getting the geolocation coordinates in cases where the machine is used when traveling).
The applet provides an option for manual input of sunrise/sunset times and an arbitrarily set twilight delay, but it would be much more accurate and useful if it used manual location coordinates with local calculations of sunrise/sunset and the nautical/civil twilight times.
Any insight on this would be much appreciated. 8-)
Part of my AHK work can be found here.

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Script to run everyday at sunset

Post by garry » 19 Jan 2022, 08:04

just some notes
when I worked on a ship (river) was night defined easy as ( must use position-lights ) until 30 minutes before sunrise and 30 minutes after sunset
------------------
https://www.timeanddate.com/astronomy/different-types-twilight.html
-- The Different Types of Twilight, Dawn and Dusk
Civil twilight is the brightest form of twilight.
There is enough natural sunlight during this period that artificial light may not be required to carry out outdoor activities.
Only the brightest celestial objects can be observed by the naked eye during this time.
Several countries use this definition of civil twilight to make laws related to aviation, hunting, and the usage of headlights and street lamps.

-Nautical twilight occurs when the center of the Sun is between 6 degrees and 12 degrees below the horizon.
This twilight period is less bright than civil twilight and artificial light is generally required for outdoor activities.
-Nautical dawn occurs when the Sun is 12 degrees below the horizon during the morning.
-Nautical dusk occurs when the Sun is 12 degrees below the horizon in the evening.
-
-Civil twilight occurs when the Sun is less than 6 degrees below the horizon. In the morning, civil twilight begins when the Sun is 6 degrees below the horizon and ends at sunrise.
In the evening, it begins at sunset and ends when the Sun reaches 6 degrees below the horizon.
-Civil dawn is the moment when the center of the Sun is 6 degrees below the horizon in the morning.
-Civil dusk is the moment when the center of the Sun is 6 degrees below the horizon in the evening.
---------------------

User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: Script to run everyday at sunset

Post by Drugwash » 19 Jan 2022, 08:37

Thanks @garry, that should be a useful information. :thumbup: The script should be able to work with that data after some slight changes. Now I wish I could remember enough javascript to change the applet code accordingly. :lol:
Part of my AHK work can be found here.

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Script to run everyday at sunset

Post by amateur+ » 19 Jan 2022, 10:45

@Drugwash, I guess twilights borders should be easy to calculate, I'll try to adjust the function to calculate them too.
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: Script to run everyday at sunset

Post by Drugwash » 19 Jan 2022, 11:22

Yes, I thought so too, it's just a matter of subtracting 6 or 12 degrees from the values before calculating.
Third parameter could be changed to accept one (or more) of a list of abbreviations for the different times, such as ctu/ctd (civil twilight up/sunrise, civil twilight down/sunset), ntu/ntd (nautical twilight up/sunrise, nautical twilight down/sunset), snr/sns (sunrise, sunset) or whatever other assortment of strings.
Part of my AHK work can be found here.

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Script to run everyday at sunset

Post by SKAN » 19 Jan 2022, 11:23

Hi @Drugwash
Overkill : Try Swiss Ephemeris
Spoiler

Post Reply

Return to “Ask for Help (v1)”