Script to run everyday at sunset Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
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:40

I 've no idea how to read that! :lol: What is it? :D

That applet I mentioned is something similar to the built-in Windows 10+ function that changes brightness and color temperature according to current time. I just tried to make it a bit more fancier, and for that I used the two twilight values in order to initiate the slow change more accurately. It works fine when connected to the web, but for those that can't or won't there is a manual option which uses fixed values for everything - something I'm not quite happy with. This function here could help in calculating the real values offline with a much less margin of error... if I can port it from AHK to JS. :D
Still, I had promised - at least to myself - to build an AHK version of that applet and maybe it's time to do that. All I need is to find out how to access Unix commands from Windows code under Wine. :?
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, 12:03

Drugwash wrote:
19 Jan 2022, 11:40
I 've no idea how to read that! :lol: What is it? :D
Its a ticking calendar... and you are seeing the moment that I took the screenshot. :D
If you/anyone need the most accurate (offline) source for sun rise/set in windows, Swiss Ephemeris DLL's would be the solution.

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, 12:14

Not for Linux, unfortunately, unless it's an application running under Wine or in a Windows virtual machine. ;) To me a few minutes' error is acceptable, no need for super-precision.

That calendar looks more like tick-tock-boom! :lol: So much information crammed together. Darn, I'm getting old. :D
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, 12:32

Drugwash wrote:
19 Jan 2022, 12:14
Not for Linux, unfortunately, unless it's an application running under Wine or in a Windows virtual machine. ;)
Ah! I see.
To me a few minutes' error is acceptable, no need for super-precision.
Yes. I already said it was an overkill. :)
That calendar looks more like tick-tock-boom! :lol: So much information crammed together. Darn, I'm getting old. :D
Me too. If you hadn't noticed the title-bar, it is running in a AHK version released in 2013 (that's when I wrote it)
8 bitmaps, data composed on buffer and blitted. Earth moves very fast than I had imagined. I refresh the calendar only per sec.. No flcker :D

PS: My apologies to the OP for getting off-topic. :(

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

Re: Script to run everyday at sunset

Post by amateur+ » 19 Jan 2022, 19:16

Ready. I've updated the function sunset(), now it's also able to calculate civil, nautical and astronomical twilight time returning an object if a new twilight parameter is true.
viewtopic.php?p=439619#p439619

@SKAN, I think I'm a noob to handle with that dll. But if someone will make a script to get extremly precise sunset/twilight data, it will be funny :).
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 » 20 Jan 2022, 09:19

Thank you for implementing the additional parameters. :thumbup:
Now, one minor observation if I may: wouldn't it be more consistent if the result would be an object in all cases, rather than String vs Object? It would make the usage more flexible since the output would always be parsed like an object regardless of the input type, and it would be easier to switch between input types without changing much of the parsing code.
Just saying. What would others think?
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 » 20 Jan 2022, 12:10

I have no idea ...... is this correct / how I use a function ?

Code: Select all

;- https://www.autohotkey.com/boards/viewtopic.php?p=439619#p439619  / user amateur+  ( function above )
;- https://gml.noaa.gov/grad/solcalc/solareqns.pdf
;- https://www.timeanddate.com/astronomy/different-types-twilight.html
;- https://www.timeanddate.com/sun/romania/Ploiesti
;- https://latitudelongitude.org/ro/ploiesti/
;-
lati:=44.95
long:=26.01667
Sunrise_RO_pl:=sunset(lati,long,1,,a_now)
Sunset_RO_pl :=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.sunrise
aa4:=twilight2.sunset . "-" . twilight2.nautical  
aa5:=twilight1.astronomical . "-" . twilight1.sunrise
aa6:=twilight2.sunset . "-" . twilight2.astronomical 
;---
msgbox, 262144,SUNSET/SUNRISE ,RO-Ploieşti= %Sunrise_RO_pl%---%Sunset_RO_pl%`nTwilight civil= %aa1% / %aa2%`nTwilight nautical=%aa3% / %aa4%`nTwilight astronomical=%aa5% / %aa6%
return

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 » 20 Jan 2022, 14:17

Maybe there's no need for the two separate parameters. The function could always return the whole set of timestamps as an object, and the user simply selects the desired one(s).
So it'd simply be sunset(latitude := 0, longitude := 0, date := "") {...} or maybe better named suntimes(). Or something. :)
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+ » 20 Jan 2022, 14:35

@garry, I think this would be better:

Code: Select all

lati:=44.95
long:=26.01667
Sunrise_RO_pl:=sunset(lati,long,1,,a_now)
Sunset_RO_pl :=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 ,RO-Ploiesti= %Sunrise_RO_pl%---%Sunset_RO_pl%`nTwilight civil= %aa1% / %aa2%`nTwilight nautical=%aa3% / %aa4%`nTwilight astronomical=%aa5% / %aa6%
Civil Twilight – Definition and Explanation
Also I think we may add another new parameter timezone, so output could be in local time of the chosen location if the script writer knows its timezone.
@Drugwash, I thought about alternatives but at the moment I decided to choose string+obj realization.
I didn't want to kill usual string function for those time-to-time scripters who will need only sunrise/sunset time and don't want to learn what objects in programming are.
Initially I thought about two different functions: sunset() and twilight(). The latter should return an object and the first would be earlier non-twilight string version of sunset(). But the calculations inside the functions would be almost the same so I thought it would be strange to offer to people to have in their libraries two different functions-twins. So I decided to join them to offer the user a choice via additional parameter.
For example, one can write a code to handle object types only and all he need to do is to use twilight = true always even for single sunrise/sunset time (since obj.sunset/obj.sunrise are also present among object keys).
But I'm not a professional programmer and open for further discussion with more experienced people than me. Should we have two different functions or maybe just one always returning a twilight object or maybe object with all 8 keys (for sunrise and sunset)?
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 » 20 Jan 2022, 16:04

In regard to timezone, doesn't the OS provide some API to retrieve the timezone for a specified location based on geolocation?
There can be four distinct usage patterns for timezone:
1. Local coordinates and corresponding local timezone
2. Local coordinates and some remote timezone
3. Remote coordinates and corresponding remote timezone
4. Remote coordinates and local timezone
Each pattern may have its own purpose, that is for the user to decide. The function should be able to provide either of the results.

In regard to result type, although I usually am a bit reluctant to objects in this case it may be easier and more versatile to only use an object for all possible results, eliminating any possible ambiguity related to parameter usage and return type. Granted a String would be straightforward and directly usable but it would create confusion.
Then, adding yet another parameter - namely timezone - besides the current two would complicate the function, adding to the confusion.
So my humble opinion is to keep only the object and eliminate the two switch parameters in view of adding timezone.
In time other results could be added to the object, such as duration between civil and nautical twilight, or day length (between sunrise and sunset) or whatever other combination.
Let's hear other opinions.
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+ » 20 Jan 2022, 19:13

I think you should be right and we need to return an object with all 8 values (and maybe some extra that you've mentioned).
Lets choose appropriate object structure. What will be convinient? Example:

Code: Select all

t_obj := {dark_end: t, duration: dt}
suntime_obj := {sunrise:	{itself: t, civil: t_obj, nautical: t_obj, astronomical: t_obj}
			  , sunset:		{itself: t, civil: t_obj, nautical: t_obj, astronomical: t_obj}
			  , day:		dt
			  , night: 		dt}
; Or:
t_obj := {dark_end: t, duration: dt}
suntime_obj := {morning:	{sunrise: t, civil: t_obj, nautical: t_obj, astronomical: t_obj}
			  , evening:	{sunset:  t, civil: t_obj, nautical: t_obj, astronomical: t_obj}
			  , day:		t
			  , night: 		t}

Drugwash wrote: In regard to timezone, doesn't the OS provide some API to retrieve the timezone for a specified location based on geolocation?
I'm a noob here. It will be very cool if it does provide and someone will help with corresponding lines of code. Maybe @boiler or others could help.
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 » 21 Jan 2022, 09:48

Maybe first one sounds more appealing. Using time instead of itself could be easier, shorter and more straightforward (sunrise.time, sunset.time).

Can't say about any offline API, all I'm seeing on a quick search is online requests to various services.This may not be acceptable or available to certain users. Hopefully someone knows a better solution.
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 » 21 Jan 2022, 10:29

I think I do something wrong, don't understand how/when use date parameter
for Suzhou (china) I get wrong time ( 00:-5---10:21 ) and for Ploiești (Romania) is the time 1 hour minus

Code: Select all

run,https://www.timeanddate.com/sun/china/suzhou      ; utc+8  6.55--17.22  Suzhou, Jiangsu, China  2022-01-21
;run,https://latitudelongitude.org/cn/suzhou/         ; 31.30408, 120.59538
zn_time:=a_nowutc
Envadd,zn_time,8,hours
msgbox, 262144,China-Time,%zn_time%
lati:=  31.30408
long:= 120.59538
;Sunrise_CN_sh:=sunset(lati,long,1,,zn_time)
;Sunset_CN_sh :=sunset(lati,long,0,,zn_time)
Sunrise_CN_sh:=sunset(lati,long,1,,"")
Sunset_CN_sh :=sunset(lati,long,0,,"")
msgbox, 262144,SUNSET/SUNRISE ,China_Suzhou_Jiangsu= %Sunrise_CN_sh%---%Sunset_CN_sh%
return

/*
;------------
run,https://www.timeanddate.com/sun/romania/ploiesti
ro_time:= A_nowutc
;ro_time:= a_now
Envadd,ro_time,2,hours        ;- Romania UTC+2
;msgbox,%ro_time%
;-----------
lati:=44.95
long:=26.01667
Sunrise_RO_pl:=sunset(lati,long,1,,ro_time)
Sunset_RO_pl :=sunset(lati,long,0,,ro_time)
;Sunrise_RO_pl:=sunset(lati,long,1,,"")
;Sunset_RO_pl :=sunset(lati,long,0,,"")
msgbox, 262144,SUNSET/SUNRISE ,RO-Ploiesti= %Sunrise_RO_pl%---%Sunset_RO_pl%
return
*/

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 » 21 Jan 2022, 13:11

Let's wait until the function gets to a final form, maybe certain bugs will get squashed in the process.
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+ » 21 Jan 2022, 16:06

garry wrote:and for Ploiești (Romania) is the time 1 hour minus
@garry, sunset() returns time that is always local for the user that has run the script. If you run the function then that user is you. You live in GMT+1 location. Timezone for Romania is GMT+2. Sunrise in Ploiești on January 21st is at 07:47 for local for Romania GMT+2 timezone and at 06:47 for your local GMT+1 timezone. The user is you, so sunset() will return you 06:47, not 07:47.
We are discussing now also a new timezone parameter that would allow to return local for that timezone time instead of limit it to just user's local time.
So what does date parameter mean? During the year sunrise and sunset time changes. date parameter sets the day. If omitted, the funtion will return you today's sunrise/sunset time. If you set it to 2022 February 14th, it will help you to get sunset time for St. Valentine's day if you plan some promenade that evening with your wife/girlfriend.
garry wrote:for Suzhou (china) I get wrong time ( 00:-5---10:21 )
You've found a bug :). Thanks!
-5 minutes from your midnight is 23:55 of previous day for your local timezone. Now I've adjusted the function to return "23:55" instead of buggy "00:-5".
@Drugwash,
What are suggetions for dark_end? Maybe dark_border? Or darkBorder? Any other ideas?
Last edited by amateur+ on 24 Jan 2022, 03:12, edited 1 time 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
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: Script to run everyday at sunset

Post by Drugwash » 21 Jan 2022, 17:55

amateur+ wrote:
21 Jan 2022, 16:06
What are suggestions for dark_end? Maybe dark_border? Or darkBorder?
Just... end? sunrise.civil.end sounds pretty clear to me, as an example. :)
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+ » 21 Jan 2022, 18:59

It is quite precise word for evening twilight. But in the morning it is a start point of current twilight stage, not the end.
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 » 21 Jan 2022, 20:38

My fault for not paying enough attention to the code. :oops:
In the applet I implemented a circular pattern so each value is an end to a period that started with the previous end, exception being the sunrise which is the only start (and is also the end of the cycle). That's why I thought of a simple and generic end name for all values. Of course, the names there were only used internally so it didn't matter much. Here it can be start/end according to morning/evening.
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 » 22 Jan 2022, 13:23

@amateur+
thank you for explanation , I'm an idiot ... I was wrong , was meaning I can use UTC +8 for China to get it's local time for sunrise/sunset )
We are discussing now also a new timezone parameter that would allow to return local for that timezone time instead of limit it to just user's local time.
So what does date parameter mean?
During the year sunrise and sunset time changes.
Date parameter sets the day.
If omitted, the function will return you today's sunrise/sunset time.
If you set it to 2022 February 14th, it will help you to get sunset time for St. Valentine's day if you plan some promenade that evening with your wife/girlfriend.

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

Re: Script to run everyday at sunset

Post by amateur+ » 22 Jan 2022, 18:36

@garry, of course no, you're nice and clever. And thank you for examples to functions you always provide on the forum. We'll definitely add timezone parameter to the function and it will be more comfortable for certan usage.
Maybe we should use time property too instead of start/end?
obj.civil.time, obj.civil.duration, obj.sunset.time

Code: Select all

t_obj := {time: t, duration: dt}
suntime_obj := {sunrise:	{time: t, civil: t_obj, nautical: t_obj, astronomical: t_obj}
			  , sunset:		{time: t, civil: t_obj, nautical: t_obj, astronomical: t_obj}
			  , day:		dt
			  , black_night:dt}
; black_night is a time period between astronomical evening and morning twilights, not between sunset and sunrise.
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.

Post Reply

Return to “Ask for Help (v1)”