It is currently March 29th, 2024, 1:03 pm

Adding, Subtracting, Comparing Times

Get help with creating, editing & fixing problems with skins
User avatar
Christopher Robin
Posts: 14
Joined: January 20th, 2020, 3:55 pm

Adding, Subtracting, Comparing Times

Post by Christopher Robin »

Given a time in the format 24:00, what is the simplest way to 1) establish the value "That time, minus thirty minutes", and 2) get a value in minutes between the current actual time and "Tx - 30"?

I've been wrestling with this for far too long without sleep, my eyeballs are fried and I've developed a grudge against mathematics itself for the facts that 715 - 30 =/= 645 and 2338 stubbornly insists on being greater than 0017 even after sunset. I have assembled several methods to produce the desired result, but my best so far needs seven different measures* to convert the time values to plain numbers before I even get to the formulas using them... all on top of the the WebParser to get Tx in the first place.

I find it hard to believe I'm not missing a better way. Moreover, I have the strong suspicion that getting a better understanding of this specific problem will significantly deepen my understanding of what I'm doing with Rainmeter in general.

Any suggestions?



--------------------------------------------------

* 1: get Tx H, 2: get Tx M, 3: (H * 60) + M = X, 4: X - 30 (I could do (H * 60) + (M - 30) in one calc, but I need X on its own for separate purposes), 5: get Now H, 6: get Now M, 7: (NH * 60) + NM = Y -- if you must know.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Adding, Subtracting, Comparing Times

Post by jsmorley »

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]

[MeasureNow]
Measure=Time
Format=%H:%M

[MeasureLater]
Measure=Time
TimeStamp=24:00
TimeStampFormat=%H:%M
Format=%#I:%M %p

[MeasureEarlier]
Measure=Time
TimeStamp=([MeasureLater:TimeStamp]-1800)
Format=%#I:%M %p
DynamicVariables=1

[MeasureDiffInMinutes]
Measure=Calc
DynamicVariables=1
Formula=(([MeasureLater:TimeStamp]-[MeasureEarlier:TimeStamp])/60)

[MeasureNowHHMM]
Measure=Time
TimeStamp=[MeasureNow]
TimeStampFormat=%H:%M
Format=%#I:%M %p
DynamicVariables=1

[MeasureDiffFromNow]
Measure=Calc
DynamicVariables=1
Formula=(([MeasureNowHHMM:TimeStamp] - [MeasureEarlier:TimeStamp])/60)

[MeasureDiffUntilNow]
Measure=Calc
DynamicVariables=1
Formula=(([MeasureEarlier:TimeStamp] - [MeasureNowHHMM:TimeStamp])/60)

[MeterOutput]
Meter=String
FontSize=13
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=[MeasureEarlier] is [MeasureDiffInMinutes] minutes earlier than [MeasureLater]

1.png



https://docs.rainmeter.net/manual/measures/time/#TimeStamp
https://docs.rainmeter.net/manual/measures/time/#TimeStampFormat

Getting the difference between "now" and some arbitrary time isn't really possible, as you don't know what "day" that arbitrary time refers to. Is it that time "from now", or that time "until now"? That time a week ago, or that time a hundred years from now?
You do not have the required permissions to view the files attached to this post.
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5384
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Adding, Subtracting, Comparing Times

Post by eclectic-tech »

Beaten again!

Always good to have options...

A similar methods, this would convert the STRING value of a webparser measure using RegExpSubstitute, to obtain the number of minutes, then multiply by 60 to get total seconds in a Calc measure, and do any math you want.

Code: Select all

; Add the 'RegExpSubstitute=1' and 'Substitute=...' lines to your webparser measure that returns the time.

[MeasureString]
Measure=String
String=23:25
RegExpSubstitute=1
Substitute="^$":"06:00","(.*):(.*)":"(\1*60)+\2"
; Substitute converts hours into minutes and adds the minutes value, to return the number of minutes for this time (0~1440) as a string.
; You need to use the string value in a calc measure to do any math on the value.

[MeasureCalc]
Measure=Calc
Formula=([MeasureString]-30)*60
; There are 1440 minutes in one day
; Times 60 equals 86400 seconds in one day

[MeasureTime]
Measure=Time
TimeStamp=[MeasureCalc:]
DynamicVariables=1

[MeterString]
Meter=String
MeasureName=MeasureTime
Text=%1 is 30 minutes earlier
SolidColor=164,164,164
Padding=5,5,5,5
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Adding, Subtracting, Comparing Times

Post by jsmorley »

