<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="120780" data-post-id="120780">
  <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’ll work on the PR for the <code>inspect</code> part first, that seems the most straight forward.</p>
<p>The <code>week_of_year</code> is by no means a requirement for CLDR-related work.  It’s that to me a calendar that doesn’t have the notion of weeks is incomplete since thats a cycle within which human activity is firmly rooted.  I accept that thats hardly a rigorous argument though.</p>
<p>For the date ranges:</p>
<ul>
<li>
<p><code>year/2</code> can be defined as <code>{year, 1, 1}..{year, months_in_year(year), days_in_month(year, months_in_year(year))}</code></p>
</li>
<li>
<p><code>quarter/2</code> can be defined in terms of <code>months_in_year(year) / 4</code></p>
</li>
</ul>
<p>… and so on.  If there is interest in a more detailed proposal or PR its not much work and I’m happy to do it.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="120780" 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/how-to-support-multiple-week-calendars-in-elixir/18783/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-120780" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="120780"
                     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="120794" data-post-id="120794">
  <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="12" data-topic="18783">
<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>For the date ranges:</p>
<ul>
<li><code>year/2</code> can be defined as <code>{year, 1, 1}..{year, months_in_year(year), days_in_month(year, months_in_year(year))}</code></li>
<li><code>quarter/2</code> can be defined in terms of <code>months_in_year(year) / 4</code></li>
</ul>
</blockquote>
</aside>
<p>Ah nice!</p>
<p>I would like to see that, yes, but as <code>Date.year_range/1</code>, <code>quarter_range/1</code>, etc. What about <code>week_range/1</code> though? We would need <code>days_in_week</code>, wouldn’t we?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="120794" 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/how-to-support-multiple-week-calendars-in-elixir/18783/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-120794" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="120794"
                     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="120798" data-post-id="120798">
  <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>José, I’ve been prototyping a bit today in the way you suggested: implement in <code>Date</code> by leveraging the existing  callbacks.  For <code>month</code> based calendars thats working well.  For <code>week</code> based calendars it may not be that easy.  Work in progress.</p>
<p><code>Date.week_range/2</code> would require <code>Date.week_of_year/2</code> I believe (the earlier conversation).</p>
<p>I’ll summarise my findings here after some more experimentation in a day or so.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="120798" 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/how-to-support-multiple-week-calendars-in-elixir/18783/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-120798" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="120798"
                     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="120806" data-post-id="120806">
  <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>Another question: would the approach above work with week-based calendars since we use month to mean week?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="120806" 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/how-to-support-multiple-week-calendars-in-elixir/18783/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-120806" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="120806"
                     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="131464" data-post-id="131464">
  <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>Well it was more than a day or so but I can summarise my findings now that <a href="https://hex.pm/packages/ex_cldr_dates_times" rel="nofollow">ex_cldr_dates_times</a> is published.</p>
<h3><a name="p-131464-week-calendars-are-complicated-1" class="anchor" href="#p-131464-week-calendars-are-complicated-1" aria-label="Heading link" rel="nofollow"></a>Week calendars are complicated</h3>
<p>The interpretation of a week varies quite a lot, even in Gregorian calendars, based upon usage and expectation.  For example:</p>
<ol>
<li>How is a week interpreted in a month-based calendar like Gregorian (Calendar.ISO)?</li>
<li>How is a month interpreted in a week-based calendar (like ISO Week)? These kinds of calendars (excluding ISO Week) define a “month” based upon 13 week quarters split into one of either <code>4-4-5</code>, <code>4-5-4</code> or <code>4-4-5</code> weeks.</li>
<li>If a calendar starts in July (like The Australian fiscal year calendar) is it referred to as the beginning year or the ending Gregorian year? That is, if the fiscal date is <code>2019-01-01</code> (meaning July 1st) what is the Gregorian date? Is it <code>2019-07-01</code> or is it <code>2020-07-01</code>?</li>
</ol>
<h3><a name="p-131464-when-does-a-week-start-2" class="anchor" href="#p-131464-when-does-a-week-start-2" aria-label="Heading link" rel="nofollow"></a>When does a week start?</h3>
<p>There are both cultural and definitional differences.</p>
<ul>
<li>Different countries start the week on either <code>Sunday</code> or <code>Monday</code> as common practise</li>
<li>Companies in countries that allow defined fiscal years can start their calendar on the “first or last day_of_week”. For example, Cisco’s fiscal year is defined as <code>last Saturday in July</code>.</li>
<li>Fiscal calendars for countries can and do start in different Gregorian months of the year.  For example, the UK fiscal year starts in April, Australia in July and the US in September.</li>
</ul>
<h3><a name="p-131464-calendar-configuration-for-week-based-calendars-3" class="anchor" href="#p-131464-calendar-configuration-for-week-based-calendars-3" aria-label="Heading link" rel="nofollow"></a>Calendar configuration for week based calendars</h3>
<p>In order to provide configuration for calendars implementing week-based behaviour I found I had to define the following configuration elements:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># Each quarter has three
# 'months` each of 13 weeks
# in either of a 4,4,5; 4,5,4
# of 5,4,4 layout
weeks_in_month: [4, 4, 5],

# Indicates if the anchor
# represents the beginning
# of the year or the end
begins_or_ends: :begins,

# Calendar begins on the
# :first, :last or :nearest
first_or_last: :first,

# First week of the year begins on this day.
# of the week.
# `day: :first` means that the first week starts
# on the first day of the year. `day: :first` only
# applies to month-based calendars and it
# will also result in a short week for the last
# week of a year
day: 1,

# Year begins in this month
month: 1,

# The year of the last_day or first_day
# is either the year with the :majority
# of months or the :beginning year
# or :ending year
year: :majority,

# First week has at least
# this many days in current
# year
min_days: 7
</code></pre>
<h3><a name="p-131464-callbacks-4" class="anchor" href="#p-131464-callbacks-4" aria-label="Heading link" rel="nofollow"></a>Callbacks</h3>
<p>In <a href="https://github.com/elixir-cldr/cldr_dates_times" rel="nofollow">my implementation</a> I found I required the following callbacks to implement week-based calendars:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  @doc """
  Returns the `month` for a given `year`, `month` or `week`, and `day`
  for a a calendar. Required to provide for returning the notion of a month
  for a week-based calendar,

  The `month_of_year` is calculated based upon the calendar configuration.

  """
  @callback month_of_year(
              year :: Calendar.year(),
              month :: Calendar.month() | Cldr.Calendar.week(),
              day :: Calendar.day()
            ) ::
              Calendar.month()

  @doc """
  Returns a tuple of `{year, week_in_year}` for a given `year`, `month` or `week`, and `day`
  for a a calendar.

  The `week_in_year` is calculated based upon the calendar configuration.

  """
  @callback week_of_year(
              year :: Calendar.year(),
              month :: Calendar.month() | Cldr.Calendar.week(),
              day :: Calendar.day()
            ) ::
              {Calendar.year(), Calendar.week()} | {:error, :not_defined}

  @doc """
  Returns a tuple of `{year, week_in_year}` for a given `year`, `month` or `week`, and `day`
  for a a calendar.

  The `iso_week_of_year` is calculated based on the ISO calendar.

  """
  @callback iso_week_of_year(
              year :: Calendar.year(),
              month :: Calendar.month(),
              day :: Calendar.day()
            ) ::
              {Calendar.year(), Calendar.week()} | {:error, :not_defined}

  @doc """
  Returns a tuple of `{month, week_in_month}` for a given `year`, `month` or `week`, and `day`
  for a a calendar.

  The `week_in_month` is calculated based upon the calendar configuration.

  """
  @callback week_of_month(Calendar.year(), Cldr.Calendar.week(), Calendar.day()) ::
              {Cldr.Calendar.month(), Cldr.Calendar.week()}

  @doc """
  Returns the calendar basis.

  Returns either :week or :month
  """
  @callback calendar_base() :: :week | :month

  @doc """
  Returns the number of periods (which are
  months in a month calendar and weeks in a
  week calendar) in a year

  """
  @callback periods_in_year(year :: Calendar.year()) :: week() | Calendar.month()

  @doc """
  Returns the number of weeks in a year

  """
  @callback weeks_in_year(year :: Calendar.year()) :: week()

  @doc """
  Returns the number of days in a year

  """
  @callback days_in_year(year :: Calendar.year()) :: Calendar.day()
</code></pre>
<h3><a name="p-131464-calendar-interval-callbacks-5" class="anchor" href="#p-131464-calendar-interval-callbacks-5" aria-label="Heading link" rel="nofollow"></a>Calendar Interval callbacks</h3>
<p>I use <code>Interval.year/2</code>, <code>Interval.quarter/2</code> and so on.  Would be renamed <code>Calendar.year_range/2</code> based on the earlier conversation.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  @doc """
  Returns a date range representing the days in a
  calendar year.

  """
  @callback year(year :: Calendar.year()) :: Date.Range.t()

  @doc """
  Returns a date range representing the days in a
  given quarter for a calendar year.

  """
  @callback quarter(year :: Calendar.year(), quarter :: Cldr.Calendar.quarter()) :: Date.Range.t()

  @doc """
  Returns a date range representing the days in a
  given month for a calendar year.

  """
  @callback month(year :: Calendar.year(), month :: Calendar.month()) :: Date.Range.t()

  @doc """
  Returns a date range representing the days in a
  given week for a calendar year. 

  Returns an error tuple if the calendar has no 
  notion of a week as is the case with the Julian 
  calendar. 

  """
  @callback week(year :: Calendar.year(), week :: week()) ::
              Date.Range.t() | {:error, :not_defined}
</code></pre>
<h3><a name="p-131464-basic-calendar-math-6" class="anchor" href="#p-131464-basic-calendar-math-6" aria-label="Heading link" rel="nofollow"></a>Basic Calendar math</h3>
<p>When incrementing or decrementing years, quarters, months, weeks and days - including date ranges - most of the heavy lifting can be done in a base calendar module.</p>
<p>However for incrementing (and decrementing) months and quarters  a callback is required since the nature of a quarter and a month is calendar specific.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  @doc """
  Increments a `Date.t` or `Date.Range.t` by a specified positive
  or negative integer number of periods (year, quarter, month,
  week or day).

  Calendars need only implement this callback for `:months` and `:quarters`
  since all other date periods can be derived.

  """
  @callback plus(
              year :: Calendar.year(),
              month :: Calendar.month() | week(),
              day :: Calendar.day(),
              months_or_quarters :: :months | :quarters,
              increment :: integer,
              options :: Keyword.t()
            ) :: {Calendar.year(), Calendar.month(), Calendar.day()}
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="131464" data-batch-url="/posts/batch_likers">
                        6
                      </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/how-to-support-multiple-week-calendars-in-elixir/18783/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-131464" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="131464"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></div>
  </section>
</div>
    <div class="postbit" id="264888" data-post-id="264888">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>2 posts were split to a new topic: <a href="/t/how-to-run-a-job-periodically/51044" rel="nofollow">How to run a job periodically</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="264888" 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/how-to-support-multiple-week-calendars-in-elixir/18783/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-264888" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="264888"
                     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>