Page 1 of 1

utc to local time

Posted: Wednesday 13 January 2021 21:29
by hestia
Hi,
I don't understand how to use the DzVents methods: UTC, utcSystemTime, utcTime to convert an utc time like 202101132000 to the local time = 202101132100
Somebody could give me an example?

Re: utc to local time

Posted: Wednesday 13 January 2021 23:08
by waaren
hestia wrote: Wednesday 13 January 2021 21:29 Somebody could give me an example?
There is no ready made method to go from a formatted string representing UTC time to a local time formatted string in dzVents but below example will show you how it can be done.

Code: Select all

-- requires dzVents >= 3.0.17 (domoticz 2020.2 build 12702)

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

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'UTC example'
    },

    execute = function(dz)

        local UTCtime = '202101132000'

        -- calculate the delta between UTC and local time
        local function getTimezone()
            local now = os.time()
            return math.floor(os.difftime(now, os.time(os.date("!*t", now))))
        end

        -- Convert the string to an epoch time stamp and add timezone delta in seconds
        local myLocalTimestamp = dz.time.dateToTimestamp(UTCtime,'(%d%d%d%d)(%d%d)(%d%d)(%d%d)(%d%d)' )  + getTimezone()

        -- Convert the caclulated timestamp to the required format
        local localTime = dz.time.timestampToDate(myLocalTimestamp,'yyyymmddhhMM')

        dz.log('UTC time ' .. UTCtime .. ' = ' .. localTime ..  ' local time', dz.LOG_DEBUG)

    end
}

Re: utc to local time  [Solved]

Posted: Thursday 14 January 2021 9:00
by hestia
waaren wrote: Wednesday 13 January 2021 23:08 below example will show you how it can be done

Code: Select all

-- requires dzVents >= 3.0.17 (domoticz 2020.2 build 12702)

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

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'UTC example'
    },

    execute = function(dz)

        local UTCtime = '202101132000'

        -- calculate the delta between UTC and local time
        local function getTimezone()
            local now = os.time()
            return math.floor(os.difftime(now, os.time(os.date("!*t", now))))
        end

        -- Convert the string to an epoch time stamp and add timezone delta in seconds
        local myLocalTimestamp = dz.time.dateToTimestamp(UTCtime,'(%d%d%d%d)(%d%d)(%d%d)(%d%d)(%d%d)' )  + getTimezone()

        -- Convert the caclulated timestamp to the required format
        local localTime = dz.time.timestampToDate(myLocalTimestamp,'yyyymmddhhMM')

        dz.log('UTC time ' .. UTCtime .. ' = ' .. localTime .. ' local time, dz.LOG_DEBUG)

    end
}
Thanks a lot waaren. Again!
I think there is a typo... I've changed this line:

Code: Select all

        dz.log('UTC time ' .. UTCtime .. ' = ' .. localTime .. ' local time, dz.LOG_DEBUG)
to

Code: Select all

dz.log('UTC time ' .. UTCtime .. ' = local Time ' .. localTime, dz.LOG_DEBUG)
Amazing if you didn't even run it once!!!

Re: utc to local time

Posted: Thursday 14 January 2021 9:31
by waaren
hestia wrote: Thursday 14 January 2021 9:00 Amazing if you didn't even run it once!!!
:oops: You give me too much credit. :D I tested it but made a last minute change when I posted it here.

Note to my self: Don't do this ;)