It is currently November 18th, 2019, 10:03 am

Converting EPOCH time

Discuss the use of Lua in Script measures.
User avatar
Swiftzn
Posts: 13
Joined: December 13th, 2017, 9:07 am

Converting EPOCH time

Swiftzn » December 15th, 2017, 11:09 am

Hey guys,

Trying to convert EPOCH time to normal output
i am using the function found Here

Script measure

Code: Select all

[MeasureConvertTime]
Measure=Script
ScriptFile=ConvertTime.lua
Disabled=1
Measure that Holds my time Variable

Code: Select all

[MeasureTXOneTime]
Measure=Plugin
Plugin=WebParser
URL=[MeasureTransactions]
StringIndex=1
Meter that should display my Windows time(if i have sussed this out correctly

Code: Select all

[MeterTXOneTime]
Meter=String
Text=[&MeasureConvertTime:ConvertTime([&MeasureTXOneTime]
X=230
Y=20r
W=70
H=50
And this is the error i keep getting

Script: ConvertTime.lua:4: '}' expected (to close '{' at line 2) near 'Windows'

Thanks in advance
User avatar
balala
Rainmeter Sage
Posts: 9083
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Converting EPOCH time

balala » December 15th, 2017, 12:52 pm

Would be good to know the whole code. So, please pack your config and upload it.
User avatar
Swiftzn
Posts: 13
Joined: December 13th, 2017, 9:07 am

Re: Converting EPOCH time

Swiftzn » December 15th, 2017, 1:11 pm

Ho K

See attached
You do not have the required permissions to view the files attached to this post.
User avatar
balala
Rainmeter Sage
Posts: 9083
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Converting EPOCH time

balala » December 15th, 2017, 3:38 pm

Swiftzn wrote:Ho K
I think you don't need the lua function you tried to use. That function converts the Unix epoch timestamps into Windows timestamps, which is not exactly what you need. I think instead you'd like to convert the timestamp returned by the [MeasureTXOneTime] WebParser measure of your code to usual date and time (like 12/15/2017 7:00:00 - this is just an example, has nothing to do with the value returned for example right now). Am I right?
If I am, here is the solution: first you've disabled the [MeasureConvertTime] script measure of your code (with a Disabled=1 option). Forget it and remove this Disabled=1 option. Otherwise you have to enable the measure, when it should make the conversion.
Secondly, you've added to the lua code just the ConvertTime function, nothing else. This way the lua code won't interact with the skin where it is used. You have two way to make this happening: through the Initialize() (which is executed just once, when the skin is loaded or refreshed), respectively through the Update() (executed on each update cycle of the skin) functions.
Here is the rewritten lua code. Please replace your old code with this one:

Code: Select all

function Update()
	TXOneTime = SKIN:GetMeasure('MeasureTXOneTime')
	TXOneTimeNum = TXOneTime:GetValue()
	local Year, Month, Day, Hour, Minute, Second = os.date("%Y", TXOneTimeNum), os.date("%m", TXOneTimeNum), os.date("%d", TXOneTimeNum), os.date("%H", TXOneTimeNum), os.date("%M", TXOneTimeNum), os.date("%S", TXOneTimeNum)
	return Month..'/'..Day..'/'..Year..' '..Hour..':'..Minute..':'..Second
end

--function ConvertTime(n, To)
--	local Formats = {
--		Unix    = -1
--		Windows = 1
--		}
--	return Formats[To] and n + 11644473600 * Formats[To] or nil
--end
See I left there the ConvertTime function, but it is commented out, so won't be used (but don't even need it any more).
The first two lines of the Update() function get the value returned by the [MeasureTXOneTime] (first line) and convert it to numeric format (second line). In the third line I introduced six variable, which will keep the year, month, day, hour, minute and second of the timestamp, accordingly. The last line returns these value through the Month/Day/Year Hour:Minute:Second format. You can modify this format, if you need it in any other form (and I suppose you'll need to modify it). See here how can you change the format of the returned variable.
Now the last step would be to use the returned value into your skin. If you did all the above modification to both the code of the skin and to the lua code, the [MeasureConvertTime] Script measure returns the date in the mentioned form. You'll have to use its value: add the MeasureName=MeasureConvertTime option to the [MeterTXOneTime] meter and replace its Text option with Text=%1.
I think here is one more problem: it seems the above string is going outside of the skin (to right). Reduce the X value of it. Eg try with X=20.

Please let me know if this is what you needed.
User avatar
Swiftzn
Posts: 13
Joined: December 13th, 2017, 9:07 am

Re: Converting EPOCH time

Swiftzn » December 15th, 2017, 3:53 pm

:twisted: Thanks for all the Answer.

So the returned value of the [MeasureTXOneTime] is a Unix Epoch time stamp.
So would you lua code work with that?

I will try this anyway thanks you so much will report back if it works

Update
Yup that worked perfectly, thank you very much.
Owe you one again haha :D
Last edited by Swiftzn on December 15th, 2017, 4:01 pm, edited 1 time in total.
User avatar
balala
Rainmeter Sage
Posts: 9083
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Converting EPOCH time

balala » December 15th, 2017, 3:55 pm

Swiftzn wrote:Thanks for all the Answer.

So the returned value of the [MeasureTXOneTime] is a Unix Epoch time stamp.
So would you lua code work with that?

I will try this anyway thanks you so much will report back if it works
Try it and see how it works. Please let me know if something is not ok.
User avatar
Swiftzn
Posts: 13
Joined: December 13th, 2017, 9:07 am

Re: Converting EPOCH time

Swiftzn » December 15th, 2017, 4:01 pm

Works a treat.

there is a slight lag when you force refresh the skin, but i assume that can be fixed by putting an initialze function in?
No biggie really
User avatar
jsmorley
Developer
Posts: 19725
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Converting EPOCH time

jsmorley » December 15th, 2017, 4:07 pm

Seems to me that you want to get the unix timestamp number from WebParser, and display that as a formatted date / time string of some kind in the skin.

I think you are on the right track, using an inline Lua function call is good. I would not use Update() for this.

https://www.lua.org/pil/22.1.html
Lua's date / time stuff uses the unix timestamp EPOCH numbers, so there is no need to worry about converting them to Windows timestamps in this case.

If you use this in the skin:

Code: Select all

[MeterTXOneTime]
Meter=String
Text=[&MeasureConvertTime:ConvertTime([&MeasureTXOneTime])]
MeterStyle=styleRightText
X=230
Y=20r
H=50
DynamicVariables=1
and this .lua:

Code: Select all

function ConvertTime(n)

	return os.date('%B %d %Y %H:%M:%S', n)

end
You will get this:
1.png
You do not have the required permissions to view the files attached to this post.
User avatar
balala
Rainmeter Sage
Posts: 9083
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Converting EPOCH time

balala » December 15th, 2017, 6:41 pm

jsmorley wrote:I would not use Update() for this.
jsmorley, my solution, posted above gives the same result as yours. I'd like to know why you wouldn't use the Update() function? Has it any disadvantage?
User avatar
jsmorley
Developer
Posts: 19725
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Converting EPOCH time

jsmorley » December 15th, 2017, 6:50 pm

balala wrote:jsmorley, my solution, posted above gives the same result as yours. I'd like to know why you wouldn't use the Update() function? Has it any disadvantage?
Not really a disadvantage as such, but just not the logical way to do it in my view. You want the result "on demand", as requested by the String meter. The inline function approach is clean and simple. When the String meter is updated, it asks for the result and uses it.

If you use Update(), you will get at least one execution that can't possibly return a valid result, as when the Lua Script measure is executed the first time on load / refresh, there can't be any value for the WebParser measure. So even with UpdateDivicer=-1 you will get one invalid / empty response. You can set UpdateDivider=-1 and Disabled=1 on the Lua measure, and have a FinishAction on the WebParser parent enable and update it, but seems like a lot of effort.

There are lots of ways you can get this result, but I feel like what I did is the simplest and uses the least code.