It is currently February 16th, 2019, 2:18 am

Strings and Numbers : Time and Uptime measures

Our most popular Tips and Tricks from the Rainmeter Team and others
User avatar
jsmorley
Developer
Posts: 18819
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Strings and Numbers : Time and Uptime measures

jsmorley » September 20th, 2014, 1:46 pm

One of the less known capabilities of the Time and Uptime measures is the ability to use number values returned by them to do some "date math". This is just a simple example that I hope demonstrates how things work, which you can use in your own skins.

First, let's look at the values returned by each of the measure types:


Measure=Time

The string value returned will be based on the Format option that you define. You do this by using the Time Codes mixed with any text you want, to format the date / time string you desire.

Code: Select all

[MeasureTime]
Measure=Time
Format=%A, %B %#d, %Y at %#I:%M %p
This might return: "Saturday, September 20, 2014 at 8:11 AM"

The number value returned will depend a bit.

If you specify a Format option, and that option resolves to a purely "numeric" string, the number value will also be that number.

Code: Select all

[MeasureTime]
Measure=Time
Format=%#I
Both the string and number values will be "8" if the time was the same as our example above.

If you specify a Format option, and that option does not resolve to a purely "numeric" string, the number value of the measure will be zero. If the format resolves to a string that starts with a number, that number will be the numeric value.

Code: Select all

[MeasureTime]
Measure=Time
Format=The hour is: %#I
The string value will be "The hour is 8" and the number value will be 0.

If you do not specify a Format option, then the string format will default to %H:%M:%S or "24Hours:Minutes:Seconds". The number value will be a Windows Timestamp value, which is the number of seconds the current time is since January 1, 1601.

Code: Select all

[MeasureTime]
Measure=Time
The string value would be "08:11:42", and the number value would be 13055675615.

In order to do "date math" with a Time measure, you are going to want to use that Windows Timestamp value. The easiest way to do this is to not use the numeric value of the measure at all, which as you can see can conflict and cause confusion with the Format option on the measure. Instead, you will want to use the value of the measure as a Section Variable, with the :Timestamp modifier on it.

Code: Select all

[MeasureNow]
Measure=Time
Format=%A, %B %#d, %Y at %#I:%M %p

[MeasureYesterday]
Measure=Calc
Formula=([MeasureNow:Timestamp] - 86400)
DynamicVariables=1
[MeassureNow] will return a string value of "Saturday, September 20, 2014 at 8:11 AM" and a number value of 0.

[MeasureYesterday] will use the Timestamp value of [MeasureNow] in the formula, 13055675615 in this example, and subtract 86400 seconds, or one day.

The Timestamp option

The Time measure has a TimeStamp option that will allow you to specify the time you want the measure to use. This can just be a number corresponding to a valid Windows Timestamp, like our 13055675615 above, or it can be a formula that creates a valid Windows Timestamp. Note that this is not the same as the :Timestamp modifier on a section variable. That is used to "get" the timestamp value, this is used to "set" it.

Code: Select all

[MeasureNow]
Measure=Time
Format=%A, %B %#d, %Y at %#I:%M %p

[MeasureYesterday]
Measure=Time
TimeStamp=([MeasureNow:Timestamp] - 86400)
Format=%A, %B %#d, %Y at %#I:%M %p
DynamicVariables=1
Note that we use the :Timestamp value of the measure MeasureNow by using it as a Section Variable, with the :Timestamp modifier on it.

[MeasureNow] = String value of the Time measure.
[MeasureNow:Timestamp] = Timestamp value of the Time measure.

Code: Select all

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

[MeasureNow]
Measure=Time
Format=%A, %B %#d, %Y at %#I:%M %p

[MeasureYesterday]
Measure=Time
TimeStamp=([MeasureNow:Timestamp] - 86400)
Format=%A, %B %#d, %Y at %#I:%M %p
DynamicVariables=1

[MeterTodayAndYesterday]
Meter=String
FontSize=11
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Now: [MeasureNow]#CRLF#Yesterday: [MeasureYesterday]
DynamicVariables=1
03.png

Measure=Uptime

The string value returned will be based on the Format option that you define. You do this by using the Uptime Codes mixed with any text you want, to format the uptime string you desire.

