<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="160548" data-post-id="160548">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="kip" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kip/120/1440_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  kip
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>ex_cldr Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>And if you chose to go the “add more names” route you could use <code>TzWorld.timezone_at/1</code> to return the timezone name of that city. See <a href="https://hex.pm/packages/tz_world" rel="nofollow">tz_world</a>. It uses the cool <a href="https://github.com/evansiroky/timezone-boundary-builder" rel="nofollow">timezone boundary builder</a> data.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="160548" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/list-of-time-zones-in-select-element/28532/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-160548" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="160548"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="249844" data-post-id="249844">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="nathanl" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nathanl/120/39891_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  nathanl
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here’s one approach to letting users select a locale like “America/New_York”:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def locale_list do
    now = DateTime.utc_now()

    Tzdata.zone_list()
    |&gt; Enum.map(fn zone -&gt;
      tzinfo = Timex.Timezone.get(zone, now)
      offset = Timex.TimezoneInfo.format_offset(tzinfo) # added in v3.78
      label = "#{tzinfo.full_name} - #{tzinfo.abbreviation} (#{offset})"

      {tzinfo.full_name, label}
    end)
    |&gt; Enum.uniq()
  end
</code></pre>
<p>This outputs a list of values like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">[
  {"Africa/Abidjan", "Africa/Abidjan - GMT (+00:00:00)"},
  ...
]
</code></pre>
<p>If you wanted to sort these by offset, you could use the value of <code>Timex.Timezone.total_offset(tzinfo)</code>, but I think alphabetical makes more sense, since the locales are grouped by region.</p>
<p>In the template, I think an <code>&lt;input&gt;</code> with a <code>&lt;datalist&gt;</code> is better than a <code>&lt;select&gt;</code>, as it lets users type and use autocomplete, which in my testing will match any substring, like “africa” or “abidjan” or “gmt” or “05:30” (although exactly how that works is up to the browser). This means that if somebody knows their offset and wants to type it, it can narrow to the locales that match.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">&lt;input list="locales" name="user-locale" id="user-locale" value={@user.tz_locale}&gt;
&lt;datalist id="locales"&gt;
  &lt;%= for {fullname, label} &lt;- @locales do %&gt;
    &lt;option value={ fullname } &gt;
      &lt;%= label %&gt;
    &lt;/option&gt;
  &lt;% end %&gt;
&lt;/datalist&gt;
</code></pre>
<p>It’s still not as nice as being able to type the name of whatever town you live in, big or small, but the answers above could help you do that.</p>
<p>Oh yeah, and <code>Intl.DateTimeFormat().resolvedOptions().timeZone</code> in JS can get the user’s locale from the browser - <a href="https://caniuse.com/?search=DateTimeFormat" class="inline-onebox" rel="noopener nofollow ugc">"DateTimeFormat" | Can I use... Support tables for HTML5, CSS3, etc</a></p>
<p>So you can add a button to autofill:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">&lt;button phx-hook="AutofillLocale"&gt;Autofill&lt;/button&gt;
</code></pre>
<p>…and set up a hook to do it:</p>
<pre data-code-wrap="javascript"><code class="lang-javascript">Hooks.AutofillLocale = {
  mounted() {
    this.el.addEventListener("click", e =&gt; {
      let locale = Intl.DateTimeFormat().resolvedOptions().timeZone
      document.getElementById("user-locale").value = locale
    })
  }
}
</code></pre>
<p>Here’s a quick demo:</p>
<p><img src="https://forum.elixirforum.com/uploads/default/original/3X/7/f/7f1957bca5baa63aed1242c94b219df17e0298ad.gif" alt="locale_picker" data-base62-sha1="i8mVZdchg9lJ3edDMZHqKYNIFdP" width="480" height="132" class="animated"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="249844" data-batch-url="/posts/batch_likers">
                        11
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/list-of-time-zones-in-select-element/28532/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-249844" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="249844"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>