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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote group-livebook_core_team" data-username="josevalim" data-post="10" data-topic="18734">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/48/1787_2.png" class="avatar"> josevalim:</div>
<blockquote>
<p>This is exactly what I was looking for, thank you. One of the issues mentioned there though is that in Japan they consider the AM/PM boundaries to differ from western ones. So I am wondering if we should instead pass the whole hour, minute and second to the formatter and make the AM/PM completely a formatter concern. I would say this is preferable because having to build a calendar only for localization is quite annoying.</p>
</blockquote>
</aside>
<p>We’d need to pass the “parts per day” thingy into the formatter - the formatter should be calendar-agnostic and hours, minutes, etc are a calendar concern.</p>
<aside class="quote group-livebook_core_team" data-username="josevalim" data-post="12" data-topic="18734">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/48/1787_2.png" class="avatar"> josevalim:</div>
<blockquote>
<p>The opposite is also true, the calendar is bound to some localization rules. As you said, <code>day_of_week</code> and <code>week_of_year</code> can be localized.</p>
</blockquote>
</aside>
<p>I think the simplest and most pragmatic approach would be to specify that regardless of the formatter the <code>week_of_year</code> always uses the ISO week, in that it always starts on Mondays. On the other hand for the <code>day_of_week</code> we can easily specify that they are 1-based and start on Monday and the formatter may translate between different conventions easily.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="108149" 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/proposal-strftime-based-calendar-datetime-formatting/18734/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-108149" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="108149"
                     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 #12"></div>
  </section>
</div>
    <div class="postbit" id="108157" data-post-id="108157">
  <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">
								<aside class="quote no-group" data-username="michalmuskala" data-post="13" data-topic="18734">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/michalmuskala/48/20288_2.png" class="avatar"> michalmuskala:</div>
<blockquote>
<p>I think the simplest and most pragmatic approach would be to specify that regardless of the formatter the <code>week_of_year</code> always uses the ISO week, in that it always starts on Mondays.</p>
</blockquote>
</aside>
<p>I’m curious why only the <code>week_of_year</code> would be interpreted in the context of the ISO calendar only when all the other elements would be interpreted in a calendar-specific way?  Thats the point of adding <code>week_of_year</code> to the <code>Calendar</code> behaviour, no?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="108157" 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/proposal-strftime-based-calendar-datetime-formatting/18734/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-108157" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="108157"
                     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 #13"></div>
  </section>
</div>
    <div class="postbit" id="108169" data-post-id="108169">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="kip" data-post="14" data-topic="18734">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kip/48/1440_2.png" class="avatar"> kip:</div>
<blockquote>
<p>I’m curious why only the <code>week_of_year</code> would be interpreted in the context of the ISO calendar only when all the other elements would be interpreted in a calendar-specific way?</p>
</blockquote>
</aside>
<p>The conflict comes based on the fact that, even though it is calendar-based, it is one of the aspects that the western word disagrees with the most, so it makes the problem more apparent. There are probably other fields in the Calendar.ISO that are conflicting too but not as much?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="108169" data-batch-url="/posts/batch_likers">
                        1
                      </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/proposal-strftime-based-calendar-datetime-formatting/18734/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-108169" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="108169"
                     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 #14"></div>
  </section>
</div>
    <div class="postbit" id="108173" data-post-id="108173">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Ok, after a day of thinking, I believe I have a better understanding of the issues we are discussing. The trouble here is that we have four different concerns between the formatter and the calendar:</p>