Code: Select all

[MeasureUptime]
Measure=Uptime
Format="%4!i! days, %3!i! hours, %2!i! minutes %1!i! seconds"
Which will return "0 days, 19 hours, 35 minutes 23 seconds" as the string value.

The number value will always be the number of seconds that have elapsed since the system was restarted. In our example this would be 70686 seconds ago.

Code: Select all

[MeasureUptime]
Measure=Uptime
Format="%4!i! days, %3!i! hours, %2!i! minutes %1!i! seconds"

[MeterUptime]
Meter=String
FontSize=11
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=System restarted [MeasureUptime] ago#CRLF#That is [MeasureUptime:] seconds
DynamicVariables=1
01.png

Putting it together

So you might want to use the value returned by the Uptime measure, but instead of displaying how long ago that was, maybe you want to display the date and time the system was restarted at.

We can do that fairly easily by using the capabilities of the Time and Uptime measures we discussed above.

First, let me show the code you might use, then explain a bit.

Code: Select all

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

[MeasureUptime]
Measure=Uptime

[MeasureCurrentTimeNumber]
Measure=Time

[MeasureCurrentTimeFormat]
Measure=Time
Format=%A, %B %#d, %Y at %#I:%M %p

[MeasureRebootTime]
Measure=Time
TimeStamp=([MeasureCurrentTimeNumber:] - [MeasureUptime:])
Format=%A, %B %#d, %Y at %#I:%M %p
DynamicVariables=1

[MeterOne]
Meter=String
FontSize=11
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Now: [MeasureCurrentTimeFormat]#CRLF#Restart: [MeasureRebootTime]
DynamicVariables=1
02.png
[MeasureUptime]
This just gets the uptime with no Format option, which will return a number value of the number of seconds since the system was restarted. It will return a default string value as well, but we won't be using that.

[MeasureCurrentTimeNumber]
This will get the current time, with a number value that is the current Windows Timestamp. It will also return a default string value, but we won't be using that either.

[MeasureCurrentTimeFormat]
This returns the current time with a string value we have nicely formatted as we want using the Time Codes and some text.

[MeasureRebootTime]
This is sort of the meat-and-potatoes of this skin. What we are doing here is getting a nicely formatted date / time string. However, we are using the TimeStamp option, with a formula, that will tell the Time measure to use a time different from the current, and nicely format the time the system was restarted.

Hope this help with an understanding of how string and number values work in the Time and Uptime measures, and gives you some ideas for your own skins.
You do not have the required permissions to view the files attached to this post.
User avatar
eclectic-tech
Rainmeter Sage
Posts: 3197
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Strings and Numbers : Time and Uptime measures

eclectic-tech » September 21st, 2014, 4:59 pm

Great examples as always! :thumbup:

I do need some help though...

I am working on a skin that gets the time from the webparser in string format of: HH:MM AM/PM

I want to do calculations that will find times slightly before the time string from the webpage, and also calculate whether it is day or night.

To accomplish this, I have created string measures with RegExpSubstitution (novice at going that) to extract the hours and minutes from the webpage string, then created calc measures to create total minutes from those. I use time measures formatted %H*60+%M for comparison.

With these I can create a measure 1 hour prior, but not anytime shorter without more calculations. :(

All-in-all it seems like a lot of measures to do a few comparisons and calculations...

So I am wondering if you have a better way to convert AM/PM formatted time strings into a number value that can be added or subtracted from the downloaded time to achieve relative times and for comparisons to current time? Hopefully something simple I overlooked...

Code: Select all

[mWeatherRSS]
Measure=Plugin
Plugin=Plugins\WebParser.dll
UpdateRate=86400
; Test file... downloaded from www.weather. for my location and temperature unit
; If the skin is not working,change to this Url and run
; If it works, then you have an issues getting sunrise/sunset info from www.weather.com
;Url=file://#CURRENTPATH#milanweather.txt
Url=http://xml.weather.com/weather/local/#location#?cc=*&unit=#unit#&dayf=6
RegExp="(?siU).*<sunr>(.*)</sunr>.*<suns>(.*)</suns>.*"
StringIndex=1
FinishAction=[!EnableMeasure mDayNight][!Update *][!DisableMeasure mDayNight]
; FinishAction checks the current time against sunrise/sunset and sets the current condition to day/night
 
[mWeatherSunR]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[mWeatherRSS]
StringIndex=1
; Times are not in 24 hour format, so additional measure are needed to convert the times into total minutes for comparisons

[mPreDawnHour]
Measure=Calc
Formula=(mWeatherSunR-1)
DynamicVariables=1
; Calculates the hour for sunrise and sets the pre-dawn time to 1 hour before sunrise

[mPreDawnMinute]
Measure=String
String=[mWeatherSunR]
RegExpSubstitute=1
Substitute="^..(...)(.*)$":"\1",":":""," ":""
DynamicVariables=1
; Separates the minutes from the sunrise time string

[mPreDawnTime]
Measure=String
String=HH:MM AM
RegExpSubstitute=1
Substitute="HH":"[mPreDawnHour]","MM":"[mPreDawnMinute]"
DynamicVariables=1
; Creates a string to use for the start of sunrise action

[mRiseMinutes]
Measure=Calc
Formula=((mWeatherSunR*60)+mPreDawnMinute)
DynamicVariables=1
; Calculates the total minutes for comparison to start the sunrise action

[mWeatherSunS]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[mWeatherRSS]
StringIndex=2
; Times are not in 24 hour format, so additional measure are needed to convert the times into total minutes for comparisons

[mTwilightHour]
Measure=Calc
Formula=(mWeatherSunS+11)
; Calculates the hour for sunset and sets the twilight time to 1 hour before sunset

[mTwilightMinute]
Measure=String
String=[mWeatherSunS]
RegExpSubstitute=1
Substitute="^..(...)(.*)$":"\1",":":""," ":""
DynamicVariables=1
; Separates the minutes from the sunset time string

[mTwilightTime]
Measure=String
String=HH:MM PM
RegExpSubstitute=1
Substitute="HH":"[mTwilightHour]","MM":"[mTwilightMinute]"
DynamicVariables=1
; Creates a string to use for the start of sunset action

[mSetMinutes]
Measure=Calc
Formula=(((mWeatherSunS+12)*60)+mTwilightMinute)
DynamicVariables=1
; Calculates the total minutes for comparison to start the sunset action

[mTimeAMPM]
Measure=Time
Format=%#H:%M %p
; Time in Imperial format for comparison to webParser measures

[mHour]
Measure=Time
Format=%#H
; Returns the current hour

[mMinutes]
Measure=Time
Format=%M
; Returns the current minutes

[mTimeMinutes]
Measure=Calc
Formula=mHour*60+mMinutes
; Calculates the total minutes for the current time for comparison to activate sunrise/sunset

[mDayNight]
Measure=Calc
Formula=(mTimeMinutes>mSetMinutes)? 49:((mTimeMinutes<mRiseMinutes)? 49:0)
OnUpdateAction=[!SetVariable ImageNum [mDayNight]][!WriteKeyValue Variables ImageNum [mDayNight] "#@#UserVariables.inc"][!Redraw]
DynamicVariables=1
Disabled=1
; Runs only on load, refresh, or when the website is accessed... 
; Sets the background image based on either day (0)or night (49)

This code works, but I have had it crash Rainmeter several times, when a !Refresh * is done from another skin...

Any help is really appreciated... Thanks!
User avatar
moshi
Posts: 1851
Joined: November 13th, 2012, 9:53 pm

Re: Strings and Numbers : Time and Uptime measures

moshi » September 21st, 2014, 9:54 pm

eclectic-tech wrote:
Any help is really appreciated... Thanks!
here's a snippet of what i am using with this skin:
http://customize.org/rainmeter/skins/65673356

Code: Select all

[MeasureLat]
Measure=Plugin
Plugin=WebParser.dll
URL=http://weather.yahooapis.com/forecastrss?w=#Location#&u=f
RegExp=(?siU).*<yweather.*<yweather:astronomy sunrise="(.*)".*sunset="(.*)".*</geo:long>
UpdateRate=3600

[MeasureSunRise]
Measure=Plugin
Plugin=WebParser.dll
URL=[MeasureLat]
StringIndex=1
RegExpSubstitute=1
Substitute="^$":"06:00 am","(.*):(.*) (.*)":"((\1*15)+(\2*0.25) \3"," am":"+0)"," pm":"+180)"

[MeasureSunSet]
Measure=Plugin
Plugin=WebParser.dll
URL=[MeasureLat]
StringIndex=2
RegExpSubstitute=1
Substitute="^$":"06:00 pm","(.*):(.*) (.*)":"((\1*15)+(\2*0.25) \3"," am":"+0)"," pm":"+180)"

[MeasureTimeBase]
Measure=Time
Format="((%H*15)+(%M*0.25))"
DynamicVariables=1
IfCondition=([MeasureTimeBase] >= [MeasureSunRise]) && ([MeasureTimeBase] < [MeasureSunSet])
IfTrueAction=[!SetOptionGroup Display3 FontColor 505050][!SetOptionGroup Display2 BarColor 808080][!SetOptionGroup Display1 SolidColor ffffffc4][!UpdateMeterGroup Display1][!UpdateMeterGroup Display2][!UpdateMeterGroup Display3][!Redraw]
IfFalseAction=[!SetOptionGroup Display3 FontColor ffffff][!SetOptionGroup Display2 BarColor fafafa][!SetOptionGroup Display1 SolidColor 000000c4][!UpdateMeterGroup Display1][!UpdateMeterGroup Display2][!UpdateMeterGroup Display3][!Redraw]
this is using Yahoo! Weather. so you probably would have to use AM instead of am
User avatar
eclectic-tech
Rainmeter Sage
Posts: 3197
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Strings and Numbers : Time and Uptime measures

eclectic-tech » September 22nd, 2014, 12:14 am

Thanks moshi! I will try your substitutions...

Mine was designed by trial and error... so I appreciate your help!

Edit: Nice idea to convert times to degrees on a 24 hour clock and format the time the same...

In your skin, anything after 6:00 am is day and after 6:00 pm is night, not exactly what I was looking for, but you have given me another way to work with this and I appreciate the suggestion!
User avatar
moshi
Posts: 1851
Joined: November 13th, 2012, 9:53 pm

Re: Strings and Numbers : Time and Uptime measures

moshi » September 22nd, 2014, 12:40 pm

eclectic-tech wrote: In your skin, anything after 6:00 am is day and after 6:00 pm is night, not exactly what I was looking for, but you have given me another way to work with this and I appreciate the suggestion!
are you sure you are:
• connected to the internet?
• using a Yahoo! WOEID?
User avatar
eclectic-tech
Rainmeter Sage
Posts: 3197
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Strings and Numbers : Time and Uptime measures

eclectic-tech » September 22nd, 2014, 3:13 pm

moshi wrote: are you sure you are:
• connected to the internet?
• using a Yahoo! WOEID?
It is very possible I just need to get a better understanding of RegExp.

Does Substitute="^$":"06:00 am","(.*):(.*) (.*)":"((\1*15)+(\2*0.25) \3"," am":"+0)"," pm":"+180)" substitute the entire string with '06:00 am' and then calculate the degrees? ...

Your example really help me to simplify my calculations and I do appreciate your help!
It is working with your help!!
User avatar
jsmorley
Developer
Posts: 18819
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Strings and Numbers : Time and Uptime measures

jsmorley » September 22nd, 2014, 3:20 pm

eclectic-tech wrote: Does Substitute="^$":"06:00 am","(.*):(.*) (.*)":"((\1*15)+(\2*0.25) \3"," am":"+0)"," pm":"+180)" substitute the entire string with '06:00 am' and then calculate the degrees? ...
The "^$":"06:00 am" bit simply means "if the result is an empty string, (^ = start of string, $ = end of string) then replace the entire thing with 06:00 am". So that creates a "default" value if nothing is returned.
User avatar
eclectic-tech
Rainmeter Sage
Posts: 3197
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Strings and Numbers : Time and Uptime measures

eclectic-tech » September 22nd, 2014, 3:28 pm

@jsmorley

Ah-ha... Thanks for the information and help!

@moshi

Thanks again for your details on converting string time to degrees.

With that I was able to eliminate 6 measures and simplify the skin.

Sorry for the misunderstanding... I said I was a novice at RegExp!

Thank you again, very much!