Page 1 of 1

compare time from fixing time..

Posted: Friday 14 February 2020 15:33
by OedzesG
hallo,

here a noob question again :D

for a script i needed to know how manny minutes are past since a fixed time..

for example:

fix time is: '15:00'
now its 15:30

so the number 30 is what i need..

this dosnt work

Code: Select all

dz.time.compare('15:00:00').minutes

Re: compare time from fixing time..

Posted: Friday 14 February 2020 16:47
by waaren
OedzesG wrote: Friday 14 February 2020 15:33 this doesn't work

Code: Select all

dz.time.compare('15:00:00').minutes
The compare method require two dzVents time objects.

Code: Select all

return 
{
	on = 
	{
		timer = 
		{
			'every minute',
		},
	},
	
	execute = function(dz)
	    local myTime = '15:00:00'
	    local someTime = dz.time.makeTime(dz.time.rawDate .. ' ' .. myTime) -- this will create the time object someTime
	
	    -- compare require two dzVents time objects (first one here is dz.time, second one is someTime )  
		dz.log('Delta time between  ' .. dz.time.rawTime .. ' and ' .. someTime.rawTime .. ' is ' .. dz.time.compare(someTime).minutes .. ' minutes.', dz.LOG_FORCE)
	end
}

Re: compare time from fixing time..

Posted: Friday 14 February 2020 21:38
by OedzesG
The compare method require two dzVents time objects.
thanks again waaren! :D

is there also a way to get the current minute in this week, month and year?

Re: compare time from fixing time..

Posted: Saturday 15 February 2020 2:08
by waaren
OedzesG wrote: Friday 14 February 2020 21:38 is there also a way to get the current minute in this week, month and year?
you could try this

Code: Select all

return 
{
    on = 
    {
        timer = 
        {
            'every minute',
        }
    },

    execute = function(dz)
        local day = 86400 -- (24 * 60 * 60)

        local function makeTime(date)
            return dz.time.makeTime(date .. ' 00:00:00')
        end 

        local weekStart = makeTime(os.date("%Y-%m-%d",os.time()- ( dz.time.wday - 1 ) * day))
        local dayStart = makeTime(dz.time.rawDate)
        local monthStart = makeTime(dz.time.year .. '-' .. dz.time.month ..'-01')
        local yearStart = makeTime(dz.time.year .. '-01-01')

        dz.log('Minutes today: ' .. dayStart.minutesAgo, dz.LOG_FORCE)
        dz.log('Minutes this week: ' .. weekStart.minutesAgo, dz.LOG_FORCE)
        dz.log('Minutes this month: ' .. monthStart.minutesAgo, dz.LOG_FORCE)
        dz.log('Minutes this year: ' .. yearStart.minutesAgo, dz.LOG_FORCE)
    end
}

Re: compare time from fixing time..

Posted: Sunday 16 February 2020 1:46
by OedzesG
Thnx waaren! thats exactly what i needed..

Re: compare time from fixing time..

Posted: Thursday 26 December 2024 12:52
by andrehj
(Sorry to revive an old topic, but I couldn't find anything more recent.)
waaren wrote: Friday 14 February 2020 16:47 The compare method require two dzVents time objects.

Code: Select all

return 
{
	on = 
	{
		timer = 
		{
			'every minute',
		},
	},
	
	execute = function(dz)
	    local myTime = '15:00:00'
	    local someTime = dz.time.makeTime(dz.time.rawDate .. ' ' .. myTime) -- this will create the time object someTime
	
	    -- compare require two dzVents time objects (first one here is dz.time, second one is someTime )  
		dz.log('Delta time between  ' .. dz.time.rawTime .. ' and ' .. someTime.rawTime .. ' is ' .. dz.time.compare(someTime).minutes .. ' minutes.', dz.LOG_FORCE)
	end
}
After a lot of studying, I still fail to understand how the Compare function should be used.
As far as I understand, this "dz.time.compare(someTime).minutes" only compares the current time with the time stored in the time object "someTime" and returns that difference in a number of minutes.

But how can one compare two time objects named for instance "Time1" and "Time2"? :?:

The manual doesn't help either, see https://wiki.domoticz.com/DzVents:_next ... ime_object

Code: Select all

    -- compare two times
    print(domoticz.time.compare(myDevice.lastUpdate).secs))
It says it compares two times, but all it does is compare the current time with another time....

Re: compare time from fixing time..

Posted: Friday 27 December 2024 22:56
by boum
andrehj wrote: But how can one compare two time objects named for instance "Time1" and "Time2"? :?:
have you just tried:

Code: Select all

    -- compare two times
    print(Time1.compare(Time2).minutes)