Project: YugabyteDB (App Definition and Development / Database)
File: helper-functions.md

title: Helper functions for rules 2, 3, and 4 for specifying the UTC offset [YSQL] headerTitle: Helper functions for rules 2, 3, and 4 for specifying the UTC offset linkTitle: Helper functions description: Code to create helper functions for substantiating rules 2, 3, and 4 for specifying the UTC offset. [YSQL] menu: v2.18: identifier: helper-functions parent: name-res-rules weight: 50 type: docs This page presents two helper functions. They are useful in substantiating , , and explained in this page's peer pages: This function searches for the input string in each of the relevant scopesi.e. the columns in the two relevant catalog views: pgtimezonenames.name pgtimezonenames.abbrev pgtimezoneabbrevs.abbrev It returns a vector of three booleans, one for each of the searched columns, where true means "found the string in this column" and false means "failed to find it". First create the function's return type: ```plpgsql drop type if exists occurrences_t cascade; create type occurrences_t as( names_name boolean, names_abbrev boolean, abbrevs_abbrev boolean); ``` Now create the function: ```plpgsql drop function if exists occurrences(text) cascade; create function occurrences(string in text) returns occurrences_t language plpgsql as $body$ declare namesnamecount constant int not null := (select count(*) from pgtimezonenames where upper(name) = upper(string)); namesabbrevcount constant int not null := (select count(*) from pgtimezonenames where upper(abbrev) = upper(string)); abbrevsabbrevcount constant int not null := (select count(*) from pgtimezoneabbrevs where upper(abbrev) = upper(string)); r constant occurrences_t not null := ( namesnamecount > 0, namesabbrevcount > 0, abbrevsabbrevcount > 0)::occurrences_t; begin return r; end; $body$; ``` This function tries the input string in each of the three syntax contexts where a string that's intended to specify the UTC offset may be used. As the set timezone argument as the at time zone argument in specifying a timestamptz value. See , the in this page's parent, "Rules for resolving a string that's intended to identify a UTC offset", and the examples that follow it. The function uses the helper function to annotate the

Create it thus: ```plpgsql drop function if exists legalscopesforsyntaxcontext(text) cascade; create function legalscopesforsyntaxcontext(string in text) returns table(x text) language plpgsql as $body$ declare ok constant text not null := '> OK'; x1 constant text not null := '> invalidparametervalue'; x2 constant text not null := '> invaliddatetimeformat'; settimezone constant text not null := $$set timezone = '%s'$$; timezoneinvocation constant text not null := $$select timezone('%s', '%s')$$; timestamptzliteral constant text not null := $$select '%s %s'::timestamptz$$; ts_plain constant timestamp not null := '2021-06-07 12:00:00'; tstext constant text not null := tsplain::text; ts_tz timestamptz not null := 'infinity'; -- any not null value will do settimezone constant text not null := format(settimezone_, string); timezoneinvocation constant text not null := format(timezoneinvocation, string, tsplain); timestamptzliteral constant text not null := format(timestamptzliteral, tsplain, string); settimezonemsg constant text not null := rpad(set_timezone ||';', 61); timezoneinvocationmsg constant text not null := rpad(timezone_invocation ||';', 61); timestamptzliteralmsg constant text not null := rpad(timestamptz_literal ||';', 61); occurrences constant occurrences_t not null := occurrences(string); begin x := rpad(string||':', 20) || 'namesname: ' ||occurrences.namesname ::text||' / '|| 'namesabbrev: ' ||occurrences.namesabbrev ::text||' / '|| 'abbrevsabbrev: '||occurrences.abbrevsabbrev::text; return next; x := rpad('-', 90, '-'); return next; -- "set timezone" begin execute set_timezone; x := settimezonemsg||ok; return next; exception when invalidparametervalue then x := settimezonemsg||x1; return next; end; -- "at timezone" begin execute timezoneinvocation into tstz; x := timezoneinvocationmsg||ok; return next; exception when invalidparametervalue then x := timezoneinvocationmsg||x1; return next; end; begin execute timestamptzliteral into tstz; x := timestamptzliteralmsg||ok; return next; exception when invaliddatetimeformat then x := timestamptzliteralmsg||x2; return next; end; end; $body$; ``` Test it for a selection of strings: ```plpgsql select x from legalscopesforsyntaxcontext('WEST'); select x from legalscopesforsyntaxcontext('America/New_York'); select x from legalscopesforsyntaxcontext('XJT'); select x from legalscopesforsyntaxcontext('MST'); ``` These are the results ```output WEST: namesname: false / namesabbrev: true / abbrevs_abbrev: false set timezone = 'WEST'; > invalidparametervalue select timezone('WEST', '2021-06-07 12:00:00'); > invalidparametervalue select '2021-06-07 12:00:00 WEST'::timestamptz; > invaliddatetimeformat ``` and ```output America/NewYork: namesname: true / namesabbrev: false / abbrevsabbrev: false set timezone = 'America/New_York'; > OK select timezone('America/New_York', '2021-06-07 12:00:00'); > OK select '2021-06-07 12:00:00 America/New_York'::timestamptz; > OK ``` and ```output XJT: namesname: false / namesabbrev: false / abbrevs_abbrev: true set timezone = 'XJT'; > invalidparametervalue select timezone('XJT', '2021-06-07 12:00:00'); > OK select '2021-06-07 12:00:00 XJT'::timestamptz; > OK ``` and ```output MST: namesname: true / namesabbrev: true / abbrevs_abbrev: true set timezone = 'MST'; > OK select timezone('MST', '2021-06-07 12:00:00'); > OK select '2021-06-07 12:00:00 MST'::timestamptz; > OK ```