<ul>
<li>localization - default date, default time and default date time)</li>
<li>week_of_year - what is the initial day of the week (monday or sunday) and on which week the year starts (it is also part of localization)</li>
<li>translation so weekday 1 is monday (en) / segunda-feira (pt-br) and month 2 is february (en) / fevereiro (pt-br)</li>
<li>padding - which is how to pad each entry, which is calendar specific</li>
</ul>
<p>The problem is that we are trying to fit those concerns into either the formatter or the calendar and having trouble doing so. Here is a new proposal: we get rid of the formatter and instead we pass multiple options. The options are:</p>
<ul>
<li>default_date - configures the default date</li>
<li>default_time - configures the default time</li>
<li>default_datetime - configures the default datetime</li>
<li>week_of_year - a function that receives calendar, year, month, day and returns the week_of_year tuple</li>
<li>month_names - a function that converts integers to month names: <code>fn 1 -&gt; "January"; 2 -&gt; "February"; ... end</code></li>
<li>abbreviated_month_names - a function that converts integers to abbreviated month names:  <code>fn 1 -&gt; "Jan"; 2 =&gt; "Feb"; ... end</code></li>
<li>day_of_week_names - a function that converts integers to day of week names:  <code>fn 1 -&gt; "Monday"; 2 -&gt; ... end</code></li>
<li>abbreviated_day_of_week_names - a function that converts integers to day of week names:  <code>fn 1 -&gt; "Monday"; 2 -&gt; ... end</code></li>
<li>default_padding - (<code>:zero</code>, <code>:space</code>, <code>:none</code>)</li>
<li>year_padding - 4</li>
<li>month_padding - 2</li>
<li>day_padding - 2</li>
<li>hour_padding - 2</li>
<li>minute_padding - 2</li>
<li>second_padding - 2</li>
<li>day_of_week_padding - 2</li>
<li>day_of_year_padding - 2</li>
<li>week_of_year_padding - 2</li>
</ul>
<p>The calendar should return a map with defaults for all of those options which can be customized when the <code>format</code> function is invoked.</p>
<h2><a name="p-108173-ampm-1" class="anchor" href="#p-108173-ampm-1" aria-label="Heading link" rel="nofollow"></a>AM/PM</h2>
<p>For AM/PM, we will add a new function to Calendar that returns <code>:am</code>, <code>:pm</code>, <code>:noon</code> or <code>:midnight</code>. We need moon and midnight because some languages consider those different parts of am/pm. Therefore we will also add an option to the formatter like this:</p>
<ul>
<li>am_pm - a function that converts parts am/pm info: <code>fn :am -&gt; "am"; :midnight  -&gt; "am"; :pm -&gt; "pm"; :noon -&gt; "pm" end</code></li>
</ul> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="108173" data-batch-url="/posts/batch_likers">
                        2
                      </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/proposal-strftime-based-calendar-datetime-formatting/18734/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-108173" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="108173"
                     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 #15"></div>
  </section>
</div>
    <div class="postbit" id="108178" data-post-id="108178">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I think that an important part of formatting is to return good errors when the input data is not available for formatting. For instance if you call Calendar.format(nil, “%Y-%m-%d %H:%M:%S.%f”) it should return an error. Because <code>nil</code> does not have a year, nor hour nor minute and so on.</p>
<p>Likewise if you have a simple <code>Date</code> such as ~D[2018-12-15] and then call format with “%H:%M” (hour and minute) an error should be returned.  Just like <code>nil</code> does not have any hour or minute, neither does <code>~D[2018-12-15]</code>.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="108178" data-batch-url="/posts/batch_likers">
                        1
                      </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/proposal-strftime-based-calendar-datetime-formatting/18734/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-108178" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="108178"
                     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 #16"></div>
  </section>
</div>
    <div class="postbit" id="108189" data-post-id="108189">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote group-livebook_core_team quote-modified" data-username="josevalim" data-post="10" data-topic="18734">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/48/1787_2.png" class="avatar"> josevalim:</div>
<blockquote>
<blockquote>
<p>axelson:<br>
Is there a chance of providing a means for developers to flexibly provide their own custom formatting? It could be used to provide the ordinal formatting that <a class="mention" href="/u/nicd" rel="nofollow">@Nicd</a> is requesting.</p>
</blockquote>
<p>Can you use string interpolation in those cases?</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">original = compute_ordinal(now)
iex&gt; Calendar.format(now, "%B #{ordinal}")
December 14th
</code></pre>
<p>And if you want this to be extensible too you can have your own behaviour that builds on top of the existing one. The only trouble with interpolation is if the returned string also contains a <code>%</code> , but that is rare and you can always escape it. Or you can call format twice and inject your contents between them.</p>
</blockquote>
</aside>
<p>Yeah we could for simple cases. For more complex cases you could split the string when you have any custom interpolations, format the results separately, and then join all three pieces (recursively). <img src="https://forum.elixirforum.com/images/emoji/apple/+1.png?v=15" title=":+1:" class="emoji" alt=":+1:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="108189" 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/proposal-strftime-based-calendar-datetime-formatting/18734/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-108189" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="108189"
                     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 #17"></div>
  </section>
