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

i need the timestamp for a time

Discuss the use of Lua in Script measures.
Alex Becherer

i need the timestamp for a time

Post by Alex Becherer »

hi,

i am pretty lost here. i am reading time and date measures from an rss feed:
[MeasureMail1Year] (four digits)
[MeasureMail1Month] (two digits)
[MeasureMail1Day]
[MeasureMail1Hour]
[MeasureMail1Minute]
[MeasureMail1Second]

and would like to convert these numbers to a Windows timestamp.
does somebody have a lua script to do that?

please mind: the only programming knowledge i ever learned is Commodore64 Basic, so my solution to this problem would be calculations from a known timestamp of the first of January for each year and have @include files for each year (GOTO). but i am sure there must be a more elegant way to do this.
User avatar
Kaelri
Developer
Posts: 1721
Joined: July 25th, 2009, 4:47 am

Re: i need the timestamp for a time

Post by Kaelri »

This should do it:

Code: Select all

function Initialize()
	MeasureMail1Year   = SKIN:GetMeasure('MeasureMail1Year')
	MeasureMail1Month  = SKIN:GetMeasure('MeasureMail1Month')
	MeasureMail1Day    = SKIN:GetMeasure('MeasureMail1Day')
	MeasureMail1Hour   = SKIN:GetMeasure('MeasureMail1Hour')
	MeasureMail1Minute = SKIN:GetMeasure('MeasureMail1Minute')
	MeasureMail1Second = SKIN:GetMeasure('MeasureMail1Second')
end

function Update()
	local t = {
		year  = MeasureMail1Year:GetStringValue(),
		month = MeasureMail1Month:GetStringValue(),
		day   = MeasureMail1Day:GetStringValue(),
		hour  = MeasureMail1Hour:GetStringValue(),
		min   = MeasureMail1Minute:GetStringValue(),
		sec   = MeasureMail1Second :GetStringValue(),
	}

	return os.time(t) + 11644473600
end
User avatar
MerlinTheRed
Rainmeter Sage
Posts: 889
Joined: September 6th, 2011, 6:34 am

Re: i need the timestamp for a time

Post by MerlinTheRed »

Totally obvious :P Does the Lua os.time give a Unix timestamp?
Have more fun creating skins with Sublime Text 2 and the Rainmeter Package!
Alex Becherer

Re: i need the timestamp for a time

Post by Alex Becherer »

thanks a lot. it works great.

i have one problem though.
the timestamp is 12991857659
which is Tuesday, 2012-09-11 19:20:59 UTC
this is correct.

when i use a Time measure to convert this to a readable format i get
Tuesday, 2012-09-11 17:20:59

which is pretty off as in my local timezone it would be 21:20:59

also it seems TimeZone and DaylightSavingTime have no effect on a Time measure with TimeStamp.


Edit: copied the wrong measure
User avatar
Kaelri
Developer
Posts: 1721
Joined: July 25th, 2009, 4:47 am

Re: i need the timestamp for a time

Post by Kaelri »

MerlinTheRed wrote:Totally obvious :P Does the Lua os.time give a Unix timestamp?
Correct. The number I add at the end is just the number of seconds between 1970 (the origin date for Unix time) and 1601 (Windows time).
Alex Becherer wrote:i have one problem though.
the timestamp is 12991857659
which is Tuesday, 2012-09-11 19:20:59 UTC
this is correct.

when i use a Time measure to convert this to a readable format i get
Tuesday, 2012-09-11 17:20:59

which is pretty off as in my local timezone it would be 21:20:59
Sorry, I didn't realize the original time wasn't local. Try this:

Code: Select all

function Initialize()
	MeasureMail1Year   = SKIN:GetMeasure('MeasureMail1Year')
	MeasureMail1Month  = SKIN:GetMeasure('MeasureMail1Month')
	MeasureMail1Day    = SKIN:GetMeasure('MeasureMail1Day')
	MeasureMail1Hour   = SKIN:GetMeasure('MeasureMail1Hour')
	MeasureMail1Minute = SKIN:GetMeasure('MeasureMail1Minute')
	MeasureMail1Second = SKIN:GetMeasure('MeasureMail1Second')
end

function Update()
	local UTC             = os.date('!*t')
	local LocalTime       = os.date('*t')
	local DaylightSavings = LocalTime.isdst and 3600 or 0
	local LocalOffset     = os.time(LocalTime) - os.time(UTC) + DaylightSavings

	local t = {
		year  = MeasureMail1Year:GetStringValue(),
		month = MeasureMail1Month:GetStringValue(),
		day   = MeasureMail1Day:GetStringValue(),
		hour  = MeasureMail1Hour:GetStringValue(),
		min   = MeasureMail1Minute:GetStringValue(),
		sec   = MeasureMail1Second:GetStringValue(),
	}

	return os.time(t) + 11644473600 + 2 * LocalOffset
end
Alex Becherer

Re: i need the timestamp for a time

Post by Alex Becherer »

thanks a lot. it works great :)