Yes. The tricky bit is the relationship between that arbitrary time and "now". Certainly doable, but you have to decide if you mean that time in the past or that time in the future. The answer can be anywhere from -1400 minutes to +1400 minutes.
User avatar
Yincognito
Rainmeter Sage
Posts: 7029
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Adding, Subtracting, Comparing Times

Post by Yincognito »

This is almost complete, according to the "requirements":

Code: Select all

[Variables]
;TimeString1="02.01.2020 00:17:00"
;TimeString2="01.01.2020 23:38:00"
TX="00:17"

[Rainmeter]
DynamicWindowSize=1
AccurateText=1

---Measures---

[MeasureTX]
Measure=Time
Format=%H:%M
TimeStamp=#TX#
TimeStampFormat=%H:%M

[MeasureTXMinus30Min]
Measure=Time
Format=%H:%M
TimeStamp=([MeasureTX:TimeStamp]-1800)

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

[MeasureCurrentTimeMinus_TXMinus30Min]
Measure=Time
Format=%H:%M
TimeStamp=([MeasureCurrentTime:TimeStamp]-[MeasureTXMinus30Min:TimeStamp])

[MeasureFinalResultInMin]
Measure=Uptime
SecondsValue=[MeasureCurrentTimeMinus_TXMinus30Min:]
Format=%2!010i!
DynamicVariables=1

---Meters---

[MeterTimeDifference]
Meter=String
FontFace=Consolas
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
MeasureName=MeasureTXMinus30Min
MeasureName2=MeasureCurrentTime
MeasureName3=MeasureCurrentTimeMinus_TXMinus30Min
MeasureName4=MeasureFinalResultInMin
Text="TX - 30 Minutes = %1#CRLF#CurrentTime = %2#CRLF#CurrentTime - (TX - 30 Minutes) = %3#CRLF#Final Result In Minutes = %4"
Where jsmorley could help is to enlighten us how you can get the number of minutes (like 204 minutes) from a %H:%M time format, i.e. 03:24, aka 3h and 24 min, without using a Calc measure and without "breaking" %H:%M into 2 measures getting the hours and minutes separately. If possible, of course.

And yes, you'd need to also know the date, not just the time, in order to see if a certain time is before or after another - like jsmorley pointed out. My first 2 commented lines are a leftover from such an attempt... :D

EDIT: Beaten by two, not just one. I getting really rusty, LMAO.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Adding, Subtracting, Comparing Times

Post by jsmorley »

Yincognito wrote: February 22nd, 2020, 5:09 am
Where jsmorley could help is to enlighten us how you can get the number of minutes (like 204 minutes) from a %H:%M time format, i.e. 03:24, aka 3h and 24 min, without using a Calc measure and without "breaking" %H:%M into 2 measures getting the hours and minutes separately. If possible, of course.

Code: Select all

[MeasureNow]
Measure=Time
Format=%H:%M

[MeasureNowHHMM]
Measure=Time
TimeStamp=[MeasureNow]
TimeStampFormat=%H:%M
Format=%#I:%M %p
DynamicVariables=1
Edit: Sorry, I misunderstood your question. Not sure right off, I do think it would probably take two Time measures and a Calc measure. Or at least one Time measure and a Calc measure...

A "timestamp" is the number of elapsed seconds since January 1, 1601. Comparing and getting the difference between two timestamps is easy, but if you are just getting the "time" and not a "timestamp", you have to turn it into hours and minutes and turn both into seconds. The lowest-common-denominator for time is "seconds" in Rainmeter.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Adding, Subtracting, Comparing Times

Post by jsmorley »

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]

[MeasureNow]
Measure=Time
Format=%#I:%M %p

[MeasureNowAsNumber]
Measure=Time
Format=%H.%M

[MeasureNowAsMinutes]
Measure=Calc
DynamicVariables=1
Formula=((Trunc([MeasureNowAsNumber])*3600) + ((Frac([MeasureNowAsNumber])*100)*60)/60)

[MeterDisplay]
Meter=String
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=The current time [MeasureNow] is [MeasureNowAsMinutes] minutes from midnight

1.png
You do not have the required permissions to view the files attached to this post.
User avatar
Yincognito
Rainmeter Sage
Posts: 7029
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Adding, Subtracting, Comparing Times

Post by Yincognito »

jsmorley wrote: February 22nd, 2020, 5:31 am

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]

[MeasureNow]
Measure=Time
Format=%#I:%M %p

[MeasureNowAsNumber]
Measure=Time
Format=%H.%M