</div>
    <div class="postbit" id="108195" data-post-id="108195">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I have updated the proposal with the developments mentioned in this thread. The custom formatter is gone and now we have a generally more flexible option-based API.</p>
<p><a class="mention" href="/u/kip" rel="nofollow">@kip</a> and <a class="mention" href="/u/qqwy" rel="nofollow">@Qqwy</a>, can you please review the new sections “Options” and “Calendar extensions”? I would love your thoughts generally but also in particular on this bullet:</p>
<aside class="quote group-livebook_core_team quote-modified" data-username="josevalim" data-post="1" data-topic="18734">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/48/1787_2.png" class="avatar"> josevalim:</div>
<blockquote>
<ul>
<li><code>hours_in_am_pm/3</code> - it will receive the hours, minutes and second and return a tuple with <code>{hours, :am | :pm | :noon | :midnight}</code> where hours is the equivalent hours if the day was divided in two, at midnight and noon. Note <code>:midnight</code> and <code>:noon</code> must be returned for disambiguation in different locales</li>
</ul>
</blockquote>
</aside>
<p>Thanks!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="108195" data-batch-url="/posts/batch_likers">
                        3
                      </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/proposal-strftime-based-calendar-datetime-formatting/18734/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-108195" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="108195"
                     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 #18"></div>
  </section>
</div>
    <div class="postbit" id="108208" data-post-id="108208">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote group-livebook_core_team" data-username="josevalim" data-post="19" data-topic="18734">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/48/1787_2.png" class="avatar"> josevalim:</div>
<blockquote>
<p><code>hours_in_am_pm/3</code> - it will receive the hours, minutes and second and return a tuple with <code>{hours, :am | :pm | :noon | :midnight}</code> where hours is the equivalent hours if the day was divided in two, at midnight and noon. Note <code>:midnight</code> and <code>:noon</code> must be returned for disambiguation in different locales</p>
</blockquote>
</aside>
<p>Very good!</p>
<p>As a whole, I think the new variant of the proposal with separate functions for every part is a much better way to split up the concerns. Great thinking! <img src="https://forum.elixirforum.com/images/emoji/apple/+1.png?v=15" title=":+1:" class="emoji" alt=":+1:" loading="lazy" width="20" height="20"></p>
<p>The one thing that might need more clarification, is what should happen on failure, such as:</p>
<ul>
<li>The cases that <a class="mention" href="/u/lau" rel="nofollow">@Lau</a> described in his post above.</li>
<li>What if we have a formatter ‘day-of-week’ function but we have a week of 10 days? This applies to <code>:month_names</code>, <code>:abbreviated_month_names</code>, <code>:day_of_week_names</code>, <code>:abbreviated_day_of_week_names</code>. Maybe we should standardize an ‘error’-result they should return, which <code>Calendar.format</code> then turns into a formatting-exception?</li>
</ul> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="108208" 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/proposal-strftime-based-calendar-datetime-formatting/18734/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-108208" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="108208"
                     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 #19"></div>
  </section>
</div>
    <div class="postbit" id="108234" data-post-id="108234">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote group-livebook_core_team" data-username="josevalim" data-post="19" data-topic="18734">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/48/1787_2.png" class="avatar"> josevalim:</div>
<blockquote>
<p><code>hours_in_am_pm/3</code> - it will receive the hours, minutes and second and return a tuple with <code>{hours, :am | :pm | :noon | :midnight}</code> where hours is the equivalent hours if the day was divided in two, at midnight and noon. Note <code>:midnight</code> and <code>:noon</code> must be returned for disambiguation in different locales</p>
</blockquote>
</aside>
<p>So after a second look I realized that this is not enough because in some locales they want to treat the hours from 1-12 or use 0 for midnight and 12 for noon. So I have made <code>hours_in_am_pm</code> fully localizable and removed the <code>:midnight</code> and <code>:noon</code> returns.</p>
<aside class="quote no-group" data-username="Qqwy" data-post="20" data-topic="18734">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/qqwy/48/1349_2.png" class="avatar"> Qqwy:</div>
<blockquote>
<p>The cases that <a class="mention" href="/u/lau" rel="nofollow">@Lau</a> described in his post above.</p>
</blockquote>
</aside>
<p>Yes, this we will have for sure.</p>
<aside class="quote no-group" data-username="Qqwy" data-post="20" data-topic="18734">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/qqwy/48/1349_2.png" class="avatar"> Qqwy:</div>
<blockquote>
<p>What if we have a formatter ‘day-of-week’ function but we have a week of 10 days? This applies to <code>:month_names</code> , <code>:abbreviated_month_names</code> , <code>:day_of_week_names</code> , <code>:abbreviated_day_of_week_names</code> .</p>
</blockquote>
</aside>
<p>Since it is a user supplied function, then it is up to them, so they should provide the relevant error message. We can add a formalization latter if necessary.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="108234" 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/proposal-strftime-based-calendar-datetime-formatting/18734/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-108234" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="108234"
                     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 #20"></div>
  </section>
</div>
    <div class="postbit" id="108266" data-post-id="108266">
  <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>I’ve been thinking about this a lot on a long plane flight so here goes:</p>
<h3><a name="p-108266-calendar-module-responsibilities-1" class="anchor" href="#p-108266-calendar-module-responsibilities-1" aria-label="Heading link" rel="nofollow"></a>Calendar module responsibilities</h3>
<p>This part is not about formatting but separation and encapsulation of concerns.  But if there is agreement with this, and my proposal about augmenting the format flags, then it greatly simplifies the contract between <code>strftime</code> and a calendar.</p>
<p>I think a calendar should encapsulate everything relevant to calculating days, weeks, months, years etc.  That is, any configuration should be baked into the module that implements the <code>Calendar</code> behaviour.</p>
<p>This seems obvious when we talk about the <code>iSO</code> calendar versus the <code>Balinese</code>, or <code>Ancient Egyptian</code> or the <code>Islamic</code> calendars.  But its less obvious if we consider the myriad of calendars derived from the Gregorian.</p>
<p>For example, in the US, a company may have a fiscal calendar that starts on an arbitrary day of the year.  All months, quarters, weeks and days are calculated from this date.   <a href="https://cisco.com" rel="nofollow">Cisco Systems</a> has a financial year calendar that ends on the last Saturday of July.  There are a whole class of calendars called <a href="https://en.wikipedia.org/wiki/4%E2%80%934%E2%80%935_calendar" rel="nofollow">445 calendars</a> and their cousins the <code>454</code> and <code>544</code> which are common in retail and are designed to make it better for comparing this year to prior year periods.</p>
<p>Although I started thinking this would be best done as configuration passed around in each structurally compatible <code>Date</code>, <code>Time</code> or <code>DateTime</code> struct or map, the variations are too many.  I now feel it is better that this detail be backed into a module that implements the <code>Calendar</code> behaviour.   This doesn’t make implementing a calendar any more complex.  It does potentially have the side effect of having to generate calendar modules at runtime for some use cases.</p>
<h3><a name="p-108266-calendar-behaviour-2" class="anchor" href="#p-108266-calendar-behaviour-2" aria-label="Heading link" rel="nofollow"></a>Calendar Behaviour</h3>
<p>If you accept that a <code>Calendar</code> module encapsulates all the required configuration and implementation required for the <code>behaviour</code> then implementing <code>strftime</code> comes back to mostly calling the behaviour functions.  And <code>week_of_year</code> should definitely stay - in all of those business calendars is used a lot and the notion of a week is, I think we would all agree, a very common case.</p>
<p>If you got this far then the missing pieces of the <code>Calendar</code> <code>behaviour</code> that would be required to support <code>strftime</code> would be <code>day_name</code>, <code>month_name</code> (short and long) and the <code>am/pm</code> functionality described by <a class="mention" href="/u/josevalim" rel="nofollow">@josevalim</a> above.  Since these will need to be implemented anyway it feels as if they should be part of the <code>Calendar</code> behaviour.</p>
<h3><a name="p-108266-formatting-3" class="anchor" href="#p-108266-formatting-3" aria-label="Heading link" rel="nofollow"></a>Formatting</h3>
<p>Formatting for  <code>strftime</code> should not be the calendars concern (in my opinion).  Thats a representational issue and other than names for days and months, formatting should be the <code>strftime</code> formats responsibility.  In examining the Ruby and Python implementations I see that the Python version is quite sparse but the Ruby version includes an approach that I think is pragmatic.</p>
<h3><a name="p-108266-a-proposal-4" class="anchor" href="#p-108266-a-proposal-4" aria-label="Heading link" rel="nofollow"></a>A Proposal</h3>
<pre data-code-wrap="elixir"><code class="lang-elixir">@spec strftime(date_or_time_or_datetime, format :: String.t(), options :: Map.t()) :: 
    String.t | {:error, String.t}
</code></pre>
<p><code>format</code> is a format string as described in the original proposal however it includes an optional <code>width</code> that is used to determine padding.  This is similar to how <code>printf</code> and friends in <code>C</code> define padding.</p>
<p>Format encoding: <code>%&lt;flags&gt;&lt;width&gt;&lt;conversion&gt;</code>.</p>
<p>Flags (based upon the Ruby implementation)</p>
<div class="md-table">
<table>
<thead>
<tr>
<th>Flag</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>-</td>
<td>don’t pad a numerical output</td>
</tr>
<tr>
<td>_</td>
<td>use spaces for padding</td>
</tr>
<tr>
<td>0</td>
<td>use zeros for padding</td>
</tr>
<tr>
<td>^</td>
<td>upcase the result string</td>
</tr>
<tr>
<td>#</td>
<td>change case</td>
</tr>
<tr>
<td>:</td>
<td>use colons for %z</td>
</tr>
</tbody>
</table>
</div><p>Using this approach allows a calendar implementor to use the <code>%x</code> directive to return a format string that uncludes the required padding.  That is, <code>%x</code> is really just an interpolation of a format defined in the calendar.  And that format can include the appropriate padding.  Same would apply for <code>%X</code> and <code>%c</code>.  The format for these “preferred formats” can also then vary based upon locale without too much difficulty in any locale-aware calendar.</p>
<h3><a name="p-108266-examples-5" class="anchor" href="#p-108266-examples-5" aria-label="Heading link" rel="nofollow"></a>Examples</h3>
<div class="md-table">
<table>
<thead>
<tr>
<th>Format</th>
<th>Output</th>
</tr>
</thead>
<tbody>
<tr>
<td>%m</td>
<td>1, 12</td>
</tr>
<tr>
<td>%2m</td>
<td>01, 12</td>
</tr>
<tr>
<td>%A</td>
<td>Monday</td>
</tr>
<tr>
<td>%^A</td>
<td>MONDAY</td>
</tr>
</tbody>
</table>
</div><h2><a name="p-108266-summary-6" class="anchor" href="#p-108266-summary-6" aria-label="Heading link" rel="nofollow"></a>Summary</h2>
<p>This proposal intents to:</p>
<ol>
<li>
<p>Recommend reinforcing the <code>Calendar</code> behaviour and add to it those missing pieces that support formatting which are primarily the <code>day_name</code> and <code>month_name</code> (short and long) functions.</p>
</li>
<li>
<p>Simplify the interaction between <code>strftime</code> and a <code>Calendar</code> module by reverting to the original plan of simply calling the relevant functions in the calendar module.</p>
</li>
<li>
<p>Making formatting decisions part of the <code>format</code> string (ie flags and width modifiers)</p>
</li>
</ol>
<h3><a name="p-108266-options-7" class="anchor" href="#p-108266-options-7" aria-label="Heading link" rel="nofollow"></a>Options</h3>
<p>I have included an <code>options</code> argument to <code>strftime</code> above because for localisation a locale name needs to be specified. In Python a <code>locale</code> is part of the stdlib and runtime environment but not so Elixir.  Therefore it should be passed in to the calendar.  It could be argued that since there is no other part of Elixir that embodies the idea of localisation then this is not the place to start.  I admit my bias given my work on Cldr, but I think the idea of a locale should be able to be specified.</p>
<p>A <code>Calendar</code> should feel free to return <code>{:error, "unknown locale"}</code> if a locale is requested that it doesn’t support.  Of if a more loose contract is preferred then treat the locale as entirely optional.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="108266" data-batch-url="/posts/batch_likers">
                        5
                      </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/proposal-strftime-based-calendar-datetime-formatting/18734/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-108266" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="108266"
                     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 #21"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/18734/load_more?page=3">Load more posts (20 remaining)</a>
</div></template></turbo-stream>