Time translation

Moderator: leecollings

Post Reply
Toulon7559
Posts: 859
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: <2025
Location: Hengelo(Ov)/NL
Contact:

Time translation

Post by Toulon7559 »

Reading a JSON-file from a Domoticz-device, the time info included is often reported in different formats.
Below is result of extraction of a few fields from a JSON-file by means of a lua-script:
Actualtime is EPOCH, other fields are text.

Code: Select all

========== JSONFile-times ========== 
 Actualtime  = 1605803582
 Servertime  = 2020-11-19 17:33:02
 LastUpdate  = 2020-11-13 06:06:58
For harmonization of time-values from a JSON-file (for a common_base time-processing, or just for display of the date&time-string also for Actualtime), it seems useful to have quick translation back & forth.
Plenty of dedicated, longer scripting solutions, and 'glueing' of found pieces will ultimately do the job,
but looking for a very short & simple 'general' set of functions performing both.
;-) No reason to reinvent the wheel ....
Therefore question related to lua-scripting:
- is short translation function available from a given EPOCH value to text-string?
- is reverse a short translation function available from given text-string to related EPOCH value?
Test case to be processed, see above.
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Time translation

Post by waaren »

Toulon7559 wrote: Thursday 19 November 2020 17:52 - is short translation function available from a given EPOCH value to text-string?
- is reverse a short translation function available from given text-string to related EPOCH value?
Test case to be processed, see above.
Could look like below (incl. examples)

Code: Select all

local function date2Timestamp(dateString, pattern)
	local pattern = pattern or '(%d+)%D+(%d+)%D+(%d+)%D+(%d+)%D+(%d+)' -- yyyy-mm-dd hh:mm
	local year, month, day, hour, minute, seconds = dateString:match(pattern)
	return os.time({year = year, month = month, day = day, hour = hour, min = minute, sec = seconds})
end

local function timestamp2Date(timestamp, pattern)
	return os.date( ( pattern or '%Y-%m-%d %H:%M:%S') , ( timestamp or os.time() ) )
end

-- date2Timestamp examples
print ( 'now               --> ' .. os.time() )                    -- no date2Timestamp function needed
print ( 'yesterday         --> ' .. os.time() - 24 * 3600  )       -- no date2Timestamp function needed 

print ( '2020-12-31 23:59  --> ' .. date2Timestamp('2020-12-31 23:59') )   -- default pattern
print ( '20201231235901   --> ' .. date2Timestamp('20201231235901','(%d%d%d%d)(%d%d)(%d%d)(%d%d)(%d%d)(%d%d)') ) -- supply own pattern

-- timestamp2Date examples
print ( 'now               --> ' .. timestamp2Date() ) -- now / default pattern 
print ( 'now time          --> ' .. timestamp2Date(nil, '%X') ) 
print ( 'now date          --> ' .. timestamp2Date(nil, '%x') ) 
print ( 'now year          --> ' .. timestamp2Date(nil, '%Y') ) 

print ( '\n1605810922 day month --> ' .. timestamp2Date(1605810922, '%A %B') ) -- default pattern 

-- test Cases
 local Actualtime  = 1605803582
 local Servertime  = '2020-11-19 17:33:02'
 local LastUpdate  = '2020-11-13 06:06:58'
 
print ('\nActualtime --> '  .. timestamp2Date(Actualtime) )
print ('Servertime --> '  .. date2Timestamp(Servertime) )
print ('LastUpdate --> '  .. date2Timestamp(LastUpdate) )
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Toulon7559
Posts: 859
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: <2025
Location: Hengelo(Ov)/NL
Contact:

Re: Time translation

Post by Toulon7559 »

Waaren,

;) As hoped & expected, a whole cart with tools already present somewhere!
Another step for processing of those JSON-files with correct time-values.

Thanks for these nice short translation-sets!
Last edited by Toulon7559 on Friday 20 November 2020 16:04, edited 1 time in total.
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Time translation

Post by waaren »

Toulon7559 wrote: Thursday 19 November 2020 23:29 Thanks for these nice short translation-sets!
Your welcome.

Below is a little bit longer but might be useful for Lua coders that cannot remember the Lua date/time format codes.

Code: Select all

local function timestamp2DateFormat(timestamp, humanizedPattern)

    local function convertMnemomic2FmtCode(humanizedPattern)
        local humanizedPattern = humanizedPattern or 'yyyy-mm-dd hh:MM:ss'
        mnemomics =
        {
            {'dddd' , '%A'}, -- full weekdayname(e.g. Wednesday) language depends on locale
            {'ddd'  , '%a'}, -- abbreviated weekdayname(e.g. Wed) language depends on locale
            {'dd'   , '%d'}, -- day of the month(16){[01-31]
            {'mmmm' , '%B'}, -- full monthname(e.g September) language depends on locale
            {'mmm'  , '%b'}, -- abbreviated monthname(e.g. Sep) language depends on locale
            {'mm'   , '%m'}, -- month[01-12]
            {'yyyy' , '%Y'}, -- 4-digit year
            {'yy'   , '%y'}, -- two-digityear(98){[00-99]
            {'hh'   , '%H'}, -- hour 24-hour clock){[00-23]
            {'ii'   , '%I'}, -- hour 12-hour clock[01-12]
            {'MM'   , '%M'}, -- minute{[00-59]
            {'ss'   , '%S'}, -- second [00-60]
            {'W'    , '%W'}, -- weeknumber [01-53]
            {'w'    , '%w'}, -- weekday{[0-6] Sunday-Saturday
            {'datm' , '%c'}, -- date and time (e.g. 09/16/98 23:48:10) format depends on locale
            {'mer'  , '%p'}, -- either "am" or "pm"
            {'date' , '%x'}, -- date(e.g. 09/16/98) format depends on locale
            {'time' , '%X'}, -- time(e.g. 23:48:10)
        }
        for _, conversion in ipairs(mnemomics) do
            humanizedPattern = string.gsub(humanizedPattern, conversion[1], '%' .. conversion[2])
        end
        return humanizedPattern
    end

    local dateTimeString = os.date( convertMnemomic2FmtCode(humanizedPattern)  , ( timestamp or os.time() ) )
    if dateTimeString:find('nZero') then  -- remove leading zero's
        return dateTimeString:gsub('nZero',''):gsub(' 0',' '):gsub('^0','')
    end
    return dateTimeString
end

print('\n----\n-- timestamp2DateFormat examples\n----')
print ( 'now                               --> ' .. timestamp2DateFormat() ) -- now / default pattern
print ( 'now date time                     --> ' .. timestamp2DateFormat(nil,'date time') ) -- now / default pattern
print ( 'now ddd mm mmm yyyy ii:MM mer     --> ' .. timestamp2DateFormat(nil, 'ddd mm mmm yyyy ii:MM mer') )
print ( 'timestamp ii:MM mer nZero         --> ' .. timestamp2DateFormat(1598077445, 'ii:MM mer nZero') )
print ( 'timestamp mm/yy ii:MM mer nZero   --> ' .. timestamp2DateFormat(1598077445, 'mm/yy ii:MM mer nZero') )
print ( 'timestamp dddd                    --> ' .. timestamp2DateFormat(1605803582, 'dddd') )
print ( 'timestamp W                       --> ' .. 'Week ' ..timestamp2DateFormat(1605803582, 'W') )
print ( 'timestamp w                       --> ' .. 'Weekday ' ..timestamp2DateFormat(1605803582, 'w') )

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: Bing [Bot] and 1 guest