It is currently March 29th, 2024, 2:31 pm

[Solved] Convert 12h time to 24h

General topics related to Rainmeter.
bvx
Posts: 6
Joined: February 26th, 2013, 3:11 pm

[Solved] Convert 12h time to 24h

Post by bvx »

I'm using the Last Updated variable from weather.com's rss feed:

http://xml.weather.com/weather/local/UKXX0085?cc=*&unit=m&dayf=1

Here's an example:

Code: Select all

<lsup>2/26/13 1:50 PM Local Time</lsup>
I need to convert the above value to 24 hour format, i.e. 13:50

I don't think the rss feed has an option to display times in 24 format so I'll have to do it manually. Any suggestions?
Last edited by bvx on February 26th, 2013, 5:41 pm, edited 1 time in total.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Convert 12h time to 24h

Post by jsmorley »

I think this is simplest with a small Lua script:

Here is an example...

Test.ini

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1

[MeasureWeather]
Measure=Plugin
Plugin=WebParser
URL=http://xml.weather.com/weather/local/UKXX0085?cc=*&unit=m&dayf=1
RegExp="(?siU)<lsup>.* (.*) Local Time</lsup>"
FinishAction=!EnableMeasure MeasureScript

[MeasureTime]
Measure=Plugin
Plugin=WebParser
URL=[MeasureWeather]
StringIndex=1

[MeasureScript]
Measure=Script
ScriptFile=Test.lua
Disabled=1

[MeterTime]
Meter=String
MeasureName=MeasureScript
FontSize=13
FontColor=255,255,255,255
AntiAlias=1
Test.lua

Code: Select all

function Initialize()

	msMeasureTime = SKIN:GetMeasure("MeasureTime")
	
end

function Update()

	updateTime = msMeasureTime:GetStringValue()
	
	Hour, Minute, Meridiem = string.match(updateTime, '^(.-):(.-) (.-)$')
	Hour24 = ParseTwelveHourTime(tonumber(Hour), Meridiem)
	if tonumber(Hour24) < 10 then
		Hour24 = '0'..Hour24
	end
	
	SELF:Disable()
	return (Hour24..':'..Minute)
	
end	

function ParseTwelveHourTime(Hour, Meridiem)
	if (Meridiem == 'AM') and (Hour == 12) then
		Hour = 0
	elseif (Meridiem == 'PM') and (Hour < 12) then
		Hour = Hour + 12
	end
	return Hour
end
What this is doing is getting the time information you want from weather.com, with the format "9:25 AM" or "2:15 PM" or whatever it is, using WebParser as normal.

Then when WebParser is done, it is enabling a Lua script measure that does the following:

* Gets the value of that measure returning the time from the skin.
* Uses a string.match function to break the time into Hour, Minute and Meridiem (AMPM)
* Uses a function to check the Meridiem and change the Hour into 24-hour format as needed.
* Adds a leading "0" if needed.
* Disables itself so it is not using resources until needed again.
* Returns the time formatted as "09:25" or "14:15" (using the examples above)

The skin then uses the value of the script measure when displaying the time.
bvx
Posts: 6
Joined: February 26th, 2013, 3:11 pm

Re: Convert 12h time to 24h

Post by bvx »

jsmorley, that's exactly what I needed! thank you!!! :)
User avatar
GTI.H
Posts: 75
Joined: December 15th, 2018, 3:35 am

Re: Convert 12h time to 24h

Post by GTI.H »

jsmorley wrote: February 26th, 2013, 4:26 pm I think this is simplest with a small Lua script:

Here is an example...

Test.ini

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1

[MeasureWeather]
Measure=Plugin
Plugin=WebParser
URL=http://xml.weather.com/weather/local/UKXX0085?cc=*&unit=m&dayf=1
RegExp="(?siU)<lsup>.* (.*) Local Time</lsup>"
FinishAction=!EnableMeasure MeasureScript

[MeasureTime]
Measure=Plugin
Plugin=WebParser
URL=[MeasureWeather]
StringIndex=1

[MeasureScript]
Measure=Script
ScriptFile=Test.lua
Disabled=1

[MeterTime]
Meter=String
MeasureName=MeasureScript
FontSize=13
FontColor=255,255,255,255
AntiAlias=1
Test.lua

Code: Select all

function Initialize()

	msMeasureTime = SKIN:GetMeasure("MeasureTime")
	
end

function Update()

	updateTime = msMeasureTime:GetStringValue()
	
	Hour, Minute, Meridiem = string.match(updateTime, '^(.-):(.-) (.-)$')
	Hour24 = ParseTwelveHourTime(tonumber(Hour), Meridiem)
	if tonumber(Hour24) < 10 then
		Hour24 = '0'..Hour24
	end
	
	SELF:Disable()
	return (Hour24..':'..Minute)
	
end	

function ParseTwelveHourTime(Hour, Meridiem)
	if (Meridiem == 'AM') and (Hour == 12) then
		Hour = 0
	elseif (Meridiem == 'PM') and (Hour < 12) then
		Hour = Hour + 12
	end
	return Hour
