-
Notifications
You must be signed in to change notification settings - Fork 172
Editorial: Rewrite CalendarDateUntil to be simpler and more general #3136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -525,42 +525,33 @@ <h1> | |
</dd> | ||
</dl> | ||
<emu-alg> | ||
1. If _calendar_ is *"iso8601"*, then | ||
1. Let _sign_ be -CompareISODate(_one_, _two_). | ||
1. If _sign_ = 0, return ZeroDateDuration(). | ||
1. Let _years_ be 0. | ||
1. If _largestUnit_ is ~year~, then | ||
1. Let _candidateYears_ be _sign_. | ||
1. Repeat, while ISODateSurpasses(_sign_, _one_.[[Year]] + _candidateYears_, _one_.[[Month]], _one_.[[Day]], _two_) is *false*, | ||
1. Set _years_ to _candidateYears_. | ||
1. Set _candidateYears_ to _candidateYears_ + _sign_. | ||
1. Let _months_ be 0. | ||
1. If _largestUnit_ is ~year~ or _largestUnit_ is ~month~, then | ||
1. Let _candidateMonths_ be _sign_. | ||
1. Let _intermediate_ be BalanceISOYearMonth(_one_.[[Year]] + _years_, _one_.[[Month]] + _candidateMonths_). | ||
1. Repeat, while ISODateSurpasses(_sign_, _intermediate_.[[Year]], _intermediate_.[[Month]], _one_.[[Day]], _two_) is *false*, | ||
1. Set _months_ to _candidateMonths_. | ||
1. Set _candidateMonths_ to _candidateMonths_ + _sign_. | ||
1. Set _intermediate_ to BalanceISOYearMonth(_intermediate_.[[Year]], _intermediate_.[[Month]] + _sign_). | ||
1. Set _intermediate_ to BalanceISOYearMonth(_one_.[[Year]] + _years_, _one_.[[Month]] + _months_). | ||
1. Let _constrained_ be ! RegulateISODate(_intermediate_.[[Year]], _intermediate_.[[Month]], _one_.[[Day]], ~constrain~). | ||
1. Let _weeks_ be 0. | ||
1. If _largestUnit_ is ~week~, then | ||
1. Let _candidateWeeks_ be _sign_. | ||
1. Set _intermediate_ to BalanceISODate(_constrained_.[[Year]], _constrained_.[[Month]], _constrained_.[[Day]] + 7 × _candidateWeeks_). | ||
1. Repeat, while ISODateSurpasses(_sign_, _intermediate_.[[Year]], _intermediate_.[[Month]], _intermediate_.[[Day]], _two_) is *false*, | ||
1. Set _weeks_ to _candidateWeeks_. | ||
1. Set _candidateWeeks_ to _candidateWeeks_ + sign. | ||
1. Set _intermediate_ to BalanceISODate(_intermediate_.[[Year]], _intermediate_.[[Month]], _intermediate_.[[Day]] + 7 × _sign_). | ||
1. Let _days_ be 0. | ||
1. Let _candidateDays_ be _sign_. | ||
1. Set _intermediate_ to BalanceISODate(_constrained_.[[Year]], _constrained_.[[Month]], _constrained_.[[Day]] + 7 × _weeks_ + _candidateDays_). | ||
1. Repeat, while ISODateSurpasses(_sign_, _intermediate_.[[Year]], _intermediate_.[[Month]], _intermediate_.[[Day]], _two_) is *false*, | ||
1. Set _days_ to _candidateDays_. | ||
1. Set _candidateDays_ to _candidateDays_ + _sign_. | ||
1. Set _intermediate_ to BalanceISODate(_intermediate_.[[Year]], _intermediate_.[[Month]], _intermediate_.[[Day]] + _sign_). | ||
1. Return ! CreateDateDurationRecord(_years_, _months_, _weeks_, _days_). | ||
1. Return an implementation-defined Date Duration Record as described above. | ||
1. Let _sign_ be -CompareISODate(_one_, _two_). | ||
1. If _sign_ = 0, return ZeroDateDuration(). | ||
1. Let _duration_ be CreateDateDurationRecord(0, 0, 0, 0). | ||
1. If _largestUnit_ is ~year~, then | ||
1. Let _intermediate_ be _one_. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
1. Repeat, while CompareISODate(_intermediate_, _two_) != _sign_, | ||
1. Set _duration_.[[Years]] to _duration_.[[Years]] + _sign_. | ||
1. Set _intermediate_ to CalendarDateAdd(_calendar_, _one_, _duration_, ~constrain~). | ||
1. Set _duration_.[[Years]] to _duration_.[[Years]] - _sign_. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loop is better written as: loop {
duration.years += sign;
let intermediate = CalendarDateAdd(calendar, one, duration, "constrain");
if CompareISODate(intermediate, two) == sign {
duration.years -= sign;
break;
}
} but I wrote it like I did since we don't have do-while loops in ecmarkup. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure we do:
Some examples:
|
||
1. If _largestUnit_ is ~year~ or _largestUnit_ is ~month~, then | ||
1. Let _intermediate_ be _one_. | ||
1. Repeat, while CompareISODate(_intermediate_, _two_) != _sign_, | ||
1. Set _duration_.[[Months]] to _duration_.[[Months]] + _sign_. | ||
1. Set _intermediate_ to CalendarDateAdd(_calendar_, _one_, _duration_, ~constrain~). | ||
1. Set _duration_.[[Months]] to _duration_.[[Months]] - _sign_. | ||
1. If _largestUnit_ is ~week~, then | ||
1. Let _intermediate_ be _one_. | ||
1. Repeat, while CompareISODate(_intermediate_, _two_) != _sign_, | ||
1. Set _duration_.[[Weeks]] to _duration_.[[Weeks]] + _sign_. | ||
1. Set _intermediate_ to CalendarDateAdd(_calendar_, _one_, _duration_, ~constrain~). | ||
1. Set _duration_.[[Weeks]] to _duration_.[[Weeks]] - _sign_. | ||
1. Let _intermediate_ be _one_. | ||
1. Repeat, while CompareISODate(_intermediate_, _two_) != _sign_, | ||
1. Set _duration_.[[Days]] to _duration_.[[Days]] + _sign_. | ||
1. Set _intermediate_ to CalendarDateAdd(_calendar_, _one_, _duration_, ~constrain~). | ||
1. Set _duration_.[[Days]] to _duration_.[[Days]] - _sign_. | ||
1. Return ! _duration_. | ||
</emu-alg> | ||
</emu-clause> | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double-check that my signs are correct and not negated.