1 if [ ! "$_TIMEZONE_COUNTRIES_SUBR" ]; then _TIMEZONE_COUNTRIES_SUBR=1
3 # Copyright (c) 2011-2015 Devin Teske
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 ############################################################ FUNCTIONS
31 # f_country $code $property [$var_to_set]
33 # Returns a single property of a given country. Available properties are:
35 # name Name of the country as read from _PATH_ISO3166.
36 # nzones Number of zones within the country (-1 if country has
37 # only a single zone).
38 # filename The filename portion of the TZ field (after the `/') as
39 # read from _PATH_ZONETAB.
40 # cont The principal continent in which the country lies (appears
41 # before the `/' in the TZ field of _PATH_ZONETAB).
42 # filename_N Like filename, but for the Nth zone when the country has
43 # multiple zones (nzones > 0).
44 # cont_N Like cont, but for the Nth zone when the country has
45 # multiple zones (nzones > 0).
46 # descr_N Like name, but for the Nth zone when the country has
47 # multiple zones (nzones > 0)
49 # If $var_to_set is missing or NULL, the value of $var_to_get is printed to
50 # standard output for capturing in a sub-shell (which is less-recommended
51 # because of performance degredation; for example, when called in a loop).
55 f_getvar "country_${1}_$2" $3
60 # Sorts alphabetically the 2-character country codes listed in $COUNTRIES based
61 # on the name of each country.
63 # This function is a two-parter. Below is the awk(1) portion of the function,
64 # afterward is the sh(1) function which utilizes the below awk script.
66 f_sort_countries_awk='
67 function _asorti(src, dest)
70 for (i in src) dest[++nitems] = i
71 for (i = 1; i <= nitems; k = i++) {
73 while ((k > 0) && (dest[k] > idx)) {
74 dest[k+1] = dest[k]; k--
81 split(ENVIRON["COUNTRIES"], array, /[[:space:]]+/)
85 name = ENVIRON["country_" tlc "_name"]
88 n = _asorti(countries, sorted_countries)
89 for (i = 1; i <= n; i++)
90 print countries[sorted_countries[i]]
96 export COUNTRIES # for awk(1) ENVIRON[] visibility
97 COUNTRIES=$( awk "$f_sort_countries_awk" )
98 export COUNTRIES # Pedantic
101 ############################################################ MAIN
103 f_dprintf "%s: Successfully loaded." timezone/countries.subr
105 fi # ! $_TIMEZONE_COUNTRIES_SUBR