end
What this is doing is getting the time information you want from weather.com, with the format "9:25 AM" or "2:15 PM" or whatever it is, using WebParser as normal.

Then when WebParser is done, it is enabling a Lua script measure that does the following:

* Gets the value of that measure returning the time from the skin.
* Uses a string.match function to break the time into Hour, Minute and Meridiem (AMPM)
* Uses a function to check the Meridiem and change the Hour into 24-hour format as needed.
* Adds a leading "0" if needed.
* Disables itself so it is not using resources until needed again.
* Returns the time formatted as "09:25" or "14:15" (using the examples above)

The skin then uses the value of the script measure when displaying the time.
It would be nice to have a script instead of having a script for each measure you want to convert.

Would it be possible to pass the "MeasureTime" parameter to the Lua script Test.lua?
User avatar
Brian
Developer
Posts: 2674
Joined: November 24th, 2011, 1:42 am
Location: Utah

Re: [Solved] Convert 12h time to 24h

Post by Brian »

GTI.H wrote: December 20th, 2018, 5:50 pm It would be nice to have a script instead of having a script for each measure you want to convert.

Would it be possible to pass the "MeasureTime" parameter to the Lua script Test.lua?
This thread is quite old...

While it is entirely possible to use inline lua to pass a value to a lua function and immediately display the return value...I would propose another way to parse this by utilizing the Timestamp and TimestampFormat functions of the Time measure.
https://docs.rainmeter.net/manual-beta/measures/time/#TimeStamp
https://docs.rainmeter.net/manual-beta/measures/time/#TimeStampFormat

Example:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1

[MeasureWeather]
Measure=Plugin
Plugin=WebParser
URL=http://xml.weather.com/weather/local/UKXX0085?cc=*&unit=m&dayf=1
RegExp="(?siU)<lsup>(.*) GMT</lsup>"
FinishAction=[!EnableMeasure MeasureTime][!ShowMeter MeterTime]
StringIndex=1

[MeasureTime]
Measure=Time
Timestamp=[MeasureWeather]
TimestampFormat=%#m/%#d/%#Y %I:%M %p
Format=%#D %#H:%M
DynamicVariables=1
Disabled=1

[MeterTime]
Meter=String
MeasureName=MeasureTime
FontSize=13
FontColor=255,255,255,255
SolidColor=0,0,0,1
AntiAlias=1
Hidden=1
Seems a little easier than having the extra script. Of course you would need to have a separate Webparser and Time measure for each date you want to convert.

(I will let jsmorley respond with the inline lua script version. :Whistle :D )

-Brian
User avatar
GTI.H
Posts: 75
Joined: December 15th, 2018, 3:35 am

Re: [Solved] Convert 12h time to 24h

Post by GTI.H »

Brian wrote: December 21st, 2018, 7:45 am This thread is quite old...

While it is entirely possible to use inline lua to pass a value to a lua function and immediately display the return value...I would propose another way to parse this by utilizing the Timestamp and TimestampFormat functions of the Time measure.
https://docs.rainmeter.net/manual-beta/measures/time/#TimeStamp
https://docs.rainmeter.net/manual-beta/measures/time/#TimeStampFormat

Seems a little easier than having the extra script. Of course you would need to have a separate Webparser and Time measure for each date you want to convert.

(I will let jsmorley respond with the inline lua script version. :Whistle :D )

-Brian
Thanks Brian!

I avoid using scripts.

I managed to solve this issue with the Measure Time and Uptime that jsmorley showed me to solve another issue but, I will study this your method that seems better than my solution.

* Edit:
I studied your method and it made me reduce two Measure.
Thank you! :thumbup:
Last edited by GTI.H on December 23rd, 2018, 8:42 pm, edited 1 time in total.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: [Solved] Convert 12h time to 24h

Post by jsmorley »

Of course the hours, minutes and seconds in any time is an entirely different animal than converting a number of seconds into a number of hours, minutes and seconds.
User avatar
GTI.H
Posts: 75
Joined: December 15th, 2018, 3:35 am

Re: [Solved] Convert 12h time to 24h

Post by GTI.H »

These two Measures that I managed to reduce were introduced by me and not by the jsmorley method.

I had created Measure just to convert, for example 05:58:05 and 19:41:05 to 5:58 and 19:41 that were replaced with Format =%#H:%M lines on measure created by jsmorley for my needs.

Thanks jsmorley! :thumbup:

Code: Select all

[MSSunRTimestamp]
Group=WPCActualTodayData
Measure=Time
TimeStamp=[MSSunR]
TimeStampFormat=%#I:%M %p
Format=%#H:%M
UpdateDivider=-1
DynamicVariables=1
Disabled=1

[MSSunSTimestamp]
Group=WPCActualTodayData
Measure=Time
TimeStamp=[MSSunS]
TimeStampFormat=%#I:%M %p
Format=%#H:%M
UpdateDivider=-1
DynamicVariables=1
Disabled=1