It is currently April 24th, 2024, 9:38 pm

is it dark now?

Tips and Tricks from the Rainmeter Community
User avatar
moshi
Posts: 1740
Joined: November 13th, 2012, 9:53 pm

is it dark now?

Post by moshi »

here's a method to find out wether it is night or day.

what you need:
- the current time (i use local time here)
- times for sunrise and sunset. some weather feeds provide this info for example (i use Yahoo! Weather here)

let's look at the code:

Code: Select all

[Rainmeter]
BackgroundMode=1

[Variables]
WOEID=2367015

[MeasureCurrentTime]
Measure=Time
Format=%H%M

[MeasureSunrise]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url="http://weather.yahooapis.com/forecastrss?w=#WOEID#"
RegExp="(?siU).*<yweather:astronomy *sunrise="(.*)" *sunset="(.*)"/>"
Debug=1
StringIndex=1
DecodeCharacterReference=1
RegExpSubstitute=1
Substitute="^0":"","^12:(.*) pm":"12\1","^1:(.*) pm":"13\1","^2:(.*) pm":"14\1","^3:(.*) pm":"15\1","^4:(.*) pm":"16\1","^5:(.*) pm":"17\1","^6:(.*) pm":"18\1","^7:(.*) pm":"19\1","^8:(.*) pm":"20\1","^9:(.*) pm":"21\1","^10:(.*) pm":"22\1","^11:(.*) pm":"23\1","^12:(.*) am":"0\1",":":""," am":"","^$":"600"

[MeasureSunset]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[MeasureSunrise]
StringIndex=2
DecodeCharacterReference=1
RegExpSubstitute=1
Substitute="^0":"","^12:(.*) pm":"12\1","^1:(.*) pm":"13\1","^2:(.*) pm":"14\1","^3:(.*) pm":"15\1","^4:(.*) pm":"16\1","^5:(.*) pm":"17\1","^6:(.*) pm":"18\1","^7:(.*) pm":"19\1","^8:(.*) pm":"20\1","^9:(.*) pm":"21\1","^10:(.*) pm":"22\1","^11:(.*) pm":"23\1","^12:(.*) am":"0\1",":":""," am":"","^$":"1800"

[MeasureDayNight]
Measure=Calc
DynamicVariables=1
Formula=((([MeasureCurrentTime])<([MeasureSunrise])?10000:([MeasureCurrentTime]))>([MeasureSunset])?10000:20000)
Substitute="10000":"night","20000":"day","night":"000000","day":"ffffff"

[MeterIndicator]
Meter=Image
X=0
Y=0
W=100
H=100
SolidColor=[MeasureDayNight]
DynamicVariables=1
first of all make sure you insert the correct WOEID for your city in the [Variables] section.

you can see that i use 24hr time in [MeasureCurrentTime]. the format strips everything else but hours and minutes numbers, so we can use it for calculations.

[MeasureSunrise] and [MeasureSunset] are provided by Yahoo! Weather. as the feed returns them formatted like 5:24 pm for example, we need some substitutions to have them in the same format as [MeasureCurrentTime]. the last substitution on both measures is to provide a value in case the feed returns nothing.

now all we have to do is to use a calculation find out wether [MeasureCurrentTime] is smaller than [MeasureSunrise] or bigger than [MeasureSunset].

an example where is use a similar method is the world time in my Sphynx skin. the analog clocks are black at night and white at day.
Untitled-1.jpg
You do not have the required permissions to view the files attached to this post.
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: is it dark now?

Post by jsmorley »

Very cool. Just a small thing, and more or less personal preference as much as anything else, but most of the parentheses you are using are not really needed unless you like them for "readability" of the formula.

Code: Select all

;Formula=((([MeasureCurrentTime])<([MeasureSunrise])?10000:([MeasureCurrentTime]))>([MeasureSunset])?10000:20000)
Formula=[MeasureCurrentTime] < [MeasureSunrise] ? 10000 : ([MeasureCurrentTime] > [MeasureSunset] ? 10000 : 20000)
The second one works just as well as the first...

I only point this out as sometimes I think the "blizzard of parentheses" new users see on these conditional formulas scares them off of trying to understand them. It certainly did me at first, as I couldn't see a reliable way to know how many of them to use and where that wasn't just trial and error.
User avatar
moshi
Posts: 1740
Joined: November 13th, 2012, 9:53 pm

Re: is it dark now?

Post by moshi »

jsmorley wrote:"readability" of the formula.
yep, that's why i'm doing it. SciTE does a good job on that and at least for me this makes copy/paste, search/replace easier.