Template:Opdrts

data-sort-value=""|

Opdrts is short for "Opinion Poll Date Range Table Sorting", following the naming style of Date table sorting, to show UK-style date ranges in opinion poll article tables. I wanted to use a somewhat obscure name so better names are left available for any future more general purpose template, and a compact name as it would be heavily used in table rows.

Usage
All parameters are positional, and date elements are a single numeric, or for the month optionally in 3-letter text form eg "Jan" (i.e. not full textual month) as an alternative to "01":

 

The template displays the range using the recommended style for Opinion polling for the next United Kingdom general election (see examples below), and generates a data-sort-value from the end date for correct table sorting. The default format is suitable to single year tables, with the "year" format suitable for multi-year tables.

If Start Day is greater than End Day it is taken to be in the previous month; if End Month is January, Dec is used for the displayed start month, but for compactness without its year (one less than End Year) being shown.

Examples
-> data-sort-value="2020-10-15"|14–15 Oct -> data-sort-value="2020-10-02"|30 Sep – 2 Oct -> data-sort-value="2020-08-24"|24 Aug -> data-sort-value="2019-12-12"|12 Dec 2019 -> data-sort-value="2020-01-02"|27 Dec – 2 Jan 2020

Pseudo-code
'data-sort-value="$4-$3-$2"|'

if $1 then if $1 > $2 then '$1 month($3 - 1) – ' else '$1–' fi fi

'$2 month($3)'

if $5 = "year" then ' $4' fi

Implementation notes
Makes use of the zero month feature of #time (derived from PHP strtotime) so January-1 goes to December previous year.

Note 5th format param could be extended eg with "ddM" (default), "ddm" (full month name), "ddMy" (equiv to "year") and with different output orders. If this is done, should probably implement in Lua.

Limit: a poll can take no longer than one month, because of the simplified usage syntax. If this should ever happen, you'll need to produce the text manually. (We could extend the 1st param to optionally be a full date if removing the limit became important.)

Bug workaround: the seemingly unnecessary use of 0expr is to work around a seeming bug in #time for dates like "2020-Oct-5" which generate as if it was "2020-Oct-1" taking the 5 as if 5am:
 * #time: Y-m-d & r of "2020-Oct-5" generates: &

whereas slight variations work correctly:
 * #time: Y-m-d & r of "2020-Oct-05" generates: &
 * #time: Y-m-d & r of "2020-10-5" generates: &

Note: There is some discussion on using this template at Talk:Opinion polling for the next United Kingdom general election.

Converting existing text to use opdrts
I have developed a GNU sed script to convert prior WP articles to use opdrts. You will need a Unix/Linux/clone system to do this, and if the wiki source has non-ASCII in it you must run sed in the "C" locale for correct operation. Copy and paste the text to a file on the system, run the GNU sed script below on the file, then carefully check the output to make sure the conversion is correct; if there are mistakes or oddities, eg the data-sort-value year not matching the date year, the script will generate some incorrect output: /data-sort-value="/{ s/\(\x96\|\d196\|\d150\|\d151\|\o226\)/-/g s/data-sort-value="\(20[0-9][0-9]\)[-–][0-1][0-9][-–][0-3][0-9]" *;* *| *\([0-9]\+\) *[JFMAMJJASOND][a-z][a-z] *\(-\|–\|–\) *\([0-9]\+\)  *\([JFMAMJJASOND][a-z][a-z]\) *\(\1\?\)/1: / s/data-sort-value="\(20[0-9][0-9]\)[-–][0-1][0-9][-–][0-3][0-9]" *;* *| *\([0-9]\+\) *\(-\|–\|–\) *\([0-9]\+\)  *\([JFMAMJJASOND][a-z][a-z]\) *\(\1\?\)/2: / s/data-sort-value="\(20[0-9][0-9]\)[-–][0-1][0-9][-–][0-3][0-9]" *;* *| *\([0-9]\+\)  *\([JFMAMJJASOND][a-z][a-z]\) *\(\1\?\)/3: / s/|MAYBE}}/}}/ s/|MAYBE20..}}/|year}}/ }
 * 1) note debug tags like "2: " are inserted then removed at end
 * 2) convert unusual dashes to -
 * 1) eg data-sort-value="2021-03-01"|28 Feb - 1 Mar
 * 1) eg data-sort-value="2021-03-05"|2-5 Mar
 * 1) eg data-sort-value="2021-03-15"|15 Mar


 * 1) # if your dates have no data-sort-value, you can try this little tested code, but check output very carefully:
 * /| *\([0-9]\+\).* \([JFMAMJJASOND][a-z][a-z]\) *\(20[0-9][0-9]\)/{
 * 1) s/\(\x96\|\d196\|\d150\|\d151\|\o226\)/-/g
 * 2) # eg 28 Feb - 1 Mar 2021
 * s/| *\([0-9]\+\) *[JFMAMJJASOND][a-z][a-z] *\(-\|–\|–\) *\([0-9]\+\)  *\([JFMAMJJASOND][a-z][a-z]\) *\(20[0-9][0-9]\)/|4: /
 * 1) # eg 2-5 Mar 2021
 * s/| *\([0-9]\+\) *\(-\|–\|–\) *\([0-9]\+\) *\([JFMAMJJASOND][a-z][a-z]\) *\(20[0-9][0-9]\)/|5: /
 * 1) # eg 5 Mar 2021
 * s/| *\([0-9]\+\) *\([JFMAMJJASOND][a-z][a-z]\) *\(20[0-9][0-9]\)/|6: /
 * 1) # fixup style
 * 2) s/^\(| *style=.*\)|.: {{opdrts|/\1{{opdrts|/
 * }
 * /^| *[0-9]\+.*[JFMAMJJASOND][a-z][a-z] *$/{
 * 1) s/\(\x96\|\d196\|\d150\|\d151\|\o226\)/-/g
 * 2) # eg 28 Feb - 1 Mar
 * 3) s/^| *\([0-9]\+\)  *[JFMAMJJASOND][a-z][a-z] *\(-\|–\|–\) *\([0-9]\+\)  *\([JFMAMJJASOND][a-z][a-z]\) *\(\1\?\)/|4: {{opdrts|\1|\3|\4|2021}}/
 * 4) # eg 2-5 Mar
 * 5) s/^| *\([0-9]\+\) *\(-\|–\|–\) *\([0-9]\+\) *\([JFMAMJJASOND][a-z][a-z]\)/|5: {{opdrts|\1|\3|\4|2021}}/
 * 6) # eg 5 Mar
 * 7) s/^| *\([0-9]\+\) *\([JFMAMJJASOND][a-z][a-z]\)/|6: {{opdrts||\1|\2|2021}}/
 * }
 * 1) s/^| *\([0-9]\+\) *\([JFMAMJJASOND][a-z][a-z]\)/|6: {{opdrts||\1|\2|2021}}/
 * }

s/[123456]: {{opdrts/{{opdrts/ s/^| *{{opdrts/|{{opdrts/ /^|{opdrts/s/ *$//
 * 1) remove debugging tags:
 * 1) cleanup leading/trailing space

It uses the GNU extension  so pre-GNU versions of sed won't work. Here are example commands to do the job assuming  is the script above and   is the wikipedia article text:

LC_ALL=C sed -f opdrts.sed article.old > article.new grep data-sort-value article.new egrep "opdrts| [JFMAMJJASOND][a-z][a-z] *$" article.new diff article.old article.new grep -v data-sort-value article.old | grep --color "[0-9] [JFMAMJJASOND][a-z][a-z] 20[12][0-9]"
 * 1) visual checks:
 * 1) see if you have dates with year in original without a data-sort-value: