utc to local time  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
hestia
Posts: 361
Joined: Monday 25 December 2017 23:06
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Paris
Contact:

utc to local time

Post 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?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: utc to local time

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
hestia
Posts: 361
Joined: Monday 25 December 2017 23:06
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Paris
Contact:

Re: utc to local time  [Solved]

Post 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!!!
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: utc to local time

Post 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 ;)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest