Project: YugabyteDB (App Definition and Development / Database)
File: rule-1.md

title: Rule 1 (for string intended to specify the UTC offset) [YSQL] headerTitle: Rule 1 linkTitle: 1 case-insensitive resolution description: Substantiates the rule that a string that's intended to identify a UTC offset is resolved case-insensitively. [YSQL] menu: v2.18: identifier: rule-1 parent: name-res-rules weight: 10 type: docs {{< tip title="" >}} A string that's intended to identify a UTC offset is resolved case-insensitively. (This rule excludes, of course, selecting explicitly from one of the pgtimezonenames or pgtimezoneabbrevs catalog views). {{< /tip >}} The anonymous PL/pgSQL block below looks up the name America/CostaRica, spelled exactly with that mix of case, in pgtimezonenames_ and returns its abbreviation. Because an ordinary query with a case-sensitive predicate is used, this confirms the canonical status of the spelling. The canonically spelled name America/CostaRica is also used to return utcoffset from pgtimezonenames. And the abbreviation is used to return utcoffset from pgtimezoneabbrevs. It isn't always the case that a timezone abbreviation from pgtimezonenames is found in pgtimezoneabbrevs. But the fact that the query used here returns a value without error shows that, in this case, a match is found. An assert_ statement tests that the two possibly different offset values agree. (This, too, isn't always the case.) Another assert statement confirms that the abbreviation is not found as a name in pgtimezonenames. (This sometimes is the case.) Four different variants of the timezone name, each spelling it with a different mix of case, are defined. An assert statement ("Assert #1") confirms that invoking currentsetting('TimeZone')_ returns the canonical spelling when the timezone is set using one of the case-variants of the timezone name. Another three case-variants are used, each in a different syntax context, to produce a timestamptz value. And assert statements ("Assert #2" and "Assert #3") are used to confirm that the three differently-produced timestamptz values are all the

Finally, the result of the lower() built-in function on the abbreviation is used to construct a timestamptz literal; and the upper() result is used as the at time zone argument. And assert statements ("Assert #4" and "Assert #5") are used to confirm that these two timestamptz values are the same as the first three. Try the block like this: ```plpgsql do $body$ declare -- The spelling of "nm" is the canonical one, as is recorded in "pgtimezonenames" -- and as is returned by "current_setting('TimeZone')". nm constant text not null := 'America/Costa_Rica'; -- Case variants. Each is used once in a different syntax setting. n1 constant text not null := 'AMERICA/COSTA_RICA'; n2 constant text not null := 'america/costa_rica'; n3 constant text not null := 'AMERICA/costa_rica'; n4 constant text not null := 'america/COSTA_RICA'; ab constant text not null := (select abbrev from pgtimezonenames where name = nm); on constant interval not null := (select utcoffset from pgtimezone_names where name = nm); oa constant interval not null := (select utcoffset from pgtimezone_abbrevs where abbrev = ab); c constant int not null := (select count(*) from pgtimezonenames where name = ab); set_timezone constant text not null := $$set timezone = '%s'$$; tzonentry constant text not null := current_setting('TimeZone'); begin assert oa = oa, 'name/abbrev UTC offsets disagree'; assert c = 0, 'Abbrev found in pgtimezonenames.name'; declare lower_ab constant text not null := lower(ab); upper_ab constant text not null := upper(ab); dt_text constant text not null := '2021-05-15 12:00:00'; t0 constant timestamptz not null := make_timestamptz(2021, 5, 15, 12, 0, 0, n1); begin execute format(set_timezone, n2); assert current_setting('timezone') = nm, 'Assert #1 failed'; assert (dt_text||' '||n3)::timestamptz = t0, 'Assert #2 failed'; assert (dt_text::timestamp at time zone n4) = t0, 'Assert #3 failed'; assert (dttext||' '||lowerab)::timestamptz = t0, 'Assert #4 failed'; assert (dttext::timestamp at time zone upperab) = t0, 'Assert #5 failed'; end; execute format(settimezone, tzon_entry); end; $body$; ``` The block finishes silently. This outcome supports the formulation of the rule that this page addresses.