[MeasureNowAsMinutes]
Measure=Calc
DynamicVariables=1
Formula=((Trunc([MeasureNowAsNumber])*3600) + ((Frac([MeasureNowAsNumber])*100)*60)/60)

[MeterDisplay]
Meter=String
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=The current time [MeasureNow] is [MeasureNowAsMinutes] minutes from midnight


1.png
Yep, I think this method might be the shortest one to do that. Since the OP mentioned the fact that he had to write several (as in too many and too ineffective) measures earlier to do the whole thing...

Update: Or, to avoid having to change : into . in order to be able to display the intermediary results without addditional measures, one could adapt a bit balala's idea, like so:

Code: Select all

[Variables]
;TimeString1="02.01.2020 00:17:00"
;TimeString2="01.01.2020 23:38:00"
TX="00:17"

[Rainmeter]
DynamicWindowSize=1
AccurateText=1

---Measures---

[MeasureTX]
Measure=Time
Format=%H:%M
TimeStamp=#TX#
TimeStampFormat=%H:%M

[MeasureTXMinus30Min]
Measure=Time
Format=%H:%M
TimeStamp=([MeasureTX:TimeStamp]-1800)

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

[MeasureCurrentTimeMinus_TXMinus30Min]
Measure=Time
Format=%H:%M
TimeStamp=([MeasureCurrentTime:TimeStamp]-[MeasureTXMinus30Min:TimeStamp])

[MeasureFinalResultInMinString]
Measure=String
String=[MeasureCurrentTimeMinus_TXMinus30Min]
RegExpSubstitute=1
Substitute="^(\d+):(\d+)$":"\1*60+\2"

[MeasureFinalResultInMin]
Measure=Calc
Formula=[MeasureFinalResultInMinString]

---Meters---

[MeterTimeDifference]
Meter=String
FontFace=Consolas
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
MeasureName=MeasureTXMinus30Min
MeasureName2=MeasureCurrentTime
MeasureName3=MeasureCurrentTimeMinus_TXMinus30Min
MeasureName4=MeasureFinalResultInMin
Text="TX - 30 Minutes = %1#CRLF#CurrentTime = %2#CRLF#CurrentTime - (TX - 30 Minutes) = %3#CRLF#Final Result In Minutes = %4"
Preview
Dates.jpg
You do not have the required permissions to view the files attached to this post.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
Christopher Robin
Posts: 14
Joined: January 20th, 2020, 3:55 pm

Re: Adding, Subtracting, Comparing Times

Post by Christopher Robin »

Wow, thanks to everyone for the swift and helpful replies! I'd explain exactly what I've learned from the code examples presented here thus far, but I'm embarrassed to reveal what I didn't already know. In any case, I think I see ways to considerably trim my existing structure using what I've learned tonight.

As far as comparing Now to Tx, this is the heart of that interaction:

Code: Select all

[TimeSplitter]
Measure=Calc
Formula=([Now] >= [SunsetEnd]) || ([Now] < [SunriseBegin]) ? 1 : (([Now] >= [SunriseBegin]) && ([Now] < [SunriseAbsolute]) ? 2 : (([Now] >= [SunriseAbsolute]) && ([Now] < [SunriseEnd]) ? 3 : (([Now] >= [SunriseEnd]) && ([Now] < [SunsetBegin]) ? 4 : (([Now] >= [SunsetBegin]) && ([Now] < [SunsetAbsolute]) ? 5 : 6))))
DynamicVariables=1
; 1:Night 2:SunriseIn 3:SunriseOut 4:Day 5:SunsetIn 6:SunsetOut
All the parts that actually compare Now to another value start out by asking "Is it Night? Is it SunsetIn time?" etc.

What I came in here looking for was a way to shorten the amount of lines it took to define [Now], [SunriseBegin], etc... and I've definitely got a much better grasp on how to go about that now. Cheers, everyone! :D
User avatar
Yincognito
Rainmeter Sage
Posts: 7029
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Adding, Subtracting, Comparing Times

Post by Yincognito »

Christopher Robin wrote: February 22nd, 2020, 6:29 am1:Night 2:SunriseIn 3:SunriseOut 4:Day 5:SunsetIn 6:SunsetOut
So, if you have those ins and outs, you must be querying some astonomical/nautical/civil times at dawn and dusk, if I'm not mistaken. Hehe, it reminds me of my early days with Rainmeter (not that long ago) and the ambition to put the exact amount of light, sun, moon and clouds from the sky on my computer screen... :D

I was so close to make it happen ... until the Poles changed my mood completely. Balala knows... that's how great projects begin and end (or rise in and set out, to match the topic here). ;-